Skip to content

Webpack peer dependency fixes #11152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,774 changes: 2,392 additions & 2,382 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"@types/request": "^2.47.0",
"@types/semver": "^5.3.30",
"@types/source-map": "0.5.2",
"@types/webpack": "^4.1.3",
"@types/webpack": "^4.4.0",
"@types/webpack-dev-server": "^2.9.4",
"@types/webpack-sources": "^0.1.4",
"ajv": "~6.4.0",
Expand Down Expand Up @@ -137,8 +137,8 @@
"semver": "^5.3.0",
"semver-intersect": "^1.1.2",
"source-map": "^0.5.6",
"source-map-support": "^0.5.0",
"source-map-loader": "^0.2.3",
"source-map-support": "^0.5.0",
"stats-webpack-plugin": "^0.6.2",
"style-loader": "^0.21.0",
"stylus": "^0.54.5",
Expand All @@ -153,7 +153,7 @@
"typescript": "~2.7.2",
"uglifyjs-webpack-plugin": "^1.2.5",
"url-loader": "^1.0.1",
"webpack": "~4.10.2",
"webpack": "~4.11.1",
"webpack-dev-middleware": "^3.1.3",
"webpack-dev-server": "^3.1.4",
"webpack-merge": "^4.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"tree-kill": "^1.2.0",
"uglifyjs-webpack-plugin": "^1.2.5",
"url-loader": "^1.0.1",
"webpack": "~4.10.2",
"webpack": "~4.11.1",
"webpack-dev-middleware": "^3.1.3",
"webpack-dev-server": "^3.1.4",
"webpack-merge": "^4.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
}

// set base rules to derive final rules from
const baseRules: webpack.NewUseRule[] = [
const baseRules: webpack.Rule[] = [
{ test: /\.css$/, use: [] },
{
test: /\.scss$|\.sass$/, use: [{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function _createAotPlugin(
additionalLazyModules,
nameLazyFiles: buildOptions.namedChunks,
forkTypeChecker: buildOptions.forkTypeChecker,
contextElementDependencyConstructor: require('webpack/lib/dependencies/ContextElementDependency'),
...options,
host,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"rxjs": "^6.0.0"
},
"peerDependencies": {
"webpack": "~4.6.0",
"webpack": "^4.6.0",
"webpack-dev-server": "^3.1.4"
}
}
1 change: 1 addition & 0 deletions packages/ngtools/webpack/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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