Skip to content

Commit

Permalink
fix(@angular/cli): don't search for lazy routes in Angular 5 ng test
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva authored and Zhicheng Wang committed Nov 2, 2017
1 parent 5e0073a commit afb6103
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 75 deletions.
33 changes: 19 additions & 14 deletions packages/@angular/cli/models/webpack-configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const webpackLoader: string = g['angularCliIsLocal']
: '@ngtools/webpack';


function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
function _createAotPlugin(wco: WebpackConfigOptions, options: any, useMain = true) {
const { appConfig, projectRoot, buildOptions } = wco;
options.compilerOptions = options.compilerOptions || {};

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

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

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

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

// Fallback to correct module format on projects using a shared tsconfig.
if (tsConfigPath === appTsConfigPath) {
pluginOptions.compilerOptions = { module: 'commonjs' };
pluginOptions.include = include;

// Fallback to correct module format on projects using a shared tsconfig.
if (tsConfigPath === appTsConfigPath) {
pluginOptions.compilerOptions = { module: 'commonjs' };
}
}

return {
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
plugins: [ _createAotPlugin(wco, pluginOptions) ]
plugins: [ _createAotPlugin(wco, pluginOptions, false) ]
};
}
55 changes: 24 additions & 31 deletions packages/@ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export interface AngularCompilerPluginOptions {
entryModule?: string;
mainPath?: string;
skipCodeGeneration?: boolean;
hostOverrideFileSystem?: { [path: string]: string };
hostReplacementPaths?: { [path: string]: string };
i18nInFile?: string;
i18nInFormat?: string;
Expand Down Expand Up @@ -240,13 +239,6 @@ export class AngularCompilerPlugin implements Tapable {
tsHost: webpackCompilerHost
}) as CompilerHost & WebpackCompilerHost;

