Skip to content

Commit

Permalink
fix(AoTPlugin): don't override context module deps
Browse files Browse the repository at this point in the history
Fix #2496
  • Loading branch information
filipesilva committed Jan 21, 2017
1 parent 08bb738 commit 9fc3d40
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
31 changes: 22 additions & 9 deletions packages/@ngtools/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ export class AotPlugin implements Tapable {
return callback();
}

// alter only request from @angular/core/src/linker
if (!request.context.endsWith(path.join('@angular/core/src/linker'))) {
return callback(null, request);
}

request.request = this.skipCodeGeneration ? this.basePath : this.genDir;
request.recursive = true;
request.dependencies.forEach((d: any) => d.critical = false);
Expand All @@ -210,16 +215,24 @@ export class AotPlugin implements Tapable {
return callback();
}

this.done.then(() => {
result.resource = this.skipCodeGeneration ? this.basePath : this.genDir;
result.recursive = true;
result.dependencies.forEach((d: any) => d.critical = false);
result.resolveDependencies = createResolveDependenciesFromContextMap(
(_: any, cb: any) => cb(null, this._lazyRoutes));

// there is no way to find the original request, so try to
// match request from @angular/core/src/linker as best as possible
if (
result.resource == (this.skipCodeGeneration ? this.basePath : this.genDir)
&& result.recursive == true
&& result.dependencies.every((d: any) => d.critical == false)
) {
this.done.then(() => {
result.resolveDependencies = createResolveDependenciesFromContextMap(
this._lazyRoutes,
result.resolveDependencies
);
return callback(null, result);
}, () => callback(null))
.catch(err => callback(err));
} else {
return callback(null, result);
}, () => callback(null))
.catch(err => callback(err));
}
});
});

Expand Down
18 changes: 12 additions & 6 deletions packages/@ngtools/webpack/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
const ContextElementDependency = require('webpack/lib/dependencies/ContextElementDependency');

export function createResolveDependenciesFromContextMap(createContextMap: Function) {
export function createResolveDependenciesFromContextMap(
contextMap: { [route: string]: string },
originalResolveDependencies: any
) {
return (fs: any, resource: any, recursive: any, regExp: RegExp, callback: any) => {
createContextMap(fs, function(err: Error, map: any) {
const newCallback = (err: any, deps: any) => {
if (err) {
return callback(err);
}

const dependencies = Object.keys(map)
.map((key) => new ContextElementDependency(map[key], key));
const dependencies = Object.keys(contextMap)
.map((key) => new ContextElementDependency(contextMap[key], key));

callback(null, dependencies);
});
callback(null, deps.concat(dependencies));
};

// Use original resolver and add in context map to result
originalResolveDependencies(fs, resource, recursive, regExp, newCallback);
};
}
37 changes: 34 additions & 3 deletions tests/e2e/tests/misc/lazy-module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {readdirSync} from 'fs';
import {oneLine} from 'common-tags';

import {ng} from '../../utils/process';
import {ng, npm} from '../../utils/process';
import {addImportToModule} from '../../utils/ast';
import {appendToFile, writeFile} from '../../utils/fs';


export default function() {
Expand All @@ -21,13 +22,43 @@ export default function() {
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
throw new Error('A bundle for the lazy module was not created.');
}
oldNumberOfFiles = currentNumberOfDistFiles;
})
// verify System.import still works
.then(() => writeFile('src/app/lazy-file.ts', ''))
.then(() => appendToFile('src/app/app.component.ts', `
// verify other System.import still work
declare var System: any;
const lazyFile = 'file';
System.import('./lazy-' + lazyFile);
`))
.then(() => ng('build'))
.then(() => readdirSync('dist').length)
.then(currentNumberOfDistFiles => {
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
throw new Error('A bundle for the lazy file was not created.');
}
oldNumberOfFiles = currentNumberOfDistFiles;
})
// verify 'import *' syntax doesn't break lazy modules
.then(() => npm('install', 'moment'))
.then(() => appendToFile('src/app/app.component.ts', `
import * as moment from 'moment';
console.log(moment);
`))
.then(() => ng('build'))
.then(() => readdirSync('dist').length)
.then(currentNumberOfDistFiles => {
if (oldNumberOfFiles != currentNumberOfDistFiles) {
throw new Error('Bundles were not created after adding \'import *\'.');
}
})
// Check for AoT and lazy routes.
.then(() => ng('build', '--aot'))
.then(() => readdirSync('dist').length)
.then(currentNumberOfDistFiles => {
if (oldNumberOfFiles >= currentNumberOfDistFiles) {
throw new Error('A bundle for the lazy module was not created.');
if (oldNumberOfFiles != currentNumberOfDistFiles) {
throw new Error('AoT build contains a different number of files.');
}
});
}

0 comments on commit 9fc3d40

Please sign in to comment.