Skip to content

Commit

Permalink
feat(@ngtools/webpack): allow passing in the right ContextElementDepe…
Browse files Browse the repository at this point in the history
…ndency class

This should allow multiple webpack versions in the workspace dependencies with no weird errors.

Fix angular#6417 for good.
  • Loading branch information
filipesilva committed Jun 11, 2018
1 parent 9ebe08c commit 4b6619e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/ngtools/webpack/README.md
Expand Up @@ -42,6 +42,7 @@ The loader works with webpack plugin to compile your TypeScript. It's important
* `skipCodeGeneration`. Optional, defaults to false. Disable code generation and do not refactor the code to bootstrap. This replaces `templateUrl: "string"` with `template: require("string")` (and similar for styles) to allow for webpack to properly link the resources.
* `sourceMap`. Optional. Include sourcemaps.
* `compilerOptions`. Optional. Override options in `tsconfig.json`.
* `contextElementDependencyConstructor`. Optional. Set to `require('webpack/lib/dependencies/ContextElementDependency')` if you are having `No module factory available for dependency type: ContextElementDependency` errors.

## Features
The benefits and ability of using [`@ngtools/webpack`](https://www.npmjs.com/~ngtools) standalone from the Angular CLI as presented in [Stephen Fluin's Angular CLI talk](https://youtu.be/uBRK6cTr4Vk?t=6m45s) at Angular Connect 2016:
Expand Down
23 changes: 20 additions & 3 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Expand Up @@ -54,10 +54,13 @@ import {
VirtualWatchFileSystemDecorator,
} from './virtual_file_system_decorator';


const ContextElementDependency = require('webpack/lib/dependencies/ContextElementDependency');
const treeKill = require('tree-kill');

export interface ContextElementDependency {}

export interface ContextElementDependencyConstructor {
new(modulePath: string, name: string): ContextElementDependency;
}

/**
* Option Constants
Expand Down Expand Up @@ -86,6 +89,10 @@ export interface AngularCompilerPluginOptions {
// added to the list of lazy routes
additionalLazyModules?: { [module: string]: string };

// The ContextElementDependency of correct Webpack compilation.
// This is needed when there are multiple Webpack installs.
contextElementDependencyConstructor?: ContextElementDependencyConstructor;

// Use tsconfig to include path globs.
compilerOptions?: ts.CompilerOptions;

Expand Down Expand Up @@ -128,6 +135,7 @@ export class AngularCompilerPlugin {
private _normalizedLocale: string | null;
private _warnings: (string | Error)[] = [];
private _errors: (string | Error)[] = [];
private _contextElementDependencyConstructor: ContextElementDependencyConstructor;

// TypeChecker process.
private _forkTypeChecker = true;
Expand Down Expand Up @@ -267,6 +275,15 @@ export class AngularCompilerPlugin {
this._platformTransformers = options.platformTransformers;
}

// Default ContextElementDependency to the one we can import from here.
// Failing to use the right ContextElementDependency will throw the error below:
// "No module factory available for dependency type: ContextElementDependency"
// Hoisting together with peer dependencies can make it so the imported
// ContextElementDependency does not come from the same Webpack instance that is used
// in the compilation. In that case, we can pass the right one as an option to the plugin.
this._contextElementDependencyConstructor = options.contextElementDependencyConstructor
|| require('webpack/lib/dependencies/ContextElementDependency');

// Create the webpack compiler host.
const webpackCompilerHost = new WebpackCompilerHost(
this._compilerOptions,
Expand Down Expand Up @@ -633,7 +650,7 @@ export class AngularCompilerPlugin {
if (modulePath !== null) {
const name = importPath.replace(/(\.ngfactory)?\.(js|ts)$/, '');

return new ContextElementDependency(modulePath, name);
return new this._contextElementDependencyConstructor(modulePath, name);
} else {
return null;
}
Expand Down

0 comments on commit 4b6619e

Please sign in to comment.