Skip to content

Commit c1e6529

Browse files
alan-agius4alexeagle
authored andcommitted
fix(@schematics/angular): older projects are not migrated to support differential loading
Fixes #14321
1 parent 16c8d59 commit c1e6529

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

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

+54-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {
9+
JsonAstObject,
910
JsonParseMode,
1011
isJsonObject,
1112
join,
@@ -17,6 +18,7 @@ import { Rule, Tree } from '@angular-devkit/schematics';
1718
import {
1819
findPropertyInAstObject,
1920
insertPropertyInAstObjectInOrder,
21+
removePropertyInAstObject,
2022
} from '../../utility/json-utils';
2123

2224
// tslint:disable-next-line:max-line-length
@@ -40,24 +42,12 @@ not IE 9-11 # For IE 9-11 support, remove 'not'.`;
4042
export function updateES5Projects(): Rule {
4143
return (host: Tree) => {
4244
const tsConfigPath = '/tsconfig.json';
43-
const buffer = host.read(tsConfigPath);
44-
if (!buffer) {
45-
return host;
46-
}
47-
48-
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
49-
50-
if (tsCfgAst.kind !== 'object') {
51-
return host;
52-
}
53-
54-
const compilerOptions = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
55-
if (!compilerOptions || compilerOptions.kind !== 'object') {
45+
const compilerOptions = getCompilerOptionsAstObject(host, tsConfigPath);
46+
if (!compilerOptions) {
5647
return host;
5748
}
5849

5950
const recorder = host.beginUpdate(tsConfigPath);
60-
6151
const scriptTarget = findPropertyInAstObject(compilerOptions, 'target');
6252
if (!scriptTarget) {
6353
insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'target', 'es2015', 4);
@@ -78,11 +68,11 @@ export function updateES5Projects(): Rule {
7868

7969
host.commitUpdate(recorder);
8070

81-
return updateBrowserlist;
71+
return updateProjects;
8272
};
8373
}
8474

85-
function updateBrowserlist(): Rule {
75+
function updateProjects(): Rule {
8676
return (tree) => {
8777
const angularConfigContent = tree.read('angular.json') || tree.read('.angular.json');
8878

@@ -110,6 +100,34 @@ function updateBrowserlist(): Rule {
110100
continue;
111101
}
112102

103+
// Older projects app and spec ts configs had script and module set in them.
104+
const tsConfigs = [];
105+
const architect = project.architect;
106+
if (isJsonObject(architect)
107+
&& isJsonObject(architect.build)
108+
&& isJsonObject(architect.build.options)
109+
&& typeof architect.build.options.tsConfig === 'string') {
110+
tsConfigs.push(architect.build.options.tsConfig);
111+
}
112+
113+
if (isJsonObject(architect)
114+
&& isJsonObject(architect.test)
115+
&& isJsonObject(architect.test.options)
116+
&& typeof architect.test.options.tsConfig === 'string') {
117+
tsConfigs.push(architect.test.options.tsConfig);
118+
}
119+
120+
for (const tsConfig of tsConfigs) {
121+
const compilerOptions = getCompilerOptionsAstObject(tree, tsConfig);
122+
if (!compilerOptions) {
123+
continue;
124+
}
125+
const recorder = tree.beginUpdate(tsConfig);
126+
removePropertyInAstObject(recorder, compilerOptions, 'target');
127+
removePropertyInAstObject(recorder, compilerOptions, 'module');
128+
tree.commitUpdate(recorder);
129+
}
130+
113131
const browserslistPath = join(normalize(project.root), 'browserslist');
114132
if (typeof project.sourceRoot === 'string') {
115133
// Move the CLI 7 style browserlist to root if it's there.
@@ -143,3 +161,23 @@ function updateBrowserlist(): Rule {
143161
return tree;
144162
};
145163
}
164+
165+
function getCompilerOptionsAstObject(host: Tree, tsConfigPath: string): JsonAstObject | undefined {
166+
const buffer = host.read(tsConfigPath);
167+
if (!buffer) {
168+
return;
169+
}
170+
171+
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
172+
173+
if (tsCfgAst.kind !== 'object') {
174+
return;
175+
}
176+
177+
const compilerOptions = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
178+
if (!compilerOptions || compilerOptions.kind !== 'object') {
179+
return;
180+
}
181+
182+
return compilerOptions;
183+
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,26 @@ describe('Migration to version 8', () => {
102102
expect(content).toContain('Chrome 41');
103103
expect(content).toContain('last 2 Chrome versions');
104104
});
105+
106+
it(`should remove 'target' and 'module' from non workspace tsconfig.json`, () => {
107+
const appTsConfig = '/tsconfig.app.json';
108+
const specsTsConfig = '/tsconfig.spec.json';
109+
const compilerOptions = {
110+
...oldTsConfig.compilerOptions,
111+
target: 'es2015',
112+
module: 'es2015',
113+
};
114+
115+
tree.overwrite(appTsConfig, JSON.stringify({ compilerOptions }, null, 2));
116+
const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch());
117+
const { compilerOptions: appCompilerOptions } = JSON.parse(tree2.readContent(appTsConfig));
118+
expect(appCompilerOptions.target).toBeUndefined();
119+
expect(appCompilerOptions.module).toBeUndefined();
120+
121+
const { compilerOptions: specsCompilerOptions }
122+
= JSON.parse(tree2.readContent(specsTsConfig));
123+
expect(specsCompilerOptions.target).toBeUndefined();
124+
expect(specsCompilerOptions.module).toBeUndefined();
125+
});
105126
});
106127
});

0 commit comments

Comments
 (0)