// Override some files in the FileSystem.
if (this._options.hostOverrideFileSystem) {
for (const filePath of Object.keys(this._options.hostOverrideFileSystem)) {
this._compilerHost.writeFile(filePath,
this._options.hostOverrideFileSystem[filePath], false);
}
}
// Override some files in the FileSystem with paths from the actual file system.
if (this._options.hostReplacementPaths) {
for (const filePath of Object.keys(this._options.hostReplacementPaths)) {
Expand Down Expand Up @@ -645,9 +637,8 @@ export class AngularCompilerPlugin implements Tapable {

private _makeTransformers() {

// TODO use compilerhost.denormalize when #8210 is merged.
const isAppPath = (fileName: string) =>
this._rootNames.includes(fileName.replace(/\//g, path.sep));
!fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
const isMainPath = (fileName: string) => fileName === this._mainPath;
const getEntryModule = () => this.entryModule;
const getLazyRoutes = () => this._lazyRoutes;
Expand Down Expand Up @@ -693,34 +684,36 @@ export class AngularCompilerPlugin implements Tapable {
// Make a new program and load the Angular structure.
.then(() => this._createOrUpdateProgram())
.then(() => {
// Try to find lazy routes.
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
// and other things that we might miss using the (faster) findLazyRoutesInAst.
// Lazy routes modules will be read with compilerHost and added to the changed files.
const changedTsFiles = this._getChangedTsFiles();
if (this._ngCompilerSupportsNewApi) {
this._processLazyRoutes(this._listLazyRoutesFromProgram());
} else if (this._firstRun) {
this._processLazyRoutes(this._getLazyRoutesFromNgtools());
} else if (changedTsFiles.length > 0) {
this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles));
if (this.entryModule) {
// Try to find lazy routes if we have an entry module.
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
// and other things that we might miss using the (faster) findLazyRoutesInAst.
// Lazy routes modules will be read with compilerHost and added to the changed files.
const changedTsFiles = this._getChangedTsFiles();
if (this._ngCompilerSupportsNewApi) {
this._processLazyRoutes(this._listLazyRoutesFromProgram());
} else if (this._firstRun) {
this._processLazyRoutes(this._getLazyRoutesFromNgtools());
} else if (changedTsFiles.length > 0) {
this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles));
}
}
})
.then(() => {
// Emit and report errors.

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

// Emit files.
time('AngularCompilerPlugin._update._emit');
Expand Down
12 changes: 8 additions & 4 deletions tests/e2e/setup/500-create-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,20 @@ export default function() {
.then(() => updateTsConfig(json => {
json['compilerOptions']['sourceRoot'] = '/';
}))
.then(() => updateJsonFile('src/tsconfig.spec.json', json => {
.then(() => {
if (argv.nightly) {
// *************************************************************************************
// REMOVE THIS WITH UPDATED NG5 SCHEMATICS
// In ng5 we have to tell users users to update their tsconfig.json.
// `src/tsconfig.spec.json` needs to be updated with `"include": [ "**/*.ts" ]`
// `src/tsconfig.spec.json` needs to be updated with "polyfills.ts" on `include`.
// *************************************************************************************
json['include'] = ['**/*.ts'];
return updateJsonFile('src/tsconfig.spec.json', json => {
json['include'] = json['include'].concat('polyfills.ts');
})
// Ignore error if file doesn't exist.
.catch(() => { return; });
}
}))
})
.then(() => git('config', 'user.email', '[email protected]'))
.then(() => git('config', 'user.name', 'Angular CLI E2e'))
.then(() => git('config', 'commit.gpgSign', 'false'))
Expand Down
7 changes: 0 additions & 7 deletions tests/e2e/tests/lint/lint-no-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ export default function () {
return stdout;
})
.then(() => ng('set', 'lint.0.files', '"**/baz.ts"'))
.then(() => {
// Starting with ng5, tsconfig.spec.json includes all ts files, so linting for it must
// also set the files.
if (getGlobalVariable('argv').nightly) {
return ng('set', 'lint.1.files', '"**/baz.ts"');
}
})
.then(() => writeFile('src/app/foo.ts', 'const foo = "";\n'))
.then(() => writeFile('src/app/baz.ts', 'const baz = \'\';\n'))
.then(() => ng('lint'))
Expand Down
7 changes: 0 additions & 7 deletions tests/e2e/tests/lint/lint-with-exclude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ export default function () {

return Promise.resolve()
.then(() => ng('set', 'lint.0.exclude', '"**/foo.ts"'))
.then(() => {
// Starting with ng5, tsconfig.spec.json includes all ts files, so linting for it must
// also set the files.
if (getGlobalVariable('argv').nightly) {
return ng('set', 'lint.1.exclude', '"**/foo.ts"');
}
})
.then(() => writeFile(fileName, 'const foo = "";\n'))
.then(() => ng('lint'))
.then(({ stdout }) => {
Expand Down
24 changes: 12 additions & 12 deletions tests/e2e/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ export function createProject(name: string, ...args: string[]) {
.then(() => argv['ng2'] ? useNg2() : Promise.resolve())
.then(() => argv.nightly || argv['ng-sha'] ? useSha() : Promise.resolve())
.then(() => {
// *************************************************************************************
// REMOVE THIS WITH UPDATED NG5 SCHEMATICS
// In ng5 we have to tell users users to update their tsconfig.json.
// `src/tsconfig.spec.json` needs to be updated with `"include": [ "**/*.ts" ]`
// *************************************************************************************
return updateJsonFile('src/tsconfig.spec.json', json => {
if (argv.nightly) {
json['include'] = ['**/*.ts'];
}
})
// Ignore error if file doesn't exist.
.catch(() => { return; });
if (argv.nightly) {
// *************************************************************************************
// REMOVE THIS WITH UPDATED NG5 SCHEMATICS
// In ng5 we have to tell users users to update their tsconfig.json.
// `src/tsconfig.spec.json` needs to be updated with "polyfills.ts" on `include`.
// *************************************************************************************
return updateJsonFile('src/tsconfig.spec.json', json => {
json['include'] = json['include'].concat('polyfills.ts');
})
// Ignore error if file doesn't exist.
.catch(() => { return; });
}
})
.then(() => console.log(`Project ${name} created... Installing npm.`))
.then(() => silentNpm('install'));
Expand Down

0 comments on commit afb6103

Please sign in to comment.