Skip to content

Commit 8164328

Browse files
committed
fix: preserve import attributes in ESM star reexports
1 parent e2988be commit 8164328

7 files changed

Lines changed: 82 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Preserve import attributes on ESM library star reexports of externals.

‎lib/library/ModuleLibraryPlugin.js‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
415415
) {
416416
const result = new ConcatSource(source);
417417
/** @type {Set<string>} */
418-
const moduleRequests = new Set();
418+
const externalReexports = new Set();
419419
/** @type {Map<string, string | InlinedUsedName>} */
420420
const unknownProvidedExports = new Map();
421421

@@ -445,7 +445,14 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
445445
) {
446446
// Handle export * from 'external'
447447
if (importedModule instanceof ExternalModule) {
448-
moduleRequests.add(importedModule.userRequest);
448+
let attributes = "";
449+
if (dep.attributes) {
450+
const { _isLegacyAssert, ...values } = dep.attributes;
451+
attributes = ` ${_isLegacyAssert ? "assert" : "with"} ${JSON.stringify(values)}`;
452+
}
453+
externalReexports.add(
454+
`export * from "${importedModule.userRequest}"${attributes};\n`
455+
);
449456
} else {
450457
resolveDynamicStarReexport(importedModule, true);
451458
}
@@ -473,8 +480,8 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
473480

474481
resolveDynamicStarReexport(module, false);
475482

476-
for (const request of moduleRequests) {
477-
result.add(`export * from "${request}";\n`);
483+
for (const reexport of externalReexports) {
484+
result.add(reexport);
478485
}
479486

480487
for (const [origin, used] of unknownProvidedExports) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { execFileSync } from "node:child_process";
2+
import { resolve } from "node:path";
3+
import { pathToFileURL } from "node:url";
4+
5+
it("should preserve import attributes on external star reexports", () => {
6+
const url = pathToFileURL(resolve(__dirname, `library${__STATS_I__}.mjs`));
7+
const output = execFileSync(process.execPath, [
8+
...(process.versions.deno ? ["eval"] : ["--input-type=module", "-e"]),
9+
`import * as library from ${JSON.stringify(url.href)}; console.log(JSON.stringify(library));`
10+
]);
11+
expect(JSON.parse(output.toString())).toEqual({ data: { answer: 42 }, value: 1 });
12+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./data.json" with { type: "json" };
2+
export { default as data } from "./data.json" with { type: "json" };
3+
export const value = 1;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
module.exports = {
4+
/**
5+
* @param {number} index configuration index
6+
* @returns {string} emitted test bundle
7+
*/
8+
findBundle(index) {
9+
return `main${index}.mjs`;
10+
}
11+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
// Bun accepts JSON imports without attributes, so it cannot detect this regression.
4+
module.exports = () =>
5+
!process.versions.bun &&
6+
(Boolean(process.versions.deno) ||
7+
Number(process.versions.node.split(".")[0]) >= 22);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration[]} */
4+
module.exports = [
5+
{ type: "module", minimize: false },
6+
{ type: "module", minimize: true },
7+
{ type: "modern-module", minimize: false },
8+
{ type: "modern-module", minimize: true }
9+
].map(({ type, minimize }, index) => ({
10+
mode: minimize ? "production" : "development",
11+
entry: {
12+
main: "./index.js",
13+
library: { import: "./library.js", library: { type } }
14+
},
15+
output: { module: true, filename: `[name]${index}.mjs` },
16+
externalsType: "module",
17+
externals: ["./data.json"],
18+
optimization: { minimize },
19+
plugins: [
20+
{
21+
apply(compiler) {
22+
compiler.hooks.thisCompilation.tap("testcase", (compilation) => {
23+
compilation.hooks.processAssets.tap("testcase", () => {
24+
compilation.emitAsset(
25+
"data.json",
26+
new compiler.webpack.sources.RawSource('{"answer":42}')
27+
);
28+
});
29+
});
30+
}
31+
}
32+
]
33+
}));

0 commit comments

Comments
 (0)