Skip to content

Commit 48af93b

Browse files
author
Angular Builds
committed
ea11c5549 feat(@angular-devkit/build-angular): show warnings when depending on CommonJS.
1 parent e2d6392 commit 48af93b

16 files changed

+136
-35
lines changed

‎package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
22
"name": "@angular-devkit/build-angular",
3-
"version": "0.901.0-next.4+4.4f50aa0",
3+
"version": "0.901.0-next.4+7.ea11c55",
44
"description": "Angular Webpack Build Facade",
55
"experimental": true,
66
"main": "src/index.js",
77
"typings": "src/index.d.ts",
88
"builders": "builders.json",
99
"dependencies": {
10-
"@angular-devkit/architect": "github:angular/angular-devkit-architect-builds#4f50aa0e4",
11-
"@angular-devkit/build-optimizer": "github:angular/angular-devkit-build-optimizer-builds#4f50aa0e4",
12-
"@angular-devkit/build-webpack": "github:angular/angular-devkit-build-webpack-builds#4f50aa0e4",
13-
"@angular-devkit/core": "github:angular/angular-devkit-core-builds#4f50aa0e4",
10+
"@angular-devkit/architect": "github:angular/angular-devkit-architect-builds#ea11c5549",
11+
"@angular-devkit/build-optimizer": "github:angular/angular-devkit-build-optimizer-builds#ea11c5549",
12+
"@angular-devkit/build-webpack": "github:angular/angular-devkit-build-webpack-builds#ea11c5549",
13+
"@angular-devkit/core": "github:angular/angular-devkit-core-builds#ea11c5549",
1414
"@babel/core": "7.8.7",
1515
"@babel/generator": "7.8.8",
1616
"@babel/preset-env": "7.8.7",
1717
"@babel/template": "7.8.6",
1818
"@jsdevtools/coverage-istanbul-loader": "3.0.3",
19-
"@ngtools/webpack": "github:angular/ngtools-webpack-builds#4f50aa0e4",
19+
"@ngtools/webpack": "github:angular/ngtools-webpack-builds#ea11c5549",
2020
"ajv": "6.12.0",
2121
"autoprefixer": "9.7.4",
2222
"babel-loader": "8.0.6",

‎src/angular-cli-files/models/build-options.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export interface BuildOptions {
7474
rebaseRootRelativeCssUrls?: boolean;
7575
esVersionInFileName?: boolean;
7676
experimentalRollupPass?: boolean;
77+
allowedCommonJsDependencies?: string[];
7778
}
7879
export interface WebpackTestOptions extends BuildOptions {
7980
codeCoverage?: boolean;

‎src/angular-cli-files/models/webpack-configs/browser.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
88
* found in the LICENSE file at https://angular.io/license
99
*/
1010
const license_webpack_plugin_1 = require("license-webpack-plugin");
11+
const webpack_1 = require("../../plugins/webpack");
1112
const utils_1 = require("./utils");
1213
const SubresourceIntegrityPlugin = require('webpack-subresource-integrity');
1314
function getBrowserConfig(wco) {
1415
const { buildOptions } = wco;
15-
const { crossOrigin = 'none', subresourceIntegrity, evalSourceMap, extractLicenses, vendorChunk, commonChunk, styles, } = buildOptions;
16+
const { crossOrigin = 'none', subresourceIntegrity, evalSourceMap, extractLicenses, vendorChunk, commonChunk, styles, allowedCommonJsDependencies, optimization, } = buildOptions;
1617
const extraPlugins = [];
1718
let isEval = false;
18-
const { styles: stylesOptimization, scripts: scriptsOptimization } = buildOptions.optimization;
19+
const { styles: stylesOptimization, scripts: scriptsOptimization } = optimization;
1920
const { styles: stylesSourceMap, scripts: scriptsSourceMap, hidden: hiddenSourceMap, } = buildOptions.sourceMap;
2021
// See https://webpack.js.org/configuration/devtool/ for sourcemap types.
2122
if ((stylesSourceMap || scriptsSourceMap) &&
@@ -95,7 +96,12 @@ function getBrowserConfig(wco) {
9596
},
9697
},
9798
},
98-
plugins: extraPlugins,
99+
plugins: [
100+
new webpack_1.CommonJsUsageWarnPlugin({
101+
allowedDepedencies: allowedCommonJsDependencies,
102+
}),
103+
...extraPlugins,
104+
],
99105
node: false,
100106
};
101107
}

