Skip to content

Commit a0fc670

Browse files
hanslfilipesilva
authored andcommitted
feat(@ngtools/webpack): support non-FESM angular core
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).
1 parent 6858291 commit a0fc670

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -555,21 +555,15 @@ export class AngularCompilerPlugin {
555555
// Add lazy modules to the context module for @angular/core
556556
compiler.hooks.contextModuleFactory.tap('angular-compiler', (cmf: any) => {
557557
const angularCorePackagePath = require.resolve('@angular/core/package.json');
558-
const angularCorePackageJson = require(angularCorePackagePath);
559-
const angularCoreModulePath = path.resolve(path.dirname(angularCorePackagePath),
560-
angularCorePackageJson['module']);
561-
// Pick the last part after the last node_modules instance. We do this to let people have
562-
// a linked @angular/core or cli which would not be under the same path as the project
563-
// being built.
564-
const angularCoreModuleDir = path.dirname(angularCoreModulePath).split(/node_modules/).pop();
565-
566-
// Also support the es2015 in Angular versions that have it.
567-
let angularCoreEs2015Dir: string | undefined;
568-
if (angularCorePackageJson['es2015']) {
569-
const angularCoreEs2015Path = path.resolve(path.dirname(angularCorePackagePath),
570-
angularCorePackageJson['es2015']);
571-
angularCoreEs2015Dir = path.dirname(angularCoreEs2015Path).split(/node_modules/).pop();
572-
}
558+
559+
// APFv6 does not have single FESM anymore. Instead of verifying if we're pointing to
560+
// FESMs, we resolve the `@angular/core` path and verify that the path for the
561+
// module starts with it.
562+
563+
// This may be slower but it will be compatible with both APF5, 6 and potential future
564+
// versions (until the dynamic import appears outside of core I suppose).
565+
// We resolve any symbolic links in order to get the real path that would be used in webpack.
566+
const angularCoreDirname = fs.realpathSync(path.dirname(angularCorePackagePath));
573567

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

580574
// Alter only request from Angular.
581-
if (!(angularCoreModuleDir && result.resource.endsWith(angularCoreModuleDir))
582-
&& !(angularCoreEs2015Dir && result.resource.endsWith(angularCoreEs2015Dir))) {
575+
if (!result.resource.startsWith(angularCoreDirname)) {
583576
return callback(null, result);
584577
}
585578

0 commit comments

Comments
 (0)