Skip to content

Commit ec300ae

Browse files
fix(esm): escape external requests in star reexports (#22078)
* fix(esm): escape external requests in star reexports * fix(esm): escape line separators in generated module specifiers Use `toJsStringLiteral` instead of `JSON.stringify` for the requests interpolated into generated `import` and `export * from` statements, so U+2028/U+2029 are escaped rather than left raw in the emitted source. * test(library): assert the escaped specifiers outside the bundle `__non_webpack_require__` in an ESM entry makes webpack emit `import { createRequire } from "node:module"`, which Node 10 cannot link, so read the emitted library bundle from `afterExecute` instead. --------- Co-authored-by: Alexander Akait <[email protected]>
1 parent b0446fd commit ec300ae

7 files changed

Lines changed: 104 additions & 6 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+
Escape external requests in generated ESM imports and star reexports.

‎lib/ExternalModule.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const { coreModules } = require("./node/nodeBuiltins");
2929
const createHash = require("./util/createHash");
3030
const createHooksRegistry = require("./util/createHooksRegistry");
3131
const extractUrlAndGlobal = require("./util/extractUrlAndGlobal");
32+
const { toJsStringLiteral } = require("./util/identifier");
3233
const makeSerializable = require("./util/makeSerializable");
3334
const memoize = require("./util/memoize");
3435
const { propertyAccess } = require("./util/property");
@@ -518,26 +519,26 @@ class ModuleExternalInitFragment extends InitFragment {
518519
if (ImportPhaseUtils.isSource(phase) && imported === true) {
519520
// There is no `import source * as ns` form: the source phase binds one
520521
// identifier, so it stands in for the namespace the other branches build.
521-
content = `import source ${identifier} from ${JSON.stringify(
522+
content = `import source ${identifier} from ${toJsStringLiteral(
522523
request
523524
)}${attributes};\n`;
524525
} else if (imported === true) {
525526
// namespace
526527
const phaseKeyword = ImportPhaseUtils.isDefer(phase) ? "defer " : "";
527-
content = `import ${phaseKeyword}* as ${identifier} from ${JSON.stringify(
528+
content = `import ${phaseKeyword}* as ${identifier} from ${toJsStringLiteral(
528529
request
529530
)}${attributes};\n`;
530531
} else if (imported.length === 0) {
531532
// just import, no use
532-
content = `import ${JSON.stringify(request)}${attributes};\n`;
533+
content = `import ${toJsStringLiteral(request)}${attributes};\n`;
533534
} else if (
534535
ImportPhaseUtils.isSource(phase) &&
535536
imported.length === 1 &&
536537
imported[0][0] === "default"
537538
) {
538539
// `import source x from "…"` — the source-phase form binds the source
539540
// object directly to a single identifier (no namespace, no destructuring).
540-
content = `import source ${imported[0][1]} from ${JSON.stringify(
541+
content = `import source ${imported[0][1]} from ${toJsStringLiteral(
541542
request
542543
)}${attributes};\n`;
543544
} else {
@@ -548,7 +549,7 @@ class ModuleExternalInitFragment extends InitFragment {
548549
}
549550
return name;
550551
})
551-
.join(", ")} } from ${JSON.stringify(request)}${attributes};\n`;
552+
.join(", ")} } from ${toJsStringLiteral(request)}${attributes};\n`;
552553
}
553554
return content;
554555
}

‎lib/library/ModuleLibraryPlugin.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const HarmonyExportSpecifierDependency = require("../dependencies/HarmonyExportS
1717
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
1818
const ConcatenatedModule = require("../optimize/ConcatenatedModule");
1919
const { InlinedUsedName } = require("../optimize/InlineExports");
20+
const { toJsStringLiteral } = require("../util/identifier");
2021
const {
2122
RESERVED_IDENTIFIER,
2223
SAFE_IDENTIFIER,
@@ -478,7 +479,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
478479
resolveDynamicStarReexport(module, false);
479480

480481
for (const request of moduleRequests) {
481-
result.add(`export * from "${request}";\n`);
482+
result.add(`export * from ${toJsStringLiteral(request)};\n`);
482483
}
483484

484485
for (const [origin, used] of unknownProvidedExports) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as library from "./library.mjs";
2+
3+
it("should preserve escaped external requests in native star reexports", () => {
4+
expect(library.quoted).toBe(1);
5+
expect(library.backslash).toBe(2);
6+
expect(library.newline).toBe(3);
7+
expect(library.separator).toBe(4);
8+
expect(library.paragraph).toBe(5);
9+
expect(library.plain).toBe(6);
10+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from 'quoted"module';
2+
export * from "backslash\\module";
3+
export * from "line\nmodule";
4+
export * from "separator\u2028module";
5+
export * from "paragraph\u2029module";
6+
export * from "plain-module";
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
// A raw U+2028/U+2029 ends a string literal outside the ES2019 JSON superset,
7+
// so both must reach the emitted specifier escaped.
8+
const ESCAPED_SPECIFIERS = [
9+
/import\s*\*\s*as\s+\S+\s*from\s*"separator\\u2028module"/,
10+
/import\s*\*\s*as\s+\S+\s*from\s*"paragraph\\u2029module"/,
11+
/export\s*\*\s*from\s*"separator\\u2028module"/,
12+
/export\s*\*\s*from\s*"paragraph\\u2029module"/
13+
];
14+
15+
module.exports = {
16+
/**
17+
* @param {number} index configuration index
18+
* @returns {string} emitted test bundle
19+
*/
20+
findBundle(index) {
21+
return `main${index}.mjs`;
22+
},
23+
modules: {
24+
'quoted"module': { quoted: 1 },
25+
"backslash\\module": { backslash: 2 },
26+
"line\nmodule": { newline: 3 },
27+
"separator\u2028module": { separator: 4 },
28+
"paragraph\u2029module": { paragraph: 5 },
29+
"plain-module": { plain: 6 }
30+
},
31+
/**
32+
* @param {import("../../../../").Configuration | import("../../../../").Configuration[]} options webpack options
33+
* @returns {void}
34+
*/
35+
afterExecute(options) {
36+
const configs = Array.isArray(options) ? options : [options];
37+
for (const [index, config] of configs.entries()) {
38+
const output = /** @type {EXPECTED_ANY} */ (config.output);
39+
const source = fs.readFileSync(
40+
path.join(output.path, `library${index}.mjs`),
41+
"utf8"
42+
);
43+
for (const specifier of ESCAPED_SPECIFIERS) {
44+
expect(source).toMatch(specifier);
45+
}
46+
}
47+
}
48+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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: [
18+
{ "./library.mjs": `./library${index}.mjs` },
19+
'quoted"module',
20+
"backslash\\module",
21+
"line\nmodule",
22+
"separator\u2028module",
23+
"paragraph\u2029module",
24+
"plain-module"
25+
],
26+
optimization: { minimize }
27+
}));

0 commit comments

Comments
 (0)