1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const DelegatedModule = require("./DelegatedModule");
|
---|
9 |
|
---|
10 | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */
|
---|
11 | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsContent} DllReferencePluginOptionsContent */
|
---|
12 | /** @typedef {import("./DelegatedModule").Data} Data */
|
---|
13 | /** @typedef {import("./DelegatedModule").SourceRequest} SourceRequest */
|
---|
14 | /** @typedef {import("./DelegatedModule").Type} Type */
|
---|
15 | /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * @typedef {object} Options
|
---|
19 | * @property {SourceRequest} source source
|
---|
20 | * @property {NonNullable<DllReferencePluginOptions["context"]>} context absolute context path to which lib ident is relative to
|
---|
21 | * @property {DllReferencePluginOptionsContent} content content
|
---|
22 | * @property {DllReferencePluginOptions["type"]} type type
|
---|
23 | * @property {DllReferencePluginOptions["extensions"]} extensions extensions
|
---|
24 | * @property {DllReferencePluginOptions["scope"]} scope scope
|
---|
25 | * @property {object=} associatedObjectForCache object for caching
|
---|
26 | */
|
---|
27 |
|
---|
28 | class DelegatedModuleFactoryPlugin {
|
---|
29 | /**
|
---|
30 | * @param {Options} options options
|
---|
31 | */
|
---|
32 | constructor(options) {
|
---|
33 | this.options = options;
|
---|
34 | options.type = options.type || "require";
|
---|
35 | options.extensions = options.extensions || ["", ".js", ".json", ".wasm"];
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * @param {NormalModuleFactory} normalModuleFactory the normal module factory
|
---|
40 | * @returns {void}
|
---|
41 | */
|
---|
42 | apply(normalModuleFactory) {
|
---|
43 | const scope = this.options.scope;
|
---|
44 | if (scope) {
|
---|
45 | normalModuleFactory.hooks.factorize.tapAsync(
|
---|
46 | "DelegatedModuleFactoryPlugin",
|
---|
47 | (data, callback) => {
|
---|
48 | const [dependency] = data.dependencies;
|
---|
49 | const { request } = dependency;
|
---|
50 | if (request && request.startsWith(`${scope}/`)) {
|
---|
51 | const innerRequest = `.${request.slice(scope.length)}`;
|
---|
52 | let resolved;
|
---|
53 | if (innerRequest in this.options.content) {
|
---|
54 | resolved = this.options.content[innerRequest];
|
---|
55 | return callback(
|
---|
56 | null,
|
---|
57 | new DelegatedModule(
|
---|
58 | this.options.source,
|
---|
59 | resolved,
|
---|
60 | /** @type {Type} */ (this.options.type),
|
---|
61 | innerRequest,
|
---|
62 | request
|
---|
63 | )
|
---|
64 | );
|
---|
65 | }
|
---|
66 | const extensions =
|
---|
67 | /** @type {string[]} */
|
---|
68 | (this.options.extensions);
|
---|
69 | for (let i = 0; i < extensions.length; i++) {
|
---|
70 | const extension = extensions[i];
|
---|
71 | const requestPlusExt = innerRequest + extension;
|
---|
72 | if (requestPlusExt in this.options.content) {
|
---|
73 | resolved = this.options.content[requestPlusExt];
|
---|
74 | return callback(
|
---|
75 | null,
|
---|
76 | new DelegatedModule(
|
---|
77 | this.options.source,
|
---|
78 | resolved,
|
---|
79 | /** @type {Type} */ (this.options.type),
|
---|
80 | requestPlusExt,
|
---|
81 | request + extension
|
---|
82 | )
|
---|
83 | );
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | return callback();
|
---|
88 | }
|
---|
89 | );
|
---|
90 | } else {
|
---|
91 | normalModuleFactory.hooks.module.tap(
|
---|
92 | "DelegatedModuleFactoryPlugin",
|
---|
93 | module => {
|
---|
94 | const request = module.libIdent(this.options);
|
---|
95 | if (request && request in this.options.content) {
|
---|
96 | const resolved = this.options.content[request];
|
---|
97 | return new DelegatedModule(
|
---|
98 | this.options.source,
|
---|
99 | resolved,
|
---|
100 | /** @type {Type} */ (this.options.type),
|
---|
101 | request,
|
---|
102 | module
|
---|
103 | );
|
---|
104 | }
|
---|
105 | return module;
|
---|
106 | }
|
---|
107 | );
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | module.exports = DelegatedModuleFactoryPlugin;
|
---|