-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathcommon-js-usage-warn-plugin.ts
More file actions
119 lines (100 loc) · 4.76 KB
/
Copy pathcommon-js-usage-warn-plugin.ts
File metadata and controls
119 lines (100 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { isAbsolute } from 'path';
import { Compiler, compilation } from 'webpack';
// Webpack doesn't export these so the deep imports can potentially break.
const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency');
const AMDDefineDependency = require('webpack/lib/dependencies/AMDDefineDependency');
// The below is extended because there are not in the typings
interface WebpackModule extends compilation.Module {
name?: string;
rawRequest?: string;
dependencies: unknown[];
issuer: WebpackModule | null;
userRequest?: string;
}
export interface CommonJsUsageWarnPluginOptions {
/** A list of CommonJS packages that are allowed to be used without a warning. */
allowedDepedencies?: string[];
}
export class CommonJsUsageWarnPlugin {
private shownWarnings = new Set<string>();
// Allow the below depedency for HMR
// tslint:disable-next-line: max-line-length
// https://github.com/angular/angular-cli/blob/1e258317b1f6ec1e957ee3559cc3b28ba602f3ba/packages/angular_devkit/build_angular/src/dev-server/index.ts#L605-L638
private allowedDepedencies = new Set<string>(['webpack/hot/dev-server']);
constructor(private options: CommonJsUsageWarnPluginOptions = {}) {
this.options.allowedDepedencies?.forEach(d => this.allowedDepedencies.add(d));
}
apply(compiler: Compiler) {
compiler.hooks.compilation.tap('CommonJsUsageWarnPlugin', compilation => {
compilation.hooks.finishModules.tap('CommonJsUsageWarnPlugin', modules => {
for (const { dependencies, rawRequest, issuer } of modules as unknown as WebpackModule[]) {
if (
!rawRequest ||
rawRequest.startsWith('.') ||
isAbsolute(rawRequest)
) {
// Skip if module is absolute or relative.
continue;
}
if (
this.allowedDepedencies.has(rawRequest) ||
this.allowedDepedencies.has(this.rawRequestToPackageName(rawRequest))
) {
// Skip as this module is allowed even if it's a CommonJS.
continue;
}
if (this.hasCommonJsDependencies(dependencies)) {
// Dependency is CommonsJS or AMD.
// Check if it's parent issuer is also a CommonJS dependency.
// In case it is skip as an warning will be show for the parent CommonJS dependency.
const parentDependencies = issuer?.issuer?.dependencies;
if (parentDependencies && this.hasCommonJsDependencies(parentDependencies)) {
continue;
}
// Find the main issuer (entry-point).
let mainIssuer = issuer;
while (mainIssuer?.issuer) {
mainIssuer = mainIssuer.issuer;
}
// Only show warnings for modules from main entrypoint.
// And if the issuer request is not from 'webpack-dev-server', as 'webpack-dev-server'
// will require CommonJS libraries for live reloading such as 'sockjs-node'.
if (mainIssuer?.name === 'main' && !issuer?.userRequest?.includes('webpack-dev-server')) {
let warning = `${issuer?.userRequest} depends on '${rawRequest}'.`;
if (rawRequest.startsWith('@angular/common/locales')) {
warning += `\nWhen using the 'localize' option this import is not needed. ` +
`Did you mean to import '${rawRequest.replace(/locales(\/extra)?\//, 'locales/global/')}'?\n` +
'For more info see: https://angular.io/guide/i18n#import-global-variants-of-the-locale-data';
} else {
warning += ' CommonJS or AMD dependencies can cause optimization bailouts.\n' +
'For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies';
}
// Avoid showing the same warning multiple times when in 'watch' mode.
if (!this.shownWarnings.has(warning)) {
compilation.warnings.push(warning);
this.shownWarnings.add(warning);
}
}
}
}
});
});
}
private hasCommonJsDependencies(dependencies: unknown[]): boolean {
return dependencies.some(d => d instanceof CommonJsRequireDependency || d instanceof AMDDefineDependency);
}
private rawRequestToPackageName(rawRequest: string): string {
return rawRequest.startsWith('@')
// Scoped request ex: @angular/common/locale/en -> @angular/common
? rawRequest.split('/', 2).join('/')
// Non-scoped request ex: lodash/isEmpty -> lodash
: rawRequest.split('/', 1)[0];
}
}