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.DedupeModuleResolvePlugin = void 0;
|
---|
11 | const webpack_diagnostics_1 = require("../../utils/webpack-diagnostics");
|
---|
12 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
13 | function getResourceData(resolveData) {
|
---|
14 | const { descriptionFileData, relativePath } = resolveData.createData.resourceResolveData;
|
---|
15 | return {
|
---|
16 | packageName: descriptionFileData === null || descriptionFileData === void 0 ? void 0 : descriptionFileData.name,
|
---|
17 | packageVersion: descriptionFileData === null || descriptionFileData === void 0 ? void 0 : descriptionFileData.version,
|
---|
18 | relativePath,
|
---|
19 | resource: resolveData.createData.resource,
|
---|
20 | };
|
---|
21 | }
|
---|
22 | /**
|
---|
23 | * DedupeModuleResolvePlugin is a webpack plugin which dedupes modules with the same name and versions
|
---|
24 | * that are laid out in different parts of the node_modules tree.
|
---|
25 | *
|
---|
26 | * This is needed because Webpack relies on package managers to hoist modules and doesn't have any deduping logic.
|
---|
27 | *
|
---|
28 | * This is similar to how Webpack's 'NormalModuleReplacementPlugin' works
|
---|
29 | * @see https://github.com/webpack/webpack/blob/4a1f068828c2ab47537d8be30d542cd3a1076db4/lib/NormalModuleReplacementPlugin.js#L9
|
---|
30 | */
|
---|
31 | class DedupeModuleResolvePlugin {
|
---|
32 | constructor(options) {
|
---|
33 | this.options = options;
|
---|
34 | this.modules = new Map();
|
---|
35 | }
|
---|
36 | apply(compiler) {
|
---|
37 | compiler.hooks.compilation.tap('DedupeModuleResolvePlugin', (compilation, { normalModuleFactory }) => {
|
---|
38 | normalModuleFactory.hooks.afterResolve.tap('DedupeModuleResolvePlugin', (result) => {
|
---|
39 | var _a;
|
---|
40 | if (!result) {
|
---|
41 | return;
|
---|
42 | }
|
---|
43 | const { packageName, packageVersion, relativePath, resource } = getResourceData(result);
|
---|
44 | // Empty name or versions are no valid primary entrypoints of a library
|
---|
45 | if (!packageName || !packageVersion) {
|
---|
46 | return;
|
---|
47 | }
|
---|
48 | const moduleId = packageName + '@' + packageVersion + ':' + relativePath;
|
---|
49 | const prevResolvedModule = this.modules.get(moduleId);
|
---|
50 | if (!prevResolvedModule) {
|
---|
51 | // This is the first time we visit this module.
|
---|
52 | this.modules.set(moduleId, {
|
---|
53 | resource,
|
---|
54 | request: result.request,
|
---|
55 | });
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | const { resource: prevResource, request: prevRequest } = prevResolvedModule;
|
---|
59 | if (resource === prevResource) {
|
---|
60 | // No deduping needed.
|
---|
61 | // Current path and previously resolved path are the same.
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.verbose) {
|
---|
65 | webpack_diagnostics_1.addWarning(compilation, `[DedupeModuleResolvePlugin]: ${resource} -> ${prevResource}`);
|
---|
66 | }
|
---|
67 | // Alter current request with previously resolved module.
|
---|
68 | const createData = result.createData;
|
---|
69 | createData.resource = prevResource;
|
---|
70 | createData.userRequest = prevRequest;
|
---|
71 | });
|
---|
72 | });
|
---|
73 | }
|
---|
74 | }
|
---|
75 | exports.DedupeModuleResolvePlugin = DedupeModuleResolvePlugin;
|
---|