Skip to content

Commit 2fe40ab

Browse files
alan-agius4alexeagle
authored andcommitted
fix(@schematics/angular): move browserslist even when no sourceRoot is available
``` Using package manager: 'npm' Collecting installed dependencies... Found 36 dependencies. ** Executing migrations for package '@angular/cli' ** RENAME src/browserslist => /browserslist UPDATE tslint.json (2800 bytes) UPDATE package.json (1396 bytes) UPDATE tsconfig.json (460 bytes) UPDATE tsconfig.app.json (282 bytes) UPDATE tsconfig.spec.json (256 bytes) ``` Fixes #14660
1 parent d1d5ac7 commit 2fe40ab

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

packages/schematics/angular/migrations/update-8/differential-loading.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,16 @@ export function updateES5Projects(): Rule {
8888
}
8989

9090
const browserslistPath = join(normalize(project.root), 'browserslist');
91-
if (typeof project.sourceRoot === 'string') {
92-
// Move the CLI 7 style browserlist to root if it's there.
93-
const srcBrowsersList = join(normalize(project.sourceRoot), 'browserslist');
94-
if (tree.exists(srcBrowsersList) && !tree.exists(browserslistPath)) {
95-
// TODO: use rename instead.
96-
// This is a hacky workaround. We should be able to just rename it.
97-
// On unit tests the rename works fine but on real projects it fails with
98-
// ERROR! browserslist does not exist..
99-
// This seems to happen because we are both renaming and then commiting an update.
100-
// But it's fine if we read/create/delete. There's a bug somewhere.
101-
// tree.rename(srcBrowsersList, browserslistPath);
102-
const content = tree.read(srcBrowsersList);
103-
if (content) {
104-
tree.create(browserslistPath, content);
105-
tree.delete(srcBrowsersList);
106-
}
107-
}
108-
}
10991

110-
if (!tree.exists(browserslistPath)) {
92+
// Move the CLI 7 style browserlist to root if it's there.
93+
const sourceRoot = project.sourceRoot === 'string'
94+
? project.sourceRoot
95+
: join(normalize(project.root), 'src');
96+
const srcBrowsersList = join(normalize(sourceRoot), 'browserslist');
97+
98+
if (tree.exists(srcBrowsersList)) {
99+
tree.rename(srcBrowsersList, browserslistPath);
100+
} else if (!tree.exists(browserslistPath)) {
111101
tree.create(browserslistPath, browserslistContent);
112102
}
113103
}

packages/schematics/angular/migrations/update-8/differential-loading_spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,18 @@ describe('Migration to version 8', () => {
166166
const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch());
167167
expect(tree2.exists('/browserslist')).toBe(false);
168168
});
169+
170+
it(`should move 'browserslist' to root when 'sourceRoot' is not defined`, () => {
171+
tree.rename('/browserslist', '/src/browserslist');
172+
expect(tree.exists('/src/browserslist')).toBe(true);
173+
174+
const config = JSON.parse(tree.readContent('angular.json'));
175+
config.projects['migration-test'].sourceRoot = undefined;
176+
177+
tree.overwrite('angular.json', JSON.stringify(config));
178+
const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch());
179+
expect(tree2.exists('/src/browserslist')).toBe(false);
180+
expect(tree2.exists('/browserslist')).toBe(true);
181+
});
169182
});
170183
});

0 commit comments

Comments
 (0)