Skip to content

Commit 1154e55

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

File tree

4 files changed

+56
-50
lines changed

4 files changed

+56
-50
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

+17-20
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,16 @@ export class AngularCompilerPlugin implements Tapable {
240240
tsHost: webpackCompilerHost
241241
}) as CompilerHost & WebpackCompilerHost;
242242

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-
}
250243
// Override some files in the FileSystem with paths from the actual file system.
251244
if (this._options.hostReplacementPaths) {
252245
for (const filePath of Object.keys(this._options.hostReplacementPaths)) {
253246
const replacementFilePath = this._options.hostReplacementPaths[filePath];
254247
const content = this._compilerHost.readFile(replacementFilePath);
255248
this._compilerHost.writeFile(filePath, content, false);
256249
}
250+
251+
// Reset the file tracker so overriden files are not picked up as changes.
252+
this._compilerHost.resetChangedFileTracker();
257253
}
258254

259255
// Use an identity function as all our paths are absolute already.
@@ -645,9 +641,8 @@ export class AngularCompilerPlugin implements Tapable {
645641

646642
private _makeTransformers() {
647643

648-
// TODO use compilerhost.denormalize when #8210 is merged.
649644
const isAppPath = (fileName: string) =>
650-
this._rootNames.includes(fileName.replace(/\//g, path.sep));
645+
!fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
651646
const isMainPath = (fileName: string) => fileName === this._mainPath;
652647
const getEntryModule = () => this.entryModule;
653648
const getLazyRoutes = () => this._lazyRoutes;
@@ -691,17 +686,19 @@ export class AngularCompilerPlugin implements Tapable {
691686
// Make a new program and load the Angular structure.
692687
.then(() => this._createOrUpdateProgram())
693688
.then(() => {
694-
// Try to find lazy routes.
695-
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
696-
// and other things that we might miss using the (faster) findLazyRoutesInAst.
697-
// Lazy routes modules will be read with compilerHost and added to the changed files.
698-
const changedTsFiles = this._getChangedTsFiles();
699-
if (this._ngCompilerSupportsNewApi) {
700-
this._processLazyRoutes(this._listLazyRoutesFromProgram());
701-
} else if (this._firstRun) {
702-
this._processLazyRoutes(this._getLazyRoutesFromNgtools());
703-
} else if (changedTsFiles.length > 0) {
704-
this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles));
689+
if (this.entryModule) {
690+
// Try to find lazy routes if we have an entry module.
691+
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
692+
// and other things that we might miss using the (faster) findLazyRoutesInAst.
693+
// Lazy routes modules will be read with compilerHost and added to the changed files.
694+
const changedTsFiles = this._getChangedTsFiles();
695+
if (this._ngCompilerSupportsNewApi) {
696+
this._processLazyRoutes(this._listLazyRoutesFromProgram());
697+
} else if (this._firstRun) {
698+
this._processLazyRoutes(this._getLazyRoutesFromNgtools());
699+
} else if (changedTsFiles.length > 0) {
700+
this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles));
701+
}
705702
}
706703
})
707704
.then(() => {

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/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)