| 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/dll/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */
|
|---|
| 11 | /** @typedef {import("../../declarations/plugins/dll/DllReferencePlugin").DllReferencePluginOptionsContent} DllReferencePluginOptionsContent */
|
|---|
| 12 | /** @typedef {import("./DelegatedModule").DelegatedModuleData} DelegatedModuleData */
|
|---|
| 13 | /** @typedef {import("./DelegatedModule").DelegatedModuleSourceRequest} DelegatedModuleSourceRequest */
|
|---|
| 14 | /** @typedef {import("./DelegatedModule").DelegatedModuleType} DelegatedModuleType */
|
|---|
| 15 | /** @typedef {import("../NormalModuleFactory")} NormalModuleFactory */
|
|---|
| 16 | /** @typedef {import("../util/identifier").AssociatedObjectForCache} AssociatedObjectForCache */
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Defines the options type used by this module.
|
|---|
| 20 | * @typedef {object} Options
|
|---|
| 21 | * @property {DelegatedModuleSourceRequest} source source
|
|---|
| 22 | * @property {NonNullable<DllReferencePluginOptions["context"]>} context absolute context path to which lib ident is relative to
|
|---|
| 23 | * @property {DllReferencePluginOptionsContent} content content
|
|---|
| 24 | * @property {DllReferencePluginOptions["type"]} type type
|
|---|
| 25 | * @property {DllReferencePluginOptions["extensions"]} extensions extensions
|
|---|
| 26 | * @property {DllReferencePluginOptions["scope"]} scope scope
|
|---|
| 27 | * @property {AssociatedObjectForCache=} associatedObjectForCache object for caching
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | const PLUGIN_NAME = "DelegatedModuleFactoryPlugin";
|
|---|
| 31 |
|
|---|
| 32 | class DelegatedModuleFactoryPlugin {
|
|---|
| 33 | /**
|
|---|
| 34 | * Creates an instance of DelegatedModuleFactoryPlugin.
|
|---|
| 35 | * @param {Options} options options
|
|---|
| 36 | */
|
|---|
| 37 | constructor(options) {
|
|---|
| 38 | this.options = options;
|
|---|
| 39 | options.type = options.type || "require";
|
|---|
| 40 | options.extensions = options.extensions || ["", ".js", ".json", ".wasm"];
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 45 | * @param {NormalModuleFactory} normalModuleFactory the normal module factory
|
|---|
| 46 | * @returns {void}
|
|---|
| 47 | */
|
|---|
| 48 | apply(normalModuleFactory) {
|
|---|
| 49 | const scope = this.options.scope;
|
|---|
| 50 | if (scope) {
|
|---|
| 51 | normalModuleFactory.hooks.factorize.tapAsync(
|
|---|
| 52 | PLUGIN_NAME,
|
|---|
| 53 | (data, callback) => {
|
|---|
| 54 | const [dependency] = data.dependencies;
|
|---|
| 55 | const { request } = dependency;
|
|---|
| 56 | if (request && request.startsWith(`${scope}/`)) {
|
|---|
| 57 | const innerRequest = `.${request.slice(scope.length)}`;
|
|---|
| 58 | /** @type {undefined | DelegatedModuleData} */
|
|---|
| 59 | let resolved;
|
|---|
| 60 | if (innerRequest in this.options.content) {
|
|---|
| 61 | resolved = this.options.content[innerRequest];
|
|---|
| 62 | return callback(
|
|---|
| 63 | null,
|
|---|
| 64 | new DelegatedModule(
|
|---|
| 65 | this.options.source,
|
|---|
| 66 | resolved,
|
|---|
| 67 | /** @type {DelegatedModuleType} */
|
|---|
| 68 | (this.options.type),
|
|---|
| 69 | innerRequest,
|
|---|
| 70 | request
|
|---|
| 71 | )
|
|---|
| 72 | );
|
|---|
| 73 | }
|
|---|
| 74 | const extensions =
|
|---|
| 75 | /** @type {string[]} */
|
|---|
| 76 | (this.options.extensions);
|
|---|
| 77 | for (let i = 0; i < extensions.length; i++) {
|
|---|
| 78 | const extension = extensions[i];
|
|---|
| 79 | const requestPlusExt = innerRequest + extension;
|
|---|
| 80 | if (requestPlusExt in this.options.content) {
|
|---|
| 81 | resolved = this.options.content[requestPlusExt];
|
|---|
| 82 | return callback(
|
|---|
| 83 | null,
|
|---|
| 84 | new DelegatedModule(
|
|---|
| 85 | this.options.source,
|
|---|
| 86 | resolved,
|
|---|
| 87 | /** @type {DelegatedModuleType} */
|
|---|
| 88 | (this.options.type),
|
|---|
| 89 | requestPlusExt,
|
|---|
| 90 | request + extension
|
|---|
| 91 | )
|
|---|
| 92 | );
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | return callback();
|
|---|
| 97 | }
|
|---|
| 98 | );
|
|---|
| 99 | } else {
|
|---|
| 100 | normalModuleFactory.hooks.module.tap(PLUGIN_NAME, (module) => {
|
|---|
| 101 | const request = module.libIdent(this.options);
|
|---|
| 102 | if (request && request in this.options.content) {
|
|---|
| 103 | const resolved = this.options.content[request];
|
|---|
| 104 | return new DelegatedModule(
|
|---|
| 105 | this.options.source,
|
|---|
| 106 | resolved,
|
|---|
| 107 | /** @type {DelegatedModuleType} */
|
|---|
| 108 | (this.options.type),
|
|---|
| 109 | request,
|
|---|
| 110 | module
|
|---|
| 111 | );
|
|---|
| 112 | }
|
|---|
| 113 | return module;
|
|---|
| 114 | });
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | module.exports = DelegatedModuleFactoryPlugin;
|
|---|