Skip to content

Commit 56de007

Browse files
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.
1 parent 266448d commit 56de007

7 files changed

Lines changed: 31 additions & 9 deletions

File tree

‎.changeset/013-escape-esm-reexport-requests.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Escape external module requests in ESM library star reexports.
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,
@@ -474,7 +475,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
474475
resolveDynamicStarReexport(module, false);
475476

476477
for (const request of moduleRequests) {
477-
result.add(`export * from ${JSON.stringify(request)};\n`);
478+
result.add(`export * from ${toJsStringLiteral(request)};\n`);
478479
}
479480

480481
for (const [origin, used] of unknownProvidedExports) {

‎test/configCases/library/module-reexport-escaped-request/index.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,19 @@ it("should preserve escaped external requests in native star reexports", () => {
44
expect(library.quoted).toBe(1);
55
expect(library.backslash).toBe(2);
66
expect(library.newline).toBe(3);
7-
expect(library.plain).toBe(4);
7+
expect(library.separator).toBe(4);
8+
expect(library.paragraph).toBe(5);
9+
expect(library.plain).toBe(6);
10+
});
11+
12+
it("should escape line separators in emitted module specifiers", () => {
13+
const fs = __non_webpack_require__("fs");
14+
const source = fs.readFileSync(
15+
__filename.replace(/main(\d+)\.mjs$/, "library$1.mjs"),
16+
"utf-8"
17+
);
18+
expect(source).toMatch(/import\s*\*\s*as\s+\S+\s*from\s*"separator\\u2028module"/);
19+
expect(source).toMatch(/import\s*\*\s*as\s+\S+\s*from\s*"paragraph\\u2029module"/);
20+
expect(source).toMatch(/export\s*\*\s*from\s*"separator\\u2028module"/);
21+
expect(source).toMatch(/export\s*\*\s*from\s*"paragraph\\u2029module"/);
822
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export * from 'quoted"module';
22
export * from "backslash\\module";
33
export * from "line\nmodule";
4+
export * from "separator\u2028module";
5+
export * from "paragraph\u2029module";
46
export * from "plain-module";

‎test/configCases/library/module-reexport-escaped-request/test.config.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module.exports = {
1212
'quoted"module': { quoted: 1 },
1313
"backslash\\module": { backslash: 2 },
1414
"line\nmodule": { newline: 3 },
15-
"plain-module": { plain: 4 }
15+
"separator\u2028module": { separator: 4 },
16+
"paragraph\u2029module": { paragraph: 5 },
17+
"plain-module": { plain: 6 }
1618
}
1719
};

‎test/configCases/library/module-reexport-escaped-request/webpack.config.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module.exports = [
1919
'quoted"module',
2020
"backslash\\module",
2121
"line\nmodule",
22+
"separator\u2028module",
23+
"paragraph\u2029module",
2224
"plain-module"
2325
],
2426
optimization: { minimize }

0 commit comments

Comments
 (0)