Skip to content

Commit 5e868ee

Browse files
committed
fix: preserve literals when extracting source maps
1 parent e1c10dc commit 5e868ee

17 files changed

Lines changed: 276 additions & 21 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 string contents when extracting source map comments.

‎lib/module/extractSourceMap.js‎

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ const { isAbsolute, join } = require("../fs/fs");
2020
* @typedef {object} SourceMappingURL
2121
* @property {string} sourceMappingURL
2222
* @property {string} replacementString
23+
* @property {number} replacementStart
2324
*/
2425

2526
// Matches only the last occurrence of sourceMappingURL
2627
const innerRegex = /\s*[#@]\s*sourceMappingURL\s*=\s*([^\s'"]*)\s*/;
2728

2829
const validProtocolPattern = /^[a-z][a-z0-9+.-]*:/i;
30+
const whitespaceRegex = /\s/;
31+
const lineBreakRegex = /[\r\n\u2028\u2029]/;
32+
const ambiguousCommentRegex = /['"`]|\*\//;
33+
const ambiguousBlockCommentRegex = /['"`\]]|\*\/(?!$)/;
2934

3035
const sourceMappingURLRegex = new RegExp(
31-
"(?:" +
36+
"^(?:" +
3237
"/\\*" +
3338
"(?:\\s*\r?\n(?://)?)?" +
3439
`(?:${innerRegex.source})` +
@@ -37,7 +42,7 @@ const sourceMappingURLRegex = new RegExp(
3742
"|" +
3843
`//(?:${innerRegex.source})` +
3944
")" +
40-
"\\s*"
45+
"\\s*$"
4146
);
4247

4348
/**
@@ -46,24 +51,67 @@ const sourceMappingURLRegex = new RegExp(
4651
* @returns {SourceMappingURL} source mapping information
4752
*/
4853
function getSourceMappingURL(code) {
49-
const lines = code.split(/^/m);
50-
/** @type {RegExpMatchArray | null | undefined} */
51-
let match;
52-
53-
for (let i = lines.length - 1; i >= 0; i--) {
54-
match = lines[i].match(sourceMappingURLRegex);
55-
if (match) {
54+
let end = code.length;
55+
let lineStart = end;
56+
// ECMA-426's non-parsing extraction stops at code or ambiguous comments.
57+
// Block comments also support CSS and existing JavaScript annotations.
58+
while (end > 0) {
59+
while (end > 0 && whitespaceRegex.test(code[end - 1])) end--;
60+
if (end === 0) break;
61+
// Consecutive block comments can share a long line of generated code.
62+
if (lineStart >= end) {
63+
lineStart = end;
64+
while (lineStart > 0 && !lineBreakRegex.test(code[lineStart - 1])) {
65+
lineStart--;
66+
}
67+
while (lineStart < end && whitespaceRegex.test(code[lineStart])) {
68+
lineStart++;
69+
}
70+
}
71+
let start = lineStart;
72+
let comment;
73+
if (code.startsWith("//", start)) {
74+
comment = code.slice(start, end);
75+
if (ambiguousCommentRegex.test(comment)) break;
76+
} else if (code.endsWith("*/", end)) {
77+
start = code.lastIndexOf("/*", end - 2);
78+
if (start < 0) break;
79+
comment = code.slice(start, end);
80+
// An escaped opener or a character class can belong to a trailing RegExp.
81+
if (
82+
code[start - 1] === "\\" ||
83+
ambiguousBlockCommentRegex.test(comment)
84+
) {
85+
break;
86+
}
87+
// Comments cannot nest; an inner opener may only be comment text.
88+
if (
89+
start > 0 &&
90+
code.lastIndexOf("/*", start - 1) > code.lastIndexOf("*/", start - 1)
91+
) {
92+
break;
93+
}
94+
} else {
5695
break;
5796
}
97+
const match = sourceMappingURLRegex.exec(comment);
98+
if (match) {
99+
const sourceMappingURL = match[1] || match[2] || "";
100+
return {
101+
sourceMappingURL: sourceMappingURL
102+
? decodeURI(sourceMappingURL)
103+
: sourceMappingURL,
104+
replacementString: comment,
105+
replacementStart: start
106+
};
107+
}
108+
end = start;
58109
}
59110

60-
const sourceMappingURL = match ? match[1] || match[2] || "" : "";
61-
62111
return {
63-
sourceMappingURL: sourceMappingURL
64-
? decodeURI(sourceMappingURL)
65-
: sourceMappingURL,
66-
replacementString: match ? match[0] : ""
112+
sourceMappingURL: "",
113+
replacementString: "",
114+
replacementStart: 0
67115
};
68116
}
69117

@@ -229,7 +277,8 @@ async function extractSourceMap(stringOrBuffer, resourcePath, readResource) {
229277
source: stringOrBuffer,
230278
sourceMap: inputSourceMap
231279
};
232-
const { sourceMappingURL, replacementString } = getSourceMappingURL(input);
280+
const { sourceMappingURL, replacementString, replacementStart } =
281+
getSourceMappingURL(input);
233282

234283
if (!sourceMappingURL) {
235284
return output;
@@ -310,7 +359,9 @@ async function extractSourceMap(stringOrBuffer, resourcePath, readResource) {
310359
}
311360

312361
return {
313-
source: input.replace(replacementString, ""),
362+
source:
363+
input.slice(0, replacementStart) +
364+
input.slice(replacementStart + replacementString.length),
314365
sourceMap: /** @type {RawSourceMap} */ (newMap)
315366
};
316367
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fs from "fs/promises";
2+
import { dirname, resolve } from "path";
3+
import { fileURLToPath } from "url";
4+
5+
/**
6+
* @returns {Promise<void>} resolves when the fixture is ready
7+
*/
8+
export async function setup() {
9+
const generated = resolve(
10+
dirname(fileURLToPath(import.meta.url)),
11+
"generated"
12+
);
13+
const source = `module.exports = ${JSON.stringify("value".repeat(4000))};`;
14+
// A long line followed by adjacent comments exposes repeated prefix scans.
15+
const trailing = " /* ordinary trailing comment */".repeat(128);
16+
await fs.mkdir(generated, { recursive: true });
17+
await fs.writeFile(
18+
resolve(generated, "module.js"),
19+
`${source} /*# sourceMappingURL=source.map */${trailing}`
20+
);
21+
await fs.writeFile(
22+
resolve(generated, "source.map"),
23+
JSON.stringify({
24+
version: 3,
25+
sources: ["original.js"],
26+
sourcesContent: [source],
27+
names: [],
28+
mappings: "AAAA"
29+
})
30+
);
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import("../../..").Configuration} */
2+
export default {
3+
entry: "./generated/module.js",
4+
target: "node",
5+
devtool: "source-map",
6+
module: { rules: [{ extractSourceMap: true }] },
7+
output: { library: { type: "commonjs2" } }
8+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @returns {string} block module value
3+
*/
4+
export function block() {
5+
return "block";
6+
}
7+
/*# sourceMappingURL=block.js.map */
8+
/* An ordinary trailing block. */ /* Another block on the same line. */

‎test/configCases/source-map/extract-source-map-strings/block.js.map‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { angularStyle, multiline, nested, quoted, escaped, regex } from "./strings";
2+
import { mapped, directive } from "./mapped";
3+
import { block } from "./block";
4+
import escapedSlash from "./regex-escaped";
5+
import characterClass from "./regex-class";
6+
import "./style.css";
7+
const fs = require("fs");
8+
9+
it("should leave source map directives inside literals unchanged", () => {
10+
expect(angularStyle("styles.map")).toBe(
11+
"body {}\n/*# sourceMappingURL=styles.map */"
12+
);
13+
expect(multiline).toBe("\n//# sourceMappingURL=missing.map\n");
14+
expect(nested).toBe("//# sourceMappingURL=missing.map");
15+
expect(quoted).toBe("/*# sourceMappingURL=missing.map */");
16+
expect(escaped).toBe("`\n//# sourceMappingURL=missing.map\n");
17+
expect(regex.test("#")).toBe(true);
18+
expect(escapedSlash.source).toBe("\\/*# sourceMappingURL=missing.map *");
19+
expect(characterClass.source).toBe("[/*#sourceMappingURL=missing.map]*");
20+
});
21+
22+
it("should remove the real directive without changing an identical string", () => {
23+
expect(mapped()).toBe("mapped");
24+
expect(directive).toBe("//# sourceMappingURL=mapped.js.map\n");
25+
const map = JSON.parse(fs.readFileSync(`${__filename}.map`, "utf8"));
26+
expect(map.sources.some(source => source.endsWith("/mapped-original.js"))).toBe(
27+
true
28+
);
29+
});
30+
31+
it("should still extract JavaScript and CSS block comments", () => {
32+
expect(block()).toBe("block");
33+
const map = JSON.parse(fs.readFileSync(`${__filename}.map`, "utf8"));
34+
expect(map.sources.some(source => source.endsWith("/block-original.js"))).toBe(
35+
true
36+
);
37+
const cssMap = JSON.parse(
38+
fs.readFileSync(__filename.replace(/\.js$/, ".css.map"), "utf8")
39+
);
40+
expect(cssMap.sources.some(source => source.endsWith("/style-original.css"))).toBe(
41+
true
42+
);
43+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @returns {string} mapped module value
3+
*/
4+
export function mapped() {
5+
return "mapped";
6+
}
7+
export const directive = `//# sourceMappingURL=mapped.js.map
8+
`;
9+
//# sourceMappingURL=mapped.js.map
10+
// An ordinary trailing comment.

‎test/configCases/source-map/extract-source-map-strings/mapped.js.map‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default /[/*#sourceMappingURL=missing.map]*/

0 commit comments

Comments
 (0)