Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use global runtime constants for webpack exports #17270

Merged
merged 4 commits into from May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/CompatibilityPlugin.js
Expand Up @@ -113,7 +113,7 @@ class CompatibilityPlugin {
return true;
});
parser.hooks.pattern
.for("__webpack_exports__")
.for(RuntimeGlobals.exports)
.tap(PLUGIN_NAME, pattern => {
parser.tagVariable(pattern.name, nestedWebpackIdentifierTag, {
name: "__nested_webpack_exports__",
Expand Down
26 changes: 14 additions & 12 deletions lib/dependencies/CommonJsPlugin.js
Expand Up @@ -197,10 +197,10 @@ class CommonJsPlugin {
)
);
parser.hooks.expression
.for("module.loaded")
.for(RuntimeGlobals.moduleLoaded)
.tap(PLUGIN_NAME, expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
"module.loaded";
RuntimeGlobals.moduleLoaded;
const dep = new RuntimeRequirementsDependency([
RuntimeGlobals.moduleLoaded
]);
Expand All @@ -209,16 +209,18 @@ class CommonJsPlugin {
return true;
});

parser.hooks.expression.for("module.id").tap(PLUGIN_NAME, expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
"module.id";
const dep = new RuntimeRequirementsDependency([
RuntimeGlobals.moduleId
]);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
});
parser.hooks.expression
.for(RuntimeGlobals.moduleId)
.tap(PLUGIN_NAME, expr => {
parser.state.module.buildInfo.moduleConcatenationBailout =
RuntimeGlobals.moduleId;
const dep = new RuntimeRequirementsDependency([
RuntimeGlobals.moduleId
]);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
return true;
});

parser.hooks.evaluateIdentifier.for("module.hot").tap(
PLUGIN_NAME,
Expand Down
4 changes: 3 additions & 1 deletion lib/dependencies/HarmonyExports.js
Expand Up @@ -5,6 +5,8 @@

"use strict";

const RuntimeGlobals = require("../RuntimeGlobals");

/** @typedef {import("../Parser").ParserState} ParserState */

/** @type {WeakMap<ParserState, boolean>} */
Expand All @@ -22,7 +24,7 @@ exports.enable = (parserState, isStrictHarmony) => {
if (value !== true) {
parserState.module.buildMeta.exportsType = "namespace";
parserState.module.buildInfo.strict = true;
parserState.module.buildInfo.exportsArgument = "__webpack_exports__";
parserState.module.buildInfo.exportsArgument = RuntimeGlobals.exports;
if (isStrictHarmony) {
parserState.module.buildMeta.strictHarmonyModule = true;
parserState.module.buildInfo.moduleArgument = "__webpack_module__";
Expand Down
2 changes: 1 addition & 1 deletion lib/esm/ModuleChunkFormatPlugin.js
Expand Up @@ -166,7 +166,7 @@ class ModuleChunkFormatPlugin {
}
startupSource.add(
`${
final ? "var __webpack_exports__ = " : ""
final ? `var ${RuntimeGlobals.exports} = ` : ""
}__webpack_exec__(${JSON.stringify(moduleId)});\n`
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/javascript/ArrayPushCallbackChunkFormatPlugin.js
Expand Up @@ -121,7 +121,7 @@ class ArrayPushCallbackChunkFormatPlugin {
.getChunkRuntimeRequirements(chunk)
.has(RuntimeGlobals.returnExportsFromRuntime)
) {
runtime.add("return __webpack_exports__;\n");
runtime.add(`return ${RuntimeGlobals.exports};\n`);
}
}
runtime.add("}\n");
Expand Down
34 changes: 18 additions & 16 deletions lib/javascript/JavascriptModulesPlugin.js
Expand Up @@ -801,7 +801,7 @@ class JavascriptModulesPlugin {
}
const lastInlinedModule = last(inlinedModules);
const startupSource = new ConcatSource();
startupSource.add(`var __webpack_exports__ = {};\n`);
startupSource.add(`var ${RuntimeGlobals.exports} = {};\n`);
for (const m of inlinedModules) {
const renderedModule = this.renderModule(
m,
Expand All @@ -817,7 +817,7 @@ class JavascriptModulesPlugin {
);
const exports = runtimeRequirements.has(RuntimeGlobals.exports);
const webpackExports =
exports && m.exportsArgument === "__webpack_exports__";
exports && m.exportsArgument === RuntimeGlobals.exports;
let iife = innerStrict
? "it need to be in strict mode."
: inlinedModules.size > 1
Expand Down Expand Up @@ -849,9 +849,9 @@ class JavascriptModulesPlugin {
if (exports) {
if (m !== lastInlinedModule)
startupSource.add(`var ${m.exportsArgument} = {};\n`);
else if (m.exportsArgument !== "__webpack_exports__")
else if (m.exportsArgument !== RuntimeGlobals.exports)
startupSource.add(
`var ${m.exportsArgument} = __webpack_exports__;\n`
`var ${m.exportsArgument} = ${RuntimeGlobals.exports};\n`
);
}
startupSource.add(renderedModule);
Expand All @@ -860,7 +860,7 @@ class JavascriptModulesPlugin {
}
if (runtimeRequirements.has(RuntimeGlobals.onChunksLoaded)) {
startupSource.add(
`__webpack_exports__ = ${RuntimeGlobals.onChunksLoaded}(__webpack_exports__);\n`
`${RuntimeGlobals.exports} = ${RuntimeGlobals.onChunksLoaded}(${RuntimeGlobals.exports});\n`
);
}
source.add(
Expand Down Expand Up @@ -912,7 +912,7 @@ class JavascriptModulesPlugin {
hasEntryModules &&
runtimeRequirements.has(RuntimeGlobals.returnExportsFromRuntime)
) {
source.add(`${prefix}return __webpack_exports__;\n`);
source.add(`${prefix}return ${RuntimeGlobals.exports};\n`);
}
if (iife) {
source.add("/******/ })()\n");
Expand Down Expand Up @@ -1161,7 +1161,7 @@ class JavascriptModulesPlugin {
}
if (chunks.length > 0) {
buf2.push(
`${i === 0 ? "var __webpack_exports__ = " : ""}${
`${i === 0 ? `var ${RuntimeGlobals.exports} = ` : ""}${
RuntimeGlobals.onChunksLoaded
}(undefined, ${JSON.stringify(
chunks.map(c => c.id)
Expand All @@ -1171,22 +1171,22 @@ class JavascriptModulesPlugin {
);
} else if (useRequire) {
buf2.push(
`${i === 0 ? "var __webpack_exports__ = " : ""}${
`${i === 0 ? `var ${RuntimeGlobals.exports} = ` : ""}${
RuntimeGlobals.require
}(${moduleIdExpr});`
);
} else {
if (i === 0) buf2.push("var __webpack_exports__ = {};");
if (i === 0) buf2.push(`var ${RuntimeGlobals.exports} = {};`);
if (requireScopeUsed) {
buf2.push(
`__webpack_modules__[${moduleIdExpr}](0, ${
i === 0 ? "__webpack_exports__" : "{}"
i === 0 ? RuntimeGlobals.exports : "{}"
}, ${RuntimeGlobals.require});`
);
} else if (entryRuntimeRequirements.has(RuntimeGlobals.exports)) {
buf2.push(
`__webpack_modules__[${moduleIdExpr}](0, ${
i === 0 ? "__webpack_exports__" : "{}"
i === 0 ? RuntimeGlobals.exports : "{}"
});`
);
} else {
Expand All @@ -1196,7 +1196,7 @@ class JavascriptModulesPlugin {
}
if (runtimeRequirements.has(RuntimeGlobals.onChunksLoaded)) {
buf2.push(
`__webpack_exports__ = ${RuntimeGlobals.onChunksLoaded}(__webpack_exports__);`
`${RuntimeGlobals.exports} = ${RuntimeGlobals.onChunksLoaded}(${RuntimeGlobals.exports});`
);
}
if (
Expand All @@ -1209,13 +1209,13 @@ class JavascriptModulesPlugin {
buf.push(
`${RuntimeGlobals.startup} = ${runtimeTemplate.basicFunction("", [
...buf2,
"return __webpack_exports__;"
`return ${RuntimeGlobals.exports};`
])};`
);
buf.push("");
startup.push("// run startup");
startup.push(
`var __webpack_exports__ = ${RuntimeGlobals.startup}();`
`var ${RuntimeGlobals.exports} = ${RuntimeGlobals.startup}();`
);
} else if (runtimeRequirements.has(RuntimeGlobals.startupOnlyBefore)) {
buf.push("// the startup function");
Expand Down Expand Up @@ -1263,7 +1263,9 @@ class JavascriptModulesPlugin {
`${RuntimeGlobals.startup} = ${runtimeTemplate.emptyFunction()};`
);
startup.push("// run startup");
startup.push(`var __webpack_exports__ = ${RuntimeGlobals.startup}();`);
startup.push(
`var ${RuntimeGlobals.exports} = ${RuntimeGlobals.startup}();`
);
}
return result;
}
Expand Down Expand Up @@ -1349,7 +1351,7 @@ class JavascriptModulesPlugin {
? Template.asString([
"",
"// Flag the module as loaded",
"module.loaded = true;",
`${RuntimeGlobals.moduleLoaded} = true;`,
""
])
: "",
Expand Down
2 changes: 1 addition & 1 deletion lib/javascript/StartupHelpers.js
Expand Up @@ -19,7 +19,7 @@ const { getAllChunks } = require("./ChunkHelpers");
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {(string|number)[]} EntryItem */

const EXPORT_PREFIX = "var __webpack_exports__ = ";
const EXPORT_PREFIX = `var ${RuntimeGlobals.exports} = `;

/**
* @param {ChunkGraph} chunkGraph chunkGraph
Expand Down
10 changes: 6 additions & 4 deletions lib/library/AssignLibraryPlugin.js
Expand Up @@ -7,6 +7,7 @@

const { ConcatSource } = require("webpack-sources");
const { UsageState } = require("../ExportsInfo");
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const propertyAccess = require("../util/propertyAccess");
const { getEntryRuntime } = require("../util/runtime");
Expand Down Expand Up @@ -296,7 +297,7 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
if (!exportInfo.provided) continue;
const nameAccess = propertyAccess([exportInfo.name]);
result.add(
`${exportTarget}${nameAccess} = __webpack_exports__${exportAccess}${nameAccess};\n`
`${exportTarget}${nameAccess} = ${RuntimeGlobals.exports}${exportAccess}${nameAccess};\n`
);
}
result.add(
Expand All @@ -310,10 +311,11 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
true
)};\n`
);
let exports = "__webpack_exports__";
/** @type {String} */
let exports = RuntimeGlobals.exports;
if (exportAccess) {
result.add(
`var __webpack_exports_export__ = __webpack_exports__${exportAccess};\n`
`var __webpack_exports_export__ = ${RuntimeGlobals.exports}${exportAccess};\n`
);
exports = "__webpack_exports_export__";
}
Expand All @@ -329,7 +331,7 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
fullNameResolved,
this._getPrefix(compilation).length,
false
)} = __webpack_exports__${exportAccess};\n`
)} = ${RuntimeGlobals.exports}${exportAccess};\n`
);
}
return result;
Expand Down
5 changes: 4 additions & 1 deletion lib/library/ExportPropertyLibraryPlugin.js
Expand Up @@ -7,6 +7,7 @@

const { ConcatSource } = require("webpack-sources");
const { UsageState } = require("../ExportsInfo");
const RuntimeGlobals = require("../RuntimeGlobals");
const propertyAccess = require("../util/propertyAccess");
const { getEntryRuntime } = require("../util/runtime");
const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
Expand Down Expand Up @@ -103,7 +104,9 @@ class ExportPropertyLibraryPlugin extends AbstractLibraryPlugin {
*/
renderStartup(source, module, renderContext, { options }) {
if (!options.export) return source;
const postfix = `__webpack_exports__ = __webpack_exports__${propertyAccess(
const postfix = `${RuntimeGlobals.exports} = ${
RuntimeGlobals.exports
}${propertyAccess(
Array.isArray(options.export) ? options.export : [options.export]
)};\n`;
return new ConcatSource(source, postfix);
Expand Down
9 changes: 6 additions & 3 deletions lib/library/ModuleLibraryPlugin.js
Expand Up @@ -6,6 +6,7 @@
"use strict";

const { ConcatSource } = require("webpack-sources");
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const propertyAccess = require("../util/propertyAccess");
const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
Expand Down Expand Up @@ -80,15 +81,17 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
const exports = [];
const isAsync = moduleGraph.isAsync(module);
if (isAsync) {
result.add(`__webpack_exports__ = await __webpack_exports__;\n`);
result.add(
`${RuntimeGlobals.exports} = await ${RuntimeGlobals.exports};\n`
);
}
for (const exportInfo of exportsInfo.orderedExports) {
if (!exportInfo.provided) continue;
const varName = `__webpack_exports__${Template.toIdentifier(
const varName = `${RuntimeGlobals.exports}${Template.toIdentifier(
exportInfo.name
)}`;
result.add(
`var ${varName} = __webpack_exports__${propertyAccess([
`var ${varName} = ${RuntimeGlobals.exports}${propertyAccess([
/** @type {string} */
(exportInfo.getUsedName(exportInfo.name, chunk.runtime))
])};\n`
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/AsyncModuleRuntimeModule.js
Expand Up @@ -21,7 +21,7 @@ class AsyncModuleRuntimeModule extends HelperRuntimeModule {
const fn = RuntimeGlobals.asyncModule;
return Template.asString([
'var webpackQueues = typeof Symbol === "function" ? Symbol("webpack queues") : "__webpack_queues__";',
'var webpackExports = typeof Symbol === "function" ? Symbol("webpack exports") : "__webpack_exports__";',
`var webpackExports = typeof Symbol === "function" ? Symbol("webpack exports") : "${RuntimeGlobals.exports}";`,
'var webpackError = typeof Symbol === "function" ? Symbol("webpack error") : "__webpack_error__";',
`var resolveQueue = ${runtimeTemplate.basicFunction("queue", [
"if(queue && !queue.d) {",
Expand Down