[6a3a178] | 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 | // options.source
|
---|
| 11 | // options.type
|
---|
| 12 | // options.context
|
---|
| 13 | // options.scope
|
---|
| 14 | // options.content
|
---|
| 15 | // options.associatedObjectForCache
|
---|
| 16 | class DelegatedModuleFactoryPlugin {
|
---|
| 17 | constructor(options) {
|
---|
| 18 | this.options = options;
|
---|
| 19 | options.type = options.type || "require";
|
---|
| 20 | options.extensions = options.extensions || ["", ".js", ".json", ".wasm"];
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | apply(normalModuleFactory) {
|
---|
| 24 | const scope = this.options.scope;
|
---|
| 25 | if (scope) {
|
---|
| 26 | normalModuleFactory.hooks.factorize.tapAsync(
|
---|
| 27 | "DelegatedModuleFactoryPlugin",
|
---|
| 28 | (data, callback) => {
|
---|
| 29 | const [dependency] = data.dependencies;
|
---|
| 30 | const { request } = dependency;
|
---|
| 31 | if (request && request.startsWith(`${scope}/`)) {
|
---|
| 32 | const innerRequest = "." + request.substr(scope.length);
|
---|
| 33 | let resolved;
|
---|
| 34 | if (innerRequest in this.options.content) {
|
---|
| 35 | resolved = this.options.content[innerRequest];
|
---|
| 36 | return callback(
|
---|
| 37 | null,
|
---|
| 38 | new DelegatedModule(
|
---|
| 39 | this.options.source,
|
---|
| 40 | resolved,
|
---|
| 41 | this.options.type,
|
---|
| 42 | innerRequest,
|
---|
| 43 | request
|
---|
| 44 | )
|
---|
| 45 | );
|
---|
| 46 | }
|
---|
| 47 | for (let i = 0; i < this.options.extensions.length; i++) {
|
---|
| 48 | const extension = this.options.extensions[i];
|
---|
| 49 | const requestPlusExt = innerRequest + extension;
|
---|
| 50 | if (requestPlusExt in this.options.content) {
|
---|
| 51 | resolved = this.options.content[requestPlusExt];
|
---|
| 52 | return callback(
|
---|
| 53 | null,
|
---|
| 54 | new DelegatedModule(
|
---|
| 55 | this.options.source,
|
---|
| 56 | resolved,
|
---|
| 57 | this.options.type,
|
---|
| 58 | requestPlusExt,
|
---|
| 59 | request + extension
|
---|
| 60 | )
|
---|
| 61 | );
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | return callback();
|
---|
| 66 | }
|
---|
| 67 | );
|
---|
| 68 | } else {
|
---|
| 69 | normalModuleFactory.hooks.module.tap(
|
---|
| 70 | "DelegatedModuleFactoryPlugin",
|
---|
| 71 | module => {
|
---|
| 72 | const request = module.libIdent(this.options);
|
---|
| 73 | if (request) {
|
---|
| 74 | if (request in this.options.content) {
|
---|
| 75 | const resolved = this.options.content[request];
|
---|
| 76 | return new DelegatedModule(
|
---|
| 77 | this.options.source,
|
---|
| 78 | resolved,
|
---|
| 79 | this.options.type,
|
---|
| 80 | request,
|
---|
| 81 | module
|
---|
| 82 | );
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | return module;
|
---|
| 86 | }
|
---|
| 87 | );
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | module.exports = DelegatedModuleFactoryPlugin;
|
---|