Skip to content

Commit

Permalink
feat(@ngtools/webpack): allow custom lazy module resource
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed Oct 15, 2018
1 parent 237c893 commit bf75794
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export interface AngularCompilerPluginOptions {
missingTranslation?: string;
platform?: PLATFORM;
nameLazyFiles?: boolean;
lazyModulesResource?: string;
logger?: logging.Logger;

// added to the list of lazy routes
Expand All @@ -123,6 +124,15 @@ export enum PLATFORM {
Server,
}

// 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 angularCorePackagePath = require.resolve('@angular/core/package.json');
const angularCoreResourceRoot = fs.realpathSync(path.dirname(angularCorePackagePath));

export class AngularCompilerPlugin {
private _options: AngularCompilerPluginOptions;

Expand Down Expand Up @@ -690,20 +700,16 @@ export class AngularCompilerPlugin {

// Add lazy modules to the context module for @angular/core
compiler.hooks.contextModuleFactory.tap('angular-compiler', cmf => {
const angularCorePackagePath = require.resolve('@angular/core/package.json');

// 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.tapPromise('angular-compiler', async result => {
// Alter only request from Angular.
if (!result || !this.done || !result.resource.startsWith(angularCoreDirname)) {
// Alter only existing request from Angular or one of the additional lazyModulesResources.
const isLazyModuleResource = (resource: string) => {
if (this.options.lazyModulesResource) {
return resource === this.options.lazyModulesResource;
} else {
return resource.startsWith(angularCoreResourceRoot);
}
};
if (!result || !this.done || !isLazyModuleResource(result.resource)) {
return result;
}

Expand Down

0 comments on commit bf75794

Please sign in to comment.