2015-05-13 06:17:06 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-02-23 01:17:40 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const DelegatedModule = require("./DelegatedModule");
|
2015-05-13 06:17:06 +08:00
|
|
|
|
2015-05-18 05:03:21 +08:00
|
|
|
// options.source
|
|
|
|
// options.type
|
|
|
|
// options.context
|
|
|
|
// options.scope
|
|
|
|
// options.content
|
2019-01-19 19:40:00 +08:00
|
|
|
// options.associatedObjectForCache
|
2017-02-23 01:17:40 +08:00
|
|
|
class DelegatedModuleFactoryPlugin {
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options;
|
|
|
|
options.type = options.type || "require";
|
2020-09-02 20:41:04 +08:00
|
|
|
options.extensions = options.extensions || ["", ".js", ".json", ".wasm"];
|
2017-02-23 01:17:40 +08:00
|
|
|
}
|
2015-05-13 06:17:06 +08:00
|
|
|
|
2017-02-23 01:17:40 +08:00
|
|
|
apply(normalModuleFactory) {
|
|
|
|
const scope = this.options.scope;
|
2018-02-25 09:00:20 +08:00
|
|
|
if (scope) {
|
2019-01-05 02:17:37 +08:00
|
|
|
normalModuleFactory.hooks.factorize.tapAsync(
|
2018-02-25 09:00:20 +08:00
|
|
|
"DelegatedModuleFactoryPlugin",
|
2019-01-05 02:17:37 +08:00
|
|
|
(data, callback) => {
|
2020-01-09 01:56:54 +08:00
|
|
|
const [dependency] = data.dependencies;
|
2020-01-09 02:03:04 +08:00
|
|
|
const { request } = dependency;
|
2020-01-09 02:04:41 +08:00
|
|
|
if (request && request.startsWith(`${scope}/`)) {
|
2022-03-14 05:54:18 +08:00
|
|
|
const innerRequest = "." + request.slice(scope.length);
|
2018-02-25 09:00:20 +08:00
|
|
|
let resolved;
|
|
|
|
if (innerRequest in this.options.content) {
|
|
|
|
resolved = this.options.content[innerRequest];
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
new DelegatedModule(
|
|
|
|
this.options.source,
|
|
|
|
resolved,
|
|
|
|
this.options.type,
|
|
|
|
innerRequest,
|
|
|
|
request
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (let i = 0; i < this.options.extensions.length; i++) {
|
|
|
|
const extension = this.options.extensions[i];
|
|
|
|
const requestPlusExt = innerRequest + extension;
|
|
|
|
if (requestPlusExt in this.options.content) {
|
|
|
|
resolved = this.options.content[requestPlusExt];
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
new DelegatedModule(
|
|
|
|
this.options.source,
|
|
|
|
resolved,
|
|
|
|
this.options.type,
|
|
|
|
requestPlusExt,
|
|
|
|
request + extension
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-05-18 05:03:21 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-05 02:17:37 +08:00
|
|
|
return callback();
|
2017-02-23 15:43:44 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-02-23 01:17:40 +08:00
|
|
|
} else {
|
2018-02-25 09:00:20 +08:00
|
|
|
normalModuleFactory.hooks.module.tap(
|
|
|
|
"DelegatedModuleFactoryPlugin",
|
|
|
|
module => {
|
2018-07-20 22:24:35 +08:00
|
|
|
const request = module.libIdent(this.options);
|
|
|
|
if (request) {
|
2019-06-15 19:26:04 +08:00
|
|
|
if (request in this.options.content) {
|
2018-02-25 09:00:20 +08:00
|
|
|
const resolved = this.options.content[request];
|
|
|
|
return new DelegatedModule(
|
|
|
|
this.options.source,
|
|
|
|
resolved,
|
|
|
|
this.options.type,
|
|
|
|
request,
|
|
|
|
module
|
|
|
|
);
|
|
|
|
}
|
2017-02-23 01:17:40 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
return module;
|
2015-05-18 05:03:21 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-02-23 01:17:40 +08:00
|
|
|
}
|
2015-05-18 05:03:21 +08:00
|
|
|
}
|
2017-02-23 01:17:40 +08:00
|
|
|
}
|
|
|
|
module.exports = DelegatedModuleFactoryPlugin;
|