‎src/angular-cli-files/models/webpack-configs/common.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ const webpack_sources_1 = require("webpack-sources");
1818
const utils_1 = require("../../../utils");
1919
const cache_path_1 = require("../../../utils/cache-path");
2020
const environment_options_1 = require("../../../utils/environment-options");
21-
const bundle_budget_1 = require("../../plugins/bundle-budget");
22-
const named_chunks_plugin_1 = require("../../plugins/named-chunks-plugin");
23-
const optimize_css_webpack_plugin_1 = require("../../plugins/optimize-css-webpack-plugin");
24-
const scripts_webpack_plugin_1 = require("../../plugins/scripts-webpack-plugin");
2521
const webpack_2 = require("../../plugins/webpack");
2622
const find_up_1 = require("../../utilities/find-up");
2723
const utils_2 = require("./utils");
@@ -172,7 +168,7 @@ function getCommonConfig(wco) {
172168
// Lazy scripts don't get a hash, otherwise they can't be loaded by name.
173169
const hash = script.inject ? hashFormat.script : '';
174170
const bundleName = script.bundleName;
175-
extraPlugins.push(new scripts_webpack_plugin_1.ScriptsWebpackPlugin({
171+
extraPlugins.push(new webpack_2.ScriptsWebpackPlugin({
176172
name: bundleName,
177173
sourceMap: scriptsSourceMap,
178174
filename: `${path.basename(bundleName)}${hash}.js`,
@@ -226,7 +222,12 @@ function getCommonConfig(wco) {
226222
})());
227223
}
228224
if (buildOptions.namedChunks) {
229-
extraPlugins.push(new named_chunks_plugin_1.NamedLazyChunksPlugin());
225+
extraPlugins.push(new webpack_2.NamedLazyChunksPlugin());
226+
}
227+
if (!differentialLoadingMode) {
228+
// Budgets are computed after differential builds, not via a plugin.
229+
// https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/build_angular/src/browser/index.ts
230+
extraPlugins.push(new webpack_2.BundleBudgetPlugin({ budgets: buildOptions.budgets }));
230231
}
231232
let sourceMapUseRule;
232233
if ((scriptsSourceMap || stylesSourceMap) && vendorSourceMap) {
@@ -268,7 +269,7 @@ function getCommonConfig(wco) {
268269
catch (_a) { }
269270
const extraMinimizers = [];
270271
if (stylesOptimization) {
271-
extraMinimizers.push(new optimize_css_webpack_plugin_1.OptimizeCssWebpackPlugin({
272+
extraMinimizers.push(new webpack_2.OptimizeCssWebpackPlugin({
272273
sourceMap: stylesSourceMap,
273274
// component styles retain their original file name
274275
test: file => /\.(?:css|scss|sass|less|styl)$/.test(file),
@@ -438,13 +439,7 @@ function getCommonConfig(wco) {
438439
minimizer: [
439440
new webpack_1.HashedModuleIdsPlugin(),
440441
...extraMinimizers,
441-
].concat(differentialLoadingMode ? [
442-
// Budgets are computed after differential builds, not via a plugin.
443-
// https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/build_angular/src/browser/index.ts
444-
] : [
445-
// Non differential builds should be computed here, as a plugin.
446-
new bundle_budget_1.BundleBudgetPlugin({ budgets: buildOptions.budgets }),
447-
]),
442+
],
448443
},
449444
plugins: [
450445
// Always replace the context for the System.import in angular/core to prevent warnings.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { Compiler } from 'webpack';
9+
export interface CommonJsUsageWarnPluginOptions {
10+
/** A list of CommonJS packages that are allowed to be used without a warning. */
11+
allowedDepedencies?: string[];
12+
}
13+
export declare class CommonJsUsageWarnPlugin {
14+
private options;
15+
private shownWarnings;
16+
constructor(options?: CommonJsUsageWarnPluginOptions);
17+
apply(compiler: Compiler): void;
18+
private hasCommonJsDependencies;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"use strict";
2+
/**
3+
* @license
4+
* Copyright Google Inc. All Rights Reserved.
5+
*
6+
* Use of this source code is governed by an MIT-style license that can be
7+
* found in the LICENSE file at https://angular.io/license
8+
*/
9+
Object.defineProperty(exports, "__esModule", { value: true });
10+
const path_1 = require("path");
11+
// Webpack doesn't export these so the deep imports can potentially break.
12+
const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency');
13+
const AMDDefineDependency = require('webpack/lib/dependencies/AMDDefineDependency');
14+
class CommonJsUsageWarnPlugin {
15+
constructor(options = {}) {
16+
this.options = options;
17+
this.shownWarnings = new Set();
18+
}
19+
apply(compiler) {
20+
compiler.hooks.compilation.tap('CommonJsUsageWarnPlugin', compilation => {
21+
compilation.hooks.finishModules.tap('CommonJsUsageWarnPlugin', modules => {
22+
var _a, _b, _c;
23+
for (const { dependencies, rawRequest, issuer } of modules) {
24+
if (!rawRequest ||
25+
rawRequest.startsWith('.') ||
26+
path_1.isAbsolute(rawRequest)) {
27+
// Skip if module is absolute or relative.
28+
continue;
29+
}
30+
if ((_a = this.options.allowedDepedencies) === null || _a === void 0 ? void 0 : _a.includes(rawRequest)) {
31+
// Skip as this module is allowed even if it's a CommonJS.
32+
continue;
33+
}
34+
if (this.hasCommonJsDependencies(dependencies)) {
35+
// Dependency is CommonsJS or AMD.
36+
// Check if it's parent issuer is also a CommonJS dependency.
37+
// In case it is skip as an warning will be show for the parent CommonJS dependency.
38+
if (this.hasCommonJsDependencies((_c = (_b = issuer === null || issuer === void 0 ? void 0 : issuer.issuer) === null || _b === void 0 ? void 0 : _b.dependencies) !== null && _c !== void 0 ? _c : [])) {
39+
continue;
40+
}
41+
// Find the main issuer (entry-point).
42+
let mainIssuer = issuer;
43+
while (mainIssuer === null || mainIssuer === void 0 ? void 0 : mainIssuer.issuer) {
44+
mainIssuer = mainIssuer.issuer;
45+
}
46+
// Only show warnings for modules from main entrypoint.
47+
if ((mainIssuer === null || mainIssuer === void 0 ? void 0 : mainIssuer.name) === 'main') {
48+
const warning = `${issuer === null || issuer === void 0 ? void 0 : issuer.userRequest} depends on ${rawRequest}. CommonJS or AMD dependencies can cause optimization bailouts.`;
49+
// Avoid showing the same warning multiple times when in 'watch' mode.
50+
if (!this.shownWarnings.has(warning)) {
51+
compilation.warnings.push(warning);
52+
this.shownWarnings.add(warning);
53+
}
54+
}
55+
}
56+
}
57+
});
58+
});
59+
}
60+
hasCommonJsDependencies(dependencies) {
61+
return dependencies.some(d => d instanceof CommonJsRequireDependency || d instanceof AMDDefineDependency);
62+
}
63+
}
64+
exports.CommonJsUsageWarnPlugin = CommonJsUsageWarnPlugin;

‎src/angular-cli-files/plugins/webpack.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export { BundleBudgetPlugin, BundleBudgetPluginOptions } from './bundle-budget';
1111
export { ScriptsWebpackPlugin, ScriptsWebpackPluginOptions } from './scripts-webpack-plugin';
1212
export { SuppressExtractedTextChunksWebpackPlugin } from './suppress-entry-chunks-webpack-plugin';
1313
export { RemoveHashPlugin, RemoveHashPluginOptions } from './remove-hash-plugin';
14-
export { NamedLazyChunksPlugin as NamedChunksPlugin } from './named-chunks-plugin';
14+
export { NamedLazyChunksPlugin } from './named-chunks-plugin';
15+
export { CommonJsUsageWarnPlugin } from './common-js-usage-warn-plugin';
1516
export { default as PostcssCliResources, PostcssCliResourcesOptions, } from './postcss-cli-resources';
1617
export declare const RawCssLoader: string;
1718
export declare const WebpackRollupLoader: string;

‎src/angular-cli-files/plugins/webpack.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ exports.SuppressExtractedTextChunksWebpackPlugin = suppress_entry_chunks_webpack
2121
var remove_hash_plugin_1 = require("./remove-hash-plugin");
2222
exports.RemoveHashPlugin = remove_hash_plugin_1.RemoveHashPlugin;
2323
var named_chunks_plugin_1 = require("./named-chunks-plugin");
24-
exports.NamedChunksPlugin = named_chunks_plugin_1.NamedLazyChunksPlugin;
24+
exports.NamedLazyChunksPlugin = named_chunks_plugin_1.NamedLazyChunksPlugin;
25+
var common_js_usage_warn_plugin_1 = require("./common-js-usage-warn-plugin");
26+
exports.CommonJsUsageWarnPlugin = common_js_usage_warn_plugin_1.CommonJsUsageWarnPlugin;
2527
var postcss_cli_resources_1 = require("./postcss-cli-resources");
2628
exports.PostcssCliResources = postcss_cli_resources_1.default;
2729
const path_1 = require("path");

‎src/browser/schema.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* Browser target options
33
*/
44
export interface Schema {
5+
/**
6+
* A list of CommonJS packages that are allowed to be used without a built time warning.
7+
*/
8+
allowedCommonJsDependencies?: string[];
59
/**
610
* Build using Ahead of Time compilation.
711
*/
@@ -136,7 +140,8 @@ export interface Schema {
136140
*/
137141
polyfills?: string;
138142
/**
139-
* Do not use the real path when resolving modules.
143+
* Do not use the real path when resolving modules. If unset then will default to `true` if
144+
* NodeJS option --preserve-symlinks is set.
140145
*/
141146
preserveSymlinks?: boolean;
142147
/**

‎src/browser/schema.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@
248248
},
249249
"preserveSymlinks": {
250250
"type": "boolean",
251-
"description": "Do not use the real path when resolving modules.",
252-
"default": false
251+
"description": "Do not use the real path when resolving modules. If unset then will default to `true` if NodeJS option --preserve-symlinks is set."
253252
},
254253
"extractLicenses": {
255254
"type": "boolean",
@@ -380,6 +379,14 @@
380379
"type": "boolean",
381380
"description": "Concatenate modules with Rollup before bundling them with Webpack.",
382381
"default": false
382+
},
383+
"allowedCommonJsDependencies": {
384+
"description": "A list of CommonJS packages that are allowed to be used without a built time warning.",
385+
"type": "array",
386+
"items": {
387+
"type": "string"
388+
},
389+
"default": []
383390
}
384391
},
385392
"additionalProperties": false,

‎src/karma/schema.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export interface Schema {
5858
*/
5959
polyfills?: string;
6060
/**
61-
* Do not use the real path when resolving modules.
61+
* Do not use the real path when resolving modules. If unset then will default to `true` if
62+
* NodeJS option --preserve-symlinks is set.
6263
*/
6364
preserveSymlinks?: boolean;
6465
/**

‎src/karma/schema.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@
126126
},
127127
"preserveSymlinks": {
128128
"type": "boolean",
129-
"description": "Do not use the real path when resolving modules.",
130-
"default": false
129+
"description": "Do not use the real path when resolving modules. If unset then will default to `true` if NodeJS option --preserve-symlinks is set."
131130
},
132131
"browsers": {
133132
"type": "string",

‎src/server/schema.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ export interface Schema {
9292
*/
9393
poll?: number;
9494
/**
95-
* Do not use the real path when resolving modules.
95+
* Do not use the real path when resolving modules. If unset then will default to `true` if
96+
* NodeJS option --preserve-symlinks is set.
9697
*/
9798
preserveSymlinks?: boolean;
9899
/**

‎src/server/schema.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@
198198
},
199199
"preserveSymlinks": {
200200
"type": "boolean",
201-
"description": "Do not use the real path when resolving modules.",
202-
"default": false
201+
"description": "Do not use the real path when resolving modules. If unset then will default to `true` if NodeJS option --preserve-symlinks is set."
203202
},
204203
"extractLicenses": {
205204
"type": "boolean",

‎src/utils/normalize-builder-schema.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function normalizeBrowserSchema(host, root, projectRoot, sourceRoot, options) {
2222
fileReplacements: normalize_file_replacements_1.normalizeFileReplacements(options.fileReplacements || [], syncHost, root),
2323
optimization: normalize_optimization_1.normalizeOptimization(options.optimization),
2424
sourceMap: normalizedSourceMapOptions,
25+
preserveSymlinks: options.preserveSymlinks === undefined ? process.execArgv.includes('--preserve-symlinks') : options.preserveSymlinks,
2526
statsJson: options.statsJson || false,
2627
forkTypeChecker: options.forkTypeChecker || false,
2728
budgets: options.budgets || [],

‎uniqueId

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Mon Mar 23 2020 21:59:52 GMT+0000 (Coordinated Universal Time)
1+
Mon Mar 23 2020 22:04:59 GMT+0000 (Coordinated Universal Time)

0 commit comments

Comments
 (0)