Skip to content

Commit 73e3966

Browse files
bjohansebasalexander-akait
authored andcommitted
fix: preserve import attributes in ESM star reexports
1 parent be915c3 commit 73e3966

7 files changed

Lines changed: 97 additions & 7 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: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
4646
* ModuleRenderContext
4747
* } from "../javascript/JavascriptModulesPlugin"
4848
*/
49+
/** @import { ImportAttributes } from "../javascript/JavascriptParser" */
4950
/** @import { RuntimeSpec } from "../util/runtime" */
5051
/**
5152
* @import {
@@ -124,6 +125,18 @@ const definesOnlyNativeExports = (module, moduleGraph) => {
124125
const toExportName = (name) =>
125126
SAFE_IDENTIFIER.test(name) ? name : JSON.stringify(name);
126127

128+
/**
129+
* Renders the import attributes of a reexport in the syntax the author wrote,
130+
* since a legacy `assert` clause and a `with` clause are not interchangeable.
131+
* @param {ImportAttributes | undefined} attributes the import attributes
132+
* @returns {string} the attributes clause, empty when there are none
133+
*/
134+
const toAttributesClause = (attributes) => {
135+
if (!attributes) return "";
136+
const { _isLegacyAssert: isLegacyAssert, ...values } = attributes;
137+
return ` ${isLegacyAssert ? "assert" : "with"} ${JSON.stringify(values)}`;
138+
};
139+
127140
/**
128141
* Whether the export name can also be used as the declared binding name.
129142
* @param {string} name export name
@@ -473,7 +486,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
473486
) {
474487
const result = new ConcatSource(source);
475488
/** @type {Set<string>} */
476-
const moduleRequests = new Set();
489+
const externalReexports = new Set();
477490
/** @type {Map<string, string | InlinedUsedName>} */
478491
const unknownProvidedExports = new Map();
479492
/** @type {Set<Module>} */
@@ -509,10 +522,16 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
509522
const request = importedModule.getResolvedRequest();
510523
// A property path is reported by `_checkStarReexportedExternals`,
511524
// and an absent one by the external's own build
512-
if (typeof request === "string") {
513-
moduleRequests.add(request);
514-
} else if (Array.isArray(request) && request.length === 1) {
515-
moduleRequests.add(request[0]);
525+
const specifier =
526+
typeof request === "string"
527+
? request
528+
: Array.isArray(request) && request.length === 1
529+
? request[0]
530+
: undefined;
531+
if (specifier !== undefined) {
532+
externalReexports.add(
533+
`export * from ${toJsStringLiteral(specifier)}${toAttributesClause(dep.attributes)};\n`
534+
);
516535
}
517536
} else if (!visitedReexportModules.has(importedModule)) {
518537
visitedReexportModules.add(importedModule);
@@ -542,8 +561,8 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
542561

543562
resolveDynamicStarReexport(module, false);
544563

545-
for (const request of moduleRequests) {
546-
result.add(`export * from ${toJsStringLiteral(request)};\n`);
564+
for (const reexport of externalReexports) {
565+
result.add(reexport);
547566
}
548567

549568
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)