1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC 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 | exports.CommonJsUsageWarnPlugin = void 0;
|
---|
11 | const path_1 = require("path");
|
---|
12 | const webpack_diagnostics_1 = require("../../utils/webpack-diagnostics");
|
---|
13 | // Webpack doesn't export these so the deep imports can potentially break.
|
---|
14 | const AMDDefineDependency = require('webpack/lib/dependencies/AMDDefineDependency');
|
---|
15 | const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency');
|
---|
16 | class CommonJsUsageWarnPlugin {
|
---|
17 | constructor(options = {}) {
|
---|
18 | this.options = options;
|
---|
19 | this.shownWarnings = new Set();
|
---|
20 | this.allowedDependencies = new Set(this.options.allowedDependencies);
|
---|
21 | }
|
---|
22 | apply(compiler) {
|
---|
23 | compiler.hooks.compilation.tap('CommonJsUsageWarnPlugin', (compilation) => {
|
---|
24 | compilation.hooks.finishModules.tap('CommonJsUsageWarnPlugin', (modules) => {
|
---|
25 | var _a, _b;
|
---|
26 | const mainEntry = compilation.entries.get('main');
|
---|
27 | if (!mainEntry) {
|
---|
28 | return;
|
---|
29 | }
|
---|
30 | const mainModules = new Set(mainEntry.dependencies.map((dep) => compilation.moduleGraph.getModule(dep)));
|
---|
31 | for (const module of modules) {
|
---|
32 | const { dependencies, rawRequest } = module;
|
---|
33 | if (!rawRequest ||
|
---|
34 | rawRequest.startsWith('.') ||
|
---|
35 | path_1.isAbsolute(rawRequest) ||
|
---|
36 | this.allowedDependencies.has(rawRequest) ||
|
---|
37 | this.allowedDependencies.has(this.rawRequestToPackageName(rawRequest)) ||
|
---|
38 | rawRequest.startsWith('@angular/common/locales/')) {
|
---|
39 | /**
|
---|
40 | * Skip when:
|
---|
41 | * - module is absolute or relative.
|
---|
42 | * - module is allowed even if it's a CommonJS.
|
---|
43 | * - module is a locale imported from '@angular/common'.
|
---|
44 | */
|
---|
45 | continue;
|
---|
46 | }
|
---|
47 | if (this.hasCommonJsDependencies(compilation, dependencies)) {
|
---|
48 | // Dependency is CommonsJS or AMD.
|
---|
49 | const issuer = getIssuer(compilation, module);
|
---|
50 | // Check if it's parent issuer is also a CommonJS dependency.
|
---|
51 | // In case it is skip as an warning will be show for the parent CommonJS dependency.
|
---|
52 | const parentDependencies = (_a = getIssuer(compilation, issuer)) === null || _a === void 0 ? void 0 : _a.dependencies;
|
---|
53 | if (parentDependencies &&
|
---|
54 | this.hasCommonJsDependencies(compilation, parentDependencies, true)) {
|
---|
55 | continue;
|
---|
56 | }
|
---|
57 | // Find the main issuer (entry-point).
|
---|
58 | let mainIssuer = issuer;
|
---|
59 | let nextIssuer = getIssuer(compilation, mainIssuer);
|
---|
60 | while (nextIssuer) {
|
---|
61 | mainIssuer = nextIssuer;
|
---|
62 | nextIssuer = getIssuer(compilation, mainIssuer);
|
---|
63 | }
|
---|
64 | // Only show warnings for modules from main entrypoint.
|
---|
65 | // And if the issuer request is not from 'webpack-dev-server', as 'webpack-dev-server'
|
---|
66 | // will require CommonJS libraries for live reloading such as 'sockjs-node'.
|
---|
67 | if (mainIssuer && mainModules.has(mainIssuer)) {
|
---|
68 | const warning = `${(_b = issuer) === null || _b === void 0 ? void 0 : _b.userRequest} depends on '${rawRequest}'. ` +
|
---|
69 | 'CommonJS or AMD dependencies can cause optimization bailouts.\n' +
|
---|
70 | 'For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies';
|
---|
71 | // Avoid showing the same warning multiple times when in 'watch' mode.
|
---|
72 | if (!this.shownWarnings.has(warning)) {
|
---|
73 | webpack_diagnostics_1.addWarning(compilation, warning);
|
---|
74 | this.shownWarnings.add(warning);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | });
|
---|
80 | });
|
---|
81 | }
|
---|
82 | hasCommonJsDependencies(compilation, dependencies, checkParentModules = false) {
|
---|
83 | for (const dep of dependencies) {
|
---|
84 | if (dep instanceof CommonJsRequireDependency || dep instanceof AMDDefineDependency) {
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 | if (checkParentModules) {
|
---|
88 | const module = getWebpackModule(compilation, dep);
|
---|
89 | if (module && this.hasCommonJsDependencies(compilation, module.dependencies)) {
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 | rawRequestToPackageName(rawRequest) {
|
---|
97 | return rawRequest.startsWith('@')
|
---|
98 | ? // Scoped request ex: @angular/common/locale/en -> @angular/common
|
---|
99 | rawRequest.split('/', 2).join('/')
|
---|
100 | : // Non-scoped request ex: lodash/isEmpty -> lodash
|
---|
101 | rawRequest.split('/', 1)[0];
|
---|
102 | }
|
---|
103 | }
|
---|
104 | exports.CommonJsUsageWarnPlugin = CommonJsUsageWarnPlugin;
|
---|
105 | function getIssuer(compilation, module) {
|
---|
106 | if (!module) {
|
---|
107 | return null;
|
---|
108 | }
|
---|
109 | return compilation.moduleGraph.getIssuer(module);
|
---|
110 | }
|
---|
111 | function getWebpackModule(compilation, dependency) {
|
---|
112 | if (!dependency) {
|
---|
113 | return null;
|
---|
114 | }
|
---|
115 | return compilation.moduleGraph.getModule(dependency);
|
---|
116 | }
|
---|