fix: should create export for external

This commit is contained in:
jserfeng 2025-03-31 18:56:03 +08:00
parent 97d4961cd1
commit 84342804dc
5 changed files with 62 additions and 8 deletions

View File

@ -96,6 +96,7 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
const definitions = const definitions =
/** @type {BuildMeta} */ /** @type {BuildMeta} */
(module.buildMeta).exportsFinalName; (module.buildMeta).exportsFinalName;
const shortHandedExports = [];
const exports = []; const exports = [];
for (const exportInfo of exportsInfo.orderedExports) { for (const exportInfo of exportsInfo.orderedExports) {
@ -107,7 +108,7 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
for (const reexportInfo of exp.orderedExports) { for (const reexportInfo of exp.orderedExports) {
if ( if (
!reexportInfo.provided && reexportInfo.provided === false &&
reexportInfo.name === /** @type {string[]} */ (reexport.export)[0] reexportInfo.name === /** @type {string[]} */ (reexport.export)[0]
) { ) {
shouldContinue = true; shouldContinue = true;
@ -127,15 +128,26 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
/** @type {string} */ /** @type {string} */
(webpackExportsProperty) (webpackExportsProperty)
]; ];
exports.push(
finalName === exportInfo.name if (finalName && (finalName.includes(".") || finalName.includes("["))) {
? finalName exports.push([exportInfo.name, finalName]);
: `${finalName} as ${exportInfo.name}` } else {
); shortHandedExports.push(
finalName === exportInfo.name
? finalName
: `${finalName} as ${exportInfo.name}`
);
}
} }
if (exports.length > 0) { if (shortHandedExports.length > 0) {
result.add(`export { ${exports.join(", ")} };\n`); result.add(`export { ${shortHandedExports.join(", ")} };\n`);
}
for (const [exportName, final] of exports) {
result.add(
`export ${compilation.outputOptions.environment.const ? "const" : "var"} ${exportName} = ${final};\n`
);
} }
return result; return result;

View File

@ -0,0 +1 @@
it('should compile', () => {})

View File

@ -0,0 +1,5 @@
module.exports = {
findBundle() {
return ["main.js"];
}
};

View File

@ -0,0 +1 @@
export { value } from 'external0'

View File

@ -0,0 +1,35 @@
/** @type {import("../../../../types").Configuration} */
module.exports = {
mode: "none",
entry: { main: "./index.js", test: "./test" },
output: {
module: true,
library: {
type: "modern-module"
},
filename: "[name].js",
chunkFormat: "module"
},
experiments: {
outputModule: true
},
resolve: {
extensions: [".js"]
},
externalsType: "module",
externals: ["external0"],
optimization: {
concatenateModules: true
},
plugins: [
function () {
const handler = compilation => {
compilation.hooks.afterProcessAssets.tap("testcase", assets => {
const source = assets["test.js"].source();
expect(source).toContain("export const value");
});
};
this.hooks.compilation.tap("testcase", handler);
}
]
};