Skip to content

Commit

Permalink
fix: assert output won’t be wrapped in IIFE
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 19, 2024
1 parent c54d4a4 commit 5cff7bd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 62 deletions.
91 changes: 33 additions & 58 deletions lib/library/ModernModuleLibraryPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
"use strict";

const { ConcatSource } = require("webpack-sources");
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const ConcatenatedModule = require("../optimize/ConcatenatedModule");
const propertyAccess = require("../util/propertyAccess");
const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");

/** @typedef {import("webpack-sources").Source} Source */
Expand All @@ -26,6 +23,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
/**
* @typedef {Object} ModernModuleLibraryPluginOptions
* @property {LibraryType} type
* @property {Record<string, string>=} exportsDefinitions
*/

/**
Expand All @@ -49,7 +47,8 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
compiler.hooks.compilation.tap("ModernModuleLibraryPlugin", compilation => {
const { exportsDefinitions } =
ConcatenatedModule.getCompilationHooks(compilation);
exportsDefinitions.tap("ModernModuleLibraryPlugin", () => {
exportsDefinitions.tap("ModernModuleLibraryPlugin", exportsDefinition => {
this.exportsDefinitions = exportsDefinition;
return true;
});
});
Expand Down Expand Up @@ -94,67 +93,43 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
{ moduleGraph, chunk },
{ options, compilation }
) {
const shouldExportFinalName = module.buildMeta.exportsFinalName;
const result = new ConcatSource(source);
const exportsInfo = moduleGraph.getExportsInfo(module);
const exports = [];

if (shouldExportFinalName) {
const definitions = module.buildMeta.exportsFinalName;
for (const exportInfo of exportsInfo.orderedExports) {
let shouldContinue = false;
const reexport = exportInfo.findTarget(moduleGraph, _m => true);

if (reexport) {
const exp = moduleGraph.getExportsInfo(reexport.module);

for (const reexportInfo of exp.orderedExports) {
if (
!reexportInfo.provided &&
reexportInfo.name === reexport.export[0]
) {
shouldContinue = true;
}
for (const exportInfo of exportsInfo.orderedExports) {
let shouldContinue = false;
const reexport = exportInfo.findTarget(moduleGraph, _m => true);

if (reexport) {
const exp = moduleGraph.getExportsInfo(reexport.module);

for (const reexportInfo of exp.orderedExports) {
if (
!reexportInfo.provided &&
reexportInfo.name === reexport.export[0]
) {
shouldContinue = true;
}
}

if (shouldContinue) continue;

const webpackExportsProperty = exportInfo.getUsedName(
exportInfo.name,
chunk.runtime
);
const finalName =
definitions[
/** @type {string} */
(webpackExportsProperty)
];
exports.push(
finalName === exportInfo.name
? finalName
: `${finalName} as ${exportInfo.name}`
);
}
} else {
const isAsync = moduleGraph.isAsync(module);
if (isAsync) {
result.add(
`${RuntimeGlobals.exports} = await ${RuntimeGlobals.exports};\n`
);
}
for (const exportInfo of exportsInfo.orderedExports) {
if (!exportInfo.provided) continue;
const varName = `${RuntimeGlobals.exports}${Template.toIdentifier(
exportInfo.name
)}`;
result.add(
`var ${varName} = ${RuntimeGlobals.exports}${propertyAccess([
/** @type {string} */
(exportInfo.getUsedName(exportInfo.name, chunk.runtime))
])};\n`
);
exports.push(`${varName} as ${exportInfo.name}`);
}

if (shouldContinue) continue;

const webpackExportsProperty = exportInfo.getUsedName(
exportInfo.name,
chunk.runtime
);
const finalName =
this.exportsDefinitions[
/** @type {string} */
(webpackExportsProperty)
];
exports.push(
finalName === exportInfo.name
? finalName
: `${finalName} as ${exportInfo.name}`
);
}

if (exports.length > 0) {
Expand Down
2 changes: 0 additions & 2 deletions lib/optimize/ConcatenatedModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -1543,8 +1543,6 @@ class ConcatenatedModule extends Module {
this.exportsArgument
}, {${definitions.join(",")}\n});\n`
);
} else {
this.buildMeta.exportsFinalName = definitionsForHook;
}
}

Expand Down
10 changes: 8 additions & 2 deletions test/__snapshots__/StatsTestCases.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2727,21 +2727,24 @@ LOG from webpack.FileSystemInfo
exports[`StatsTestCases should print correct stats for real-content-hash 1`] = `
"a-normal:
<<<<<<< HEAD
<<<<<<< HEAD
assets by path *.js 3.25 KiB
asset 2a14e619729cafd3bb77-2a14e6.js 2.8 KiB [emitted] [immutable] [minimized] (name: runtime)
asset 0a1b2c7ae2cee5086d70-0a1b2c.js 232 bytes [emitted] [immutable] [minimized] (name: lazy)
=======
=======
>>>>>>> e8568abb4 (fix: assert output won’t be wrapped in IIFE)
assets by path *.js 3.08 KiB
asset 12beb53ee92f2ec82aeb-12beb5.js 2.63 KiB [emitted] [immutable] [minimized] (name: runtime)
asset 4c293dac5a10c0220231-4c293d.js 2.63 KiB [emitted] [immutable] [minimized] (name: runtime)
asset 1b48b6222bb1ec5c3c23-1b48b6.js 225 bytes [emitted] [immutable] [minimized] (name: lazy)
>>>>>>> f162fa1a7 (feat: tree shakable output for module library)
asset fdf80674ac46a61ff9fe-fdf806.js 213 bytes [emitted] [immutable] [minimized] (name: index)
asset 666f2b8847021ccc7608-666f2b.js 21 bytes [emitted] [immutable] [minimized] (name: a, b)
assets by chunk 20.4 KiB (auxiliary name: lazy)
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
<<<<<<< HEAD
<<<<<<< HEAD
Entrypoint index 3 KiB (5.89 KiB) = 2a14e619729cafd3bb77-2a14e6.js 2.8 KiB fdf80674ac46a61ff9fe-fdf806.js 213 bytes 1 auxiliary asset
=======
Expand All @@ -2751,6 +2754,9 @@ exports[`StatsTestCases should print correct stats for real-content-hash 1`] = `
Entrypoint index 2.84 KiB (5.89 KiB) = 12beb53ee92f2ec82aeb-12beb5.js 2.63 KiB fdf80674ac46a61ff9fe-fdf806.js 213 bytes 1 auxiliary asset
>>>>>>> f162fa1a7 (feat: tree shakable output for module library)
>>>>>>> a0dcf6578 (feat: tree shakable output for module library)
=======
Entrypoint index 2.84 KiB (5.89 KiB) = 4c293dac5a10c0220231-4c293d.js 2.63 KiB fdf80674ac46a61ff9fe-fdf806.js 213 bytes 1 auxiliary asset
>>>>>>> e8568abb4 (fix: assert output won’t be wrapped in IIFE)
Entrypoint a 21 bytes = 666f2b8847021ccc7608-666f2b.js
Entrypoint b 21 bytes = 666f2b8847021ccc7608-666f2b.js
runtime modules 7.1 KiB 8 modules
Expand Down

0 comments on commit 5cff7bd

Please sign in to comment.