2013-01-31 01:49:25 +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-01-03 01:00:08 +08:00
|
|
|
"use strict";
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2025-09-10 03:11:56 +08:00
|
|
|
const {
|
|
|
|
getExternalModuleNodeCommonjsInitFragment
|
|
|
|
} = require("./ExternalModule");
|
2023-04-01 01:56:32 +08:00
|
|
|
const {
|
|
|
|
JAVASCRIPT_MODULE_TYPE_AUTO,
|
|
|
|
JAVASCRIPT_MODULE_TYPE_DYNAMIC,
|
|
|
|
JAVASCRIPT_MODULE_TYPE_ESM
|
|
|
|
} = require("./ModuleTypeConstants");
|
2018-11-05 21:36:15 +08:00
|
|
|
const RuntimeGlobals = require("./RuntimeGlobals");
|
2019-08-29 21:28:19 +08:00
|
|
|
const WebpackError = require("./WebpackError");
|
2018-07-30 23:08:51 +08:00
|
|
|
const ConstDependency = require("./dependencies/ConstDependency");
|
2020-12-22 21:51:09 +08:00
|
|
|
const BasicEvaluatedExpression = require("./javascript/BasicEvaluatedExpression");
|
2023-06-03 08:58:03 +08:00
|
|
|
const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
|
2019-10-22 15:27:52 +08:00
|
|
|
const {
|
2025-07-03 17:06:45 +08:00
|
|
|
evaluateToString,
|
|
|
|
toConstantDependency
|
2019-10-22 15:27:52 +08:00
|
|
|
} = require("./javascript/JavascriptParserHelpers");
|
2018-11-06 01:54:05 +08:00
|
|
|
const ChunkNameRuntimeModule = require("./runtime/ChunkNameRuntimeModule");
|
|
|
|
const GetFullHashRuntimeModule = require("./runtime/GetFullHashRuntimeModule");
|
2014-06-16 21:18:49 +08:00
|
|
|
|
2018-11-03 04:05:46 +08:00
|
|
|
/** @typedef {import("./Compiler")} Compiler */
|
2024-01-27 00:17:45 +08:00
|
|
|
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
|
2024-03-05 22:40:46 +08:00
|
|
|
/** @typedef {import("./Module").BuildInfo} BuildInfo */
|
2020-12-22 21:51:09 +08:00
|
|
|
/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
|
2024-01-26 22:47:14 +08:00
|
|
|
/** @typedef {import("./javascript/JavascriptParser").Range} Range */
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2023-06-03 07:13:44 +08:00
|
|
|
/**
|
2023-06-17 06:33:17 +08:00
|
|
|
* @returns {Record<string, {expr: string, req: string[] | null, type?: string, assign: boolean}>} replacements
|
2023-06-03 07:13:44 +08:00
|
|
|
*/
|
2025-08-26 02:23:34 +08:00
|
|
|
function getReplacements() {
|
2023-06-03 07:13:44 +08:00
|
|
|
return {
|
|
|
|
__webpack_require__: {
|
|
|
|
expr: RuntimeGlobals.require,
|
|
|
|
req: [RuntimeGlobals.require],
|
|
|
|
type: "function",
|
|
|
|
assign: false
|
|
|
|
},
|
|
|
|
__webpack_public_path__: {
|
|
|
|
expr: RuntimeGlobals.publicPath,
|
|
|
|
req: [RuntimeGlobals.publicPath],
|
|
|
|
type: "string",
|
|
|
|
assign: true
|
|
|
|
},
|
|
|
|
__webpack_base_uri__: {
|
|
|
|
expr: RuntimeGlobals.baseURI,
|
|
|
|
req: [RuntimeGlobals.baseURI],
|
|
|
|
type: "string",
|
|
|
|
assign: true
|
|
|
|
},
|
|
|
|
__webpack_modules__: {
|
|
|
|
expr: RuntimeGlobals.moduleFactories,
|
|
|
|
req: [RuntimeGlobals.moduleFactories],
|
|
|
|
type: "object",
|
|
|
|
assign: false
|
|
|
|
},
|
|
|
|
__webpack_chunk_load__: {
|
|
|
|
expr: RuntimeGlobals.ensureChunk,
|
|
|
|
req: [RuntimeGlobals.ensureChunk],
|
|
|
|
type: "function",
|
|
|
|
assign: true
|
|
|
|
},
|
|
|
|
__non_webpack_require__: {
|
2025-08-26 02:23:34 +08:00
|
|
|
expr: "require",
|
2023-06-03 08:58:03 +08:00
|
|
|
req: null,
|
2023-06-03 07:13:44 +08:00
|
|
|
type: undefined, // type is not known, depends on environment
|
|
|
|
assign: true
|
|
|
|
},
|
|
|
|
__webpack_nonce__: {
|
|
|
|
expr: RuntimeGlobals.scriptNonce,
|
|
|
|
req: [RuntimeGlobals.scriptNonce],
|
|
|
|
type: "string",
|
|
|
|
assign: true
|
|
|
|
},
|
|
|
|
__webpack_hash__: {
|
|
|
|
expr: `${RuntimeGlobals.getFullHash}()`,
|
|
|
|
req: [RuntimeGlobals.getFullHash],
|
|
|
|
type: "string",
|
|
|
|
assign: false
|
|
|
|
},
|
|
|
|
__webpack_chunkname__: {
|
|
|
|
expr: RuntimeGlobals.chunkName,
|
|
|
|
req: [RuntimeGlobals.chunkName],
|
|
|
|
type: "string",
|
|
|
|
assign: false
|
|
|
|
},
|
|
|
|
__webpack_get_script_filename__: {
|
|
|
|
expr: RuntimeGlobals.getChunkScriptFilename,
|
|
|
|
req: [RuntimeGlobals.getChunkScriptFilename],
|
|
|
|
type: "function",
|
|
|
|
assign: true
|
|
|
|
},
|
|
|
|
__webpack_runtime_id__: {
|
|
|
|
expr: RuntimeGlobals.runtimeId,
|
|
|
|
req: [RuntimeGlobals.runtimeId],
|
|
|
|
assign: false
|
|
|
|
},
|
|
|
|
"require.onError": {
|
|
|
|
expr: RuntimeGlobals.uncaughtErrorHandler,
|
|
|
|
req: [RuntimeGlobals.uncaughtErrorHandler],
|
|
|
|
type: undefined, // type is not known, could be function or undefined
|
|
|
|
assign: true // is never a pattern
|
|
|
|
},
|
|
|
|
__system_context__: {
|
|
|
|
expr: RuntimeGlobals.systemContext,
|
|
|
|
req: [RuntimeGlobals.systemContext],
|
|
|
|
type: "object",
|
|
|
|
assign: false
|
|
|
|
},
|
|
|
|
__webpack_share_scopes__: {
|
|
|
|
expr: RuntimeGlobals.shareScopeMap,
|
|
|
|
req: [RuntimeGlobals.shareScopeMap],
|
|
|
|
type: "object",
|
|
|
|
assign: false
|
|
|
|
},
|
|
|
|
__webpack_init_sharing__: {
|
|
|
|
expr: RuntimeGlobals.initializeSharing,
|
|
|
|
req: [RuntimeGlobals.initializeSharing],
|
|
|
|
type: "function",
|
|
|
|
assign: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-01-03 01:00:08 +08:00
|
|
|
|
2023-04-01 01:56:32 +08:00
|
|
|
const PLUGIN_NAME = "APIPlugin";
|
|
|
|
|
2017-01-03 01:00:08 +08:00
|
|
|
class APIPlugin {
|
2018-11-03 04:05:46 +08:00
|
|
|
/**
|
|
|
|
* Apply the plugin
|
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-03 01:00:08 +08:00
|
|
|
apply(compiler) {
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.compilation.tap(
|
2023-04-01 01:56:32 +08:00
|
|
|
PLUGIN_NAME,
|
2018-02-25 09:00:20 +08:00
|
|
|
(compilation, { normalModuleFactory }) => {
|
2025-08-26 02:23:34 +08:00
|
|
|
const moduleOutput = compilation.options.output.module;
|
|
|
|
const nodeTarget = compiler.platform.node;
|
|
|
|
const nodeEsm = moduleOutput && nodeTarget;
|
|
|
|
|
|
|
|
const REPLACEMENTS = getReplacements();
|
|
|
|
if (nodeEsm) {
|
2025-09-10 03:11:56 +08:00
|
|
|
REPLACEMENTS.__non_webpack_require__.expr =
|
|
|
|
"__WEBPACK_EXTERNAL_createRequire_require";
|
2025-08-26 02:23:34 +08:00
|
|
|
}
|
2023-06-03 07:58:12 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
compilation.dependencyTemplates.set(
|
|
|
|
ConstDependency,
|
|
|
|
new ConstDependency.Template()
|
|
|
|
);
|
2017-01-03 01:00:08 +08:00
|
|
|
|
2018-11-06 01:54:05 +08:00
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.chunkName)
|
2025-07-17 00:13:14 +08:00
|
|
|
.tap(PLUGIN_NAME, (chunk) => {
|
2018-11-06 01:54:05 +08:00
|
|
|
compilation.addRuntimeModule(
|
|
|
|
chunk,
|
2024-01-26 22:47:14 +08:00
|
|
|
new ChunkNameRuntimeModule(/** @type {string} */ (chunk.name))
|
2018-11-06 01:54:05 +08:00
|
|
|
);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
compilation.hooks.runtimeRequirementInTree
|
|
|
|
.for(RuntimeGlobals.getFullHash)
|
2025-07-08 22:46:17 +08:00
|
|
|
.tap(PLUGIN_NAME, (chunk, _set) => {
|
2020-09-02 00:09:28 +08:00
|
|
|
compilation.addRuntimeModule(chunk, new GetFullHashRuntimeModule());
|
2018-11-06 01:54:05 +08:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2023-06-03 08:58:03 +08:00
|
|
|
const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
|
|
|
|
|
|
|
|
hooks.renderModuleContent.tap(
|
|
|
|
PLUGIN_NAME,
|
|
|
|
(source, module, renderContext) => {
|
2024-03-05 22:40:46 +08:00
|
|
|
if (/** @type {BuildInfo} */ (module.buildInfo).needCreateRequire) {
|
2023-06-03 08:58:03 +08:00
|
|
|
const chunkInitFragments = [
|
2025-09-10 03:11:56 +08:00
|
|
|
getExternalModuleNodeCommonjsInitFragment(
|
|
|
|
renderContext.runtimeTemplate
|
2023-06-03 08:58:03 +08:00
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
renderContext.chunkInitFragments.push(...chunkInitFragments);
|
|
|
|
}
|
|
|
|
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-12-22 21:51:09 +08:00
|
|
|
/**
|
|
|
|
* @param {JavascriptParser} parser the parser
|
|
|
|
*/
|
2025-07-17 00:13:14 +08:00
|
|
|
const handler = (parser) => {
|
2024-08-02 02:36:27 +08:00
|
|
|
for (const key of Object.keys(REPLACEMENTS)) {
|
2018-11-06 02:03:12 +08:00
|
|
|
const info = REPLACEMENTS[key];
|
2025-07-17 00:13:14 +08:00
|
|
|
parser.hooks.expression.for(key).tap(PLUGIN_NAME, (expression) => {
|
2023-06-03 08:58:03 +08:00
|
|
|
const dep = toConstantDependency(parser, info.expr, info.req);
|
|
|
|
|
2025-08-26 02:23:34 +08:00
|
|
|
if (key === "__non_webpack_require__" && moduleOutput) {
|
|
|
|
if (nodeTarget) {
|
|
|
|
/** @type {BuildInfo} */
|
|
|
|
(parser.state.module.buildInfo).needCreateRequire = true;
|
|
|
|
} else {
|
|
|
|
const warning = new WebpackError(
|
|
|
|
`${PLUGIN_NAME}\n__non_webpack_require__ is only allowed in target node`
|
|
|
|
);
|
|
|
|
warning.loc = /** @type {DependencyLocation} */ (
|
|
|
|
expression.loc
|
|
|
|
);
|
|
|
|
warning.module = parser.state.module;
|
|
|
|
compilation.warnings.push(warning);
|
|
|
|
}
|
2023-06-03 08:58:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return dep(expression);
|
|
|
|
});
|
2019-08-29 21:28:19 +08:00
|
|
|
if (info.assign === false) {
|
2025-07-17 00:13:14 +08:00
|
|
|
parser.hooks.assign.for(key).tap(PLUGIN_NAME, (expr) => {
|
2019-08-29 21:28:19 +08:00
|
|
|
const err = new WebpackError(`${key} must not be assigned`);
|
2024-01-26 22:47:14 +08:00
|
|
|
err.loc = /** @type {DependencyLocation} */ (expr.loc);
|
2019-08-29 21:28:19 +08:00
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
2018-11-06 02:03:12 +08:00
|
|
|
if (info.type) {
|
|
|
|
parser.hooks.evaluateTypeof
|
|
|
|
.for(key)
|
2023-04-01 01:56:32 +08:00
|
|
|
.tap(PLUGIN_NAME, evaluateToString(info.type));
|
2018-11-06 02:03:12 +08:00
|
|
|
}
|
2024-08-02 02:36:27 +08:00
|
|
|
}
|
2020-12-22 21:51:09 +08:00
|
|
|
|
|
|
|
parser.hooks.expression
|
|
|
|
.for("__webpack_layer__")
|
2025-07-17 00:13:14 +08:00
|
|
|
.tap(PLUGIN_NAME, (expr) => {
|
2020-12-22 21:51:09 +08:00
|
|
|
const dep = new ConstDependency(
|
|
|
|
JSON.stringify(parser.state.module.layer),
|
2024-01-26 22:47:14 +08:00
|
|
|
/** @type {Range} */ (expr.range)
|
2020-12-22 21:51:09 +08:00
|
|
|
);
|
2024-01-26 22:47:14 +08:00
|
|
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
2020-12-22 21:51:09 +08:00
|
|
|
parser.state.module.addPresentationalDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
parser.hooks.evaluateIdentifier
|
|
|
|
.for("__webpack_layer__")
|
2025-07-17 00:13:14 +08:00
|
|
|
.tap(PLUGIN_NAME, (expr) =>
|
2020-12-22 21:51:09 +08:00
|
|
|
(parser.state.module.layer === null
|
|
|
|
? new BasicEvaluatedExpression().setNull()
|
|
|
|
: new BasicEvaluatedExpression().setString(
|
|
|
|
parser.state.module.layer
|
2024-01-27 00:17:45 +08:00
|
|
|
)
|
2024-01-26 22:47:14 +08:00
|
|
|
).setRange(/** @type {Range} */ (expr.range))
|
2020-12-22 21:51:09 +08:00
|
|
|
);
|
|
|
|
parser.hooks.evaluateTypeof
|
|
|
|
.for("__webpack_layer__")
|
2025-07-17 00:13:14 +08:00
|
|
|
.tap(PLUGIN_NAME, (expr) =>
|
2020-12-22 21:51:09 +08:00
|
|
|
new BasicEvaluatedExpression()
|
|
|
|
.setString(
|
|
|
|
parser.state.module.layer === null ? "object" : "string"
|
|
|
|
)
|
2024-01-26 22:47:14 +08:00
|
|
|
.setRange(/** @type {Range} */ (expr.range))
|
2020-12-22 21:51:09 +08:00
|
|
|
);
|
2022-01-31 18:06:30 +08:00
|
|
|
|
|
|
|
parser.hooks.expression
|
2022-01-31 20:53:43 +08:00
|
|
|
.for("__webpack_module__.id")
|
2025-07-17 00:13:14 +08:00
|
|
|
.tap(PLUGIN_NAME, (expr) => {
|
2024-03-05 22:40:46 +08:00
|
|
|
/** @type {BuildInfo} */
|
|
|
|
(parser.state.module.buildInfo).moduleConcatenationBailout =
|
2022-01-31 20:53:43 +08:00
|
|
|
"__webpack_module__.id";
|
2022-01-31 18:06:30 +08:00
|
|
|
const dep = new ConstDependency(
|
2024-07-31 10:39:30 +08:00
|
|
|
`${parser.state.module.moduleArgument}.id`,
|
2024-01-26 22:47:14 +08:00
|
|
|
/** @type {Range} */ (expr.range),
|
2022-01-31 18:06:30 +08:00
|
|
|
[RuntimeGlobals.moduleId]
|
|
|
|
);
|
2024-01-26 22:47:14 +08:00
|
|
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
2022-01-31 18:06:30 +08:00
|
|
|
parser.state.module.addPresentationalDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
parser.hooks.expression
|
|
|
|
.for("__webpack_module__")
|
2025-07-17 00:13:14 +08:00
|
|
|
.tap(PLUGIN_NAME, (expr) => {
|
2024-03-05 22:40:46 +08:00
|
|
|
/** @type {BuildInfo} */
|
|
|
|
(parser.state.module.buildInfo).moduleConcatenationBailout =
|
2022-01-31 18:06:30 +08:00
|
|
|
"__webpack_module__";
|
|
|
|
const dep = new ConstDependency(
|
|
|
|
parser.state.module.moduleArgument,
|
2024-01-26 22:47:14 +08:00
|
|
|
/** @type {Range} */ (expr.range),
|
2022-01-31 18:06:30 +08:00
|
|
|
[RuntimeGlobals.module]
|
|
|
|
);
|
2024-01-26 22:47:14 +08:00
|
|
|
dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
2022-01-31 18:06:30 +08:00
|
|
|
parser.state.module.addPresentationalDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
parser.hooks.evaluateTypeof
|
|
|
|
.for("__webpack_module__")
|
2023-04-01 01:56:32 +08:00
|
|
|
.tap(PLUGIN_NAME, evaluateToString("object"));
|
2018-02-25 09:00:20 +08:00
|
|
|
};
|
2017-12-14 17:22:27 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
normalModuleFactory.hooks.parser
|
2023-04-01 01:56:32 +08:00
|
|
|
.for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
|
|
|
.tap(PLUGIN_NAME, handler);
|
2018-02-25 09:00:20 +08:00
|
|
|
normalModuleFactory.hooks.parser
|
2023-04-01 01:56:32 +08:00
|
|
|
.for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
|
|
|
|
.tap(PLUGIN_NAME, handler);
|
2018-02-25 09:00:20 +08:00
|
|
|
normalModuleFactory.hooks.parser
|
2023-04-01 01:56:32 +08:00
|
|
|
.for(JAVASCRIPT_MODULE_TYPE_ESM)
|
|
|
|
.tap(PLUGIN_NAME, handler);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
);
|
2017-01-03 01:00:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = APIPlugin;
|