fix: collisions in ESM library

This commit is contained in:
Alexander Akait 2024-11-13 17:02:35 +03:00 committed by GitHub
commit 038e51c4b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 12 deletions

View File

@ -56,15 +56,19 @@ class ModuleChunkFormatPlugin {
"HMR is not implemented for module chunk format yet" "HMR is not implemented for module chunk format yet"
); );
} else { } else {
source.add(`export const id = ${JSON.stringify(chunk.id)};\n`); source.add(
source.add(`export const ids = ${JSON.stringify(chunk.ids)};\n`); `export const __webpack_id__ = ${JSON.stringify(chunk.id)};\n`
source.add("export const modules = "); );
source.add(
`export const __webpack_ids__ = ${JSON.stringify(chunk.ids)};\n`
);
source.add("export const __webpack_modules__ = ");
source.add(modules); source.add(modules);
source.add(";\n"); source.add(";\n");
const runtimeModules = const runtimeModules =
chunkGraph.getChunkRuntimeModulesInOrder(chunk); chunkGraph.getChunkRuntimeModulesInOrder(chunk);
if (runtimeModules.length > 0) { if (runtimeModules.length > 0) {
source.add("export const runtime =\n"); source.add("export const __webpack_runtime__ =\n");
source.add( source.add(
Template.renderChunkRuntimeModules( Template.renderChunkRuntimeModules(
runtimeModules, runtimeModules,

View File

@ -161,29 +161,29 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
withLoading || withExternalInstallChunk withLoading || withExternalInstallChunk
? `var installChunk = ${runtimeTemplate.basicFunction("data", [ ? `var installChunk = ${runtimeTemplate.basicFunction("data", [
runtimeTemplate.destructureObject( runtimeTemplate.destructureObject(
["ids", "modules", "runtime"], ["__webpack_ids__", "__webpack_modules__", "__webpack_runtime__"],
"data" "data"
), ),
'// add "modules" to the modules object,', '// add "modules" to the modules object,',
'// then flag all "ids" as loaded and fire callback', '// then flag all "ids" as loaded and fire callback',
"var moduleId, chunkId, i = 0;", "var moduleId, chunkId, i = 0;",
"for(moduleId in modules) {", "for(moduleId in __webpack_modules__) {",
Template.indent([ Template.indent([
`if(${RuntimeGlobals.hasOwnProperty}(modules, moduleId)) {`, `if(${RuntimeGlobals.hasOwnProperty}(__webpack_modules__, moduleId)) {`,
Template.indent( Template.indent(
`${RuntimeGlobals.moduleFactories}[moduleId] = modules[moduleId];` `${RuntimeGlobals.moduleFactories}[moduleId] = __webpack_modules__[moduleId];`
), ),
"}" "}"
]), ]),
"}", "}",
`if(runtime) runtime(${RuntimeGlobals.require});`, `if(__webpack_runtime__) __webpack_runtime__(${RuntimeGlobals.require});`,
"for(;i < ids.length; i++) {", "for(;i < __webpack_ids__.length; i++) {",
Template.indent([ Template.indent([
"chunkId = ids[i];", "chunkId = __webpack_ids__[i];",
`if(${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) && installedChunks[chunkId]) {`, `if(${RuntimeGlobals.hasOwnProperty}(installedChunks, chunkId) && installedChunks[chunkId]) {`,
Template.indent("installedChunks[chunkId][0]();"), Template.indent("installedChunks[chunkId][0]();"),
"}", "}",
"installedChunks[ids[i]] = 0;" "installedChunks[__webpack_ids__[i]] = 0;"
]), ]),
"}", "}",
withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : "" withOnChunkLoad ? `${RuntimeGlobals.onChunksLoaded}();` : ""

View File

@ -0,0 +1,7 @@
it("should don't have variable name conflict", function() {
expect(true).toBe(true);
});
export const id = "collision";
export const ids = ["collision"];
export const modules = { "collision": true };

View File

@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return ["main.mjs"];
}
};

View File

@ -0,0 +1,11 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
experiments: { outputModule: true },
output: {
filename: "[name].mjs",
library: { type: "module" }
},
optimization: {
runtimeChunk: "single" // any value other than `false`
}
};