Skip to content

Commit 8a99f0b

Browse files
committed
fix(@angular/cli): don't search for lazy routes in Angular 5 ng test
Fix angular#8066
1 parent b4c94b4 commit 8a99f0b

File tree

6 files changed

+63
-75
lines changed

6 files changed

+63
-75
lines changed

packages/@angular/cli/models/webpack-configs/typescript.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const webpackLoader: string = g['angularCliIsLocal']
1818
: '@ngtools/webpack';
1919

2020

21-
function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
21+
function _createAotPlugin(wco: WebpackConfigOptions, options: any, useMain = true) {
2222
const { appConfig, projectRoot, buildOptions } = wco;
2323
options.compilerOptions = options.compilerOptions || {};
2424

@@ -82,7 +82,7 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
8282

8383
if (AngularCompilerPlugin.isSupported()) {
8484
const pluginOptions: AngularCompilerPluginOptions = Object.assign({}, {
85-
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
85+
mainPath: useMain ? path.join(projectRoot, appConfig.root, appConfig.main) : undefined,
8686
i18nInFile: buildOptions.i18nFile,
8787
i18nInFormat: buildOptions.i18nFormat,
8888
i18nOutFile: buildOptions.i18nOutFile,
@@ -160,23 +160,28 @@ export function getNonAotTestConfig(wco: WebpackConfigOptions) {
160160
const tsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.testTsconfig);
161161
const appTsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);
162162

163-
// Force include main and polyfills.
164-
// This is needed for AngularCompilerPlugin compatibility with existing projects,
165-
// since TS compilation there is stricter and tsconfig.spec.ts doesn't include them.
166-
const include = [appConfig.main, appConfig.polyfills, '**/*.spec.ts'];
167-
if (appConfig.test) {
168-
include.push(appConfig.test);
169-
}
163+
let pluginOptions: any = { tsConfigPath, skipCodeGeneration: true };
170164

171-
let pluginOptions: any = { tsConfigPath, skipCodeGeneration: true, include };
165+
// The options below only apply to AoTPlugin.
166+
if (!AngularCompilerPlugin.isSupported()) {
167+
// Force include main and polyfills.
168+
// This is needed for AngularCompilerPlugin compatibility with existing projects,
169+
// since TS compilation there is stricter and tsconfig.spec.ts doesn't include them.
170+
const include = [appConfig.main, appConfig.polyfills, '**/*.spec.ts'];
171+
if (appConfig.test) {
172+
include.push(appConfig.test);
173+
}
172174

173-
// Fallback to correct module format on projects using a shared tsconfig.
174-
if (tsConfigPath === appTsConfigPath) {
175-
pluginOptions.compilerOptions = { module: 'commonjs' };
175+
pluginOptions.include = include;
176+
177+
// Fallback to correct module format on projects using a shared tsconfig.
178+
if (tsConfigPath === appTsConfigPath) {
179+
pluginOptions.compilerOptions = { module: 'commonjs' };
180+
}
176181
}
177182

178183
return {
179184
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
180-
plugins: [ _createAotPlugin(wco, pluginOptions) ]
185+
plugins: [ _createAotPlugin(wco, pluginOptions, false) ]
181186
};
182187
}

packages/@ngtools/webpack/src/angular_compiler_plugin.ts

+24-31
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export interface AngularCompilerPluginOptions {
5959
entryModule?: string;
6060
mainPath?: string;
6161
skipCodeGeneration?: boolean;
62-
hostOverrideFileSystem?: { [path: string]: string };
6362
hostReplacementPaths?: { [path: string]: string };
6463
i18nInFile?: string;
6564
i18nInFormat?: string;
@@ -240,13 +239,6 @@ export class AngularCompilerPlugin implements Tapable {
240239
tsHost: webpackCompilerHost
241240
}) as CompilerHost & WebpackCompilerHost;
242241

243-
// Override some files in the FileSystem.
244-
if (this._options.hostOverrideFileSystem) {
245-
for (const filePath of Object.keys(this._options.hostOverrideFileSystem)) {
246-
this._compilerHost.writeFile(filePath,
247-
this._options.hostOverrideFileSystem[filePath], false);
248-
}
249-
}
250242
// Override some files in the FileSystem with paths from the actual file system.
251243
if (this._options.hostReplacementPaths) {
252244
for (const filePath of Object.keys(this._options.hostReplacementPaths)) {
@@ -645,9 +637,8 @@ export class AngularCompilerPlugin implements Tapable {
645637

646638
private _makeTransformers() {
647639

648-
// TODO use compilerhost.denormalize when #8210 is merged.
649640
const isAppPath = (fileName: string) =>
650-
this._rootNames.includes(fileName.replace(/\//g, path.sep));
641+
!fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
651642
const isMainPath = (fileName: string) => fileName === this._mainPath;
652643
const getEntryModule = () => this.entryModule;
653644
const getLazyRoutes = () => this._lazyRoutes;
@@ -693,34 +684,36 @@ export class AngularCompilerPlugin implements Tapable {
693684
// Make a new program and load the Angular structure.
694685
.then(() => this._createOrUpdateProgram())
695686
.then(() => {
696-
// Try to find lazy routes.
697-
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
698-
// and other things that we might miss using the (faster) findLazyRoutesInAst.
699-
// Lazy routes modules will be read with compilerHost and added to the changed files.
700-
const changedTsFiles = this._getChangedTsFiles();
701-
if (this._ngCompilerSupportsNewApi) {
702-
this._processLazyRoutes(this._listLazyRoutesFromProgram());
703-
} else if (this._firstRun) {
704-
this._processLazyRoutes(this._getLazyRoutesFromNgtools());
705-
} else if (changedTsFiles.length > 0) {
706-
this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles));
687+
if (this.entryModule) {
688+
// Try to find lazy routes if we have an entry module.
689+
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
690+
// and other things that we might miss using the (faster) findLazyRoutesInAst.
691+
// Lazy routes modules will be read with compilerHost and added to the changed files.
692+
const changedTsFiles = this._getChangedTsFiles();
693+
if (this._ngCompilerSupportsNewApi) {
694+
this._processLazyRoutes(this._listLazyRoutesFromProgram());
695+
} else if (this._firstRun) {
696+
this._processLazyRoutes(this._getLazyRoutesFromNgtools());
697+
} else if (changedTsFiles.length > 0) {
698+
this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles));
699+
}
707700
}
708701
})
709702
.then(() => {
710703
// Emit and report errors.
711704

712705
// We now have the final list of changed TS files.
713706
// Go through each changed file and add transforms as needed.
714-
const sourceFiles = this._getChangedTsFiles().map((fileName) => {
715-
time('AngularCompilerPlugin._update.getSourceFile');
716-
const sourceFile = this._getTsProgram().getSourceFile(fileName);
717-
if (!sourceFile) {
718-
throw new Error(`${fileName} is not part of the TypeScript compilation. `
719-
+ `Please include it in your tsconfig via the 'files' or 'include' property.`);
720-
}
721-
timeEnd('AngularCompilerPlugin._update.getSourceFile');
722-
return sourceFile;
723-
});
707+
const sourceFiles = this._getChangedTsFiles()
708+
.map((fileName) => this._getTsProgram().getSourceFile(fileName))
709+
// At this point we shouldn't need to filter out undefined files, because any ts file
710+
// that changed should be emitted.
711+
// But due to hostReplacementPaths there can be files (the environment files)
712+
// that changed but aren't part of the compilation, specially on `ng test`.
713+
// So we ignore missing source files files here.
714+
// hostReplacementPaths needs to be fixed anyway to take care of the following issue.
715+
// https://github.com/angular/angular-cli/issues/7305#issuecomment-332150230
716+
.filter((x) => !!x);
724717

725718
// Emit files.
726719
time('AngularCompilerPlugin._update._emit');

tests/e2e/setup/500-create-project.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,20 @@ export default function() {
5252
.then(() => updateTsConfig(json => {
5353
json['compilerOptions']['sourceRoot'] = '/';
5454
}))
55-
.then(() => updateJsonFile('src/tsconfig.spec.json', json => {
55+
.then(() => {
5656
if (argv.nightly) {
5757
// *************************************************************************************
5858
// REMOVE THIS WITH UPDATED NG5 SCHEMATICS
5959
// In ng5 we have to tell users users to update their tsconfig.json.
60-
// `src/tsconfig.spec.json` needs to be updated with `"include": [ "**/*.ts" ]`
60+
// `src/tsconfig.spec.json` needs to be updated with "polyfills.ts" on `include`.
6161
// *************************************************************************************
62-
json['include'] = ['**/*.ts'];
62+
return updateJsonFile('src/tsconfig.spec.json', json => {
63+
json['include'] = json['include'].concat('polyfills.ts');
64+
})
65+
// Ignore error if file doesn't exist.
66+
.catch(() => { return; });
6367
}
64-
}))
68+
})
6569
.then(() => git('config', 'user.email', '[email protected]'))
6670
.then(() => git('config', 'user.name', 'Angular CLI E2e'))
6771
.then(() => git('config', 'commit.gpgSign', 'false'))

tests/e2e/tests/lint/lint-no-project.ts

-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ export default function () {
2424
return stdout;
2525
})
2626
.then(() => ng('set', 'lint.0.files', '"**/baz.ts"'))
27-
.then(() => {
28-
// Starting with ng5, tsconfig.spec.json includes all ts files, so linting for it must
29-
// also set the files.
30-
if (getGlobalVariable('argv').nightly) {
31-
return ng('set', 'lint.1.files', '"**/baz.ts"');
32-
}
33-
})
3427
.then(() => writeFile('src/app/foo.ts', 'const foo = "";\n'))
3528
.then(() => writeFile('src/app/baz.ts', 'const baz = \'\';\n'))
3629
.then(() => ng('lint'))

tests/e2e/tests/lint/lint-with-exclude.ts

-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ export default function () {
1313

1414
return Promise.resolve()
1515
.then(() => ng('set', 'lint.0.exclude', '"**/foo.ts"'))
16-
.then(() => {
17-
// Starting with ng5, tsconfig.spec.json includes all ts files, so linting for it must
18-
// also set the files.
19-
if (getGlobalVariable('argv').nightly) {
20-
return ng('set', 'lint.1.exclude', '"**/foo.ts"');
21-
}
22-
})
2316
.then(() => writeFile(fileName, 'const foo = "";\n'))
2417
.then(() => ng('lint'))
2518
.then(({ stdout }) => {

tests/e2e/utils/project.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ export function createProject(name: string, ...args: string[]) {
4444
.then(() => argv['ng2'] ? useNg2() : Promise.resolve())
4545
.then(() => argv.nightly || argv['ng-sha'] ? useSha() : Promise.resolve())
4646
.then(() => {
47-
// *************************************************************************************
48-
// REMOVE THIS WITH UPDATED NG5 SCHEMATICS
49-
// In ng5 we have to tell users users to update their tsconfig.json.
50-
// `src/tsconfig.spec.json` needs to be updated with `"include": [ "**/*.ts" ]`
51-
// *************************************************************************************
52-
return updateJsonFile('src/tsconfig.spec.json', json => {
53-
if (argv.nightly) {
54-
json['include'] = ['**/*.ts'];
55-
}
56-
})
57-
// Ignore error if file doesn't exist.
58-
.catch(() => { return; });
47+
if (argv.nightly) {
48+
// *************************************************************************************
49+
// REMOVE THIS WITH UPDATED NG5 SCHEMATICS
50+
// In ng5 we have to tell users users to update their tsconfig.json.
51+
// `src/tsconfig.spec.json` needs to be updated with "polyfills.ts" on `include`.
52+
// *************************************************************************************
53+
return updateJsonFile('src/tsconfig.spec.json', json => {
54+
json['include'] = json['include'].concat('polyfills.ts');
55+
})
56+
// Ignore error if file doesn't exist.
57+
.catch(() => { return; });
58+
}
5959
})
6060
.then(() => console.log(`Project ${name} created... Installing npm.`))
6161
.then(() => silentNpm('install'));

0 commit comments

Comments
 (0)