Skip to content

Commit

Permalink
feat(@ngtools/webpack): support non-FESM angular core
Browse files Browse the repository at this point in the history
APF6 does not have fESM anymore, so we need to change our logic to include
any files in core (technically there is a faster way to do this, but for now
and for testing purposes this can go it as is).
  • Loading branch information
hansl authored and filipesilva committed Mar 14, 2018
1 parent 6858291 commit a0fc670
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions packages/@ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,21 +555,15 @@ export class AngularCompilerPlugin {
// Add lazy modules to the context module for @angular/core
compiler.hooks.contextModuleFactory.tap('angular-compiler', (cmf: any) => {
const angularCorePackagePath = require.resolve('@angular/core/package.json');
const angularCorePackageJson = require(angularCorePackagePath);
const angularCoreModulePath = path.resolve(path.dirname(angularCorePackagePath),
angularCorePackageJson['module']);
// Pick the last part after the last node_modules instance. We do this to let people have
// a linked @angular/core or cli which would not be under the same path as the project
// being built.
const angularCoreModuleDir = path.dirname(angularCoreModulePath).split(/node_modules/).pop();

// Also support the es2015 in Angular versions that have it.
let angularCoreEs2015Dir: string | undefined;
if (angularCorePackageJson['es2015']) {
const angularCoreEs2015Path = path.resolve(path.dirname(angularCorePackagePath),
angularCorePackageJson['es2015']);
angularCoreEs2015Dir = path.dirname(angularCoreEs2015Path).split(/node_modules/).pop();
}

// APFv6 does not have single FESM anymore. Instead of verifying if we're pointing to
// FESMs, we resolve the `@angular/core` path and verify that the path for the
// module starts with it.

// This may be slower but it will be compatible with both APF5, 6 and potential future
// versions (until the dynamic import appears outside of core I suppose).
// We resolve any symbolic links in order to get the real path that would be used in webpack.
const angularCoreDirname = fs.realpathSync(path.dirname(angularCorePackagePath));

cmf.hooks.afterResolve.tapAsync('angular-compiler',
(result: any, callback: (err?: any, request?: any) => void) => {
Expand All @@ -578,8 +572,7 @@ export class AngularCompilerPlugin {
}

// Alter only request from Angular.
if (!(angularCoreModuleDir && result.resource.endsWith(angularCoreModuleDir))
&& !(angularCoreEs2015Dir && result.resource.endsWith(angularCoreEs2015Dir))) {
if (!result.resource.startsWith(angularCoreDirname)) {
return callback(null, result);
}

Expand Down

0 comments on commit a0fc670

Please sign in to comment.