Skip to content

Commit 426aad3

Browse files
committed
fix(dependencies): keep context matcher valid under Unicode flags
Preserving the user's `wrappedContextRegExp` flags means `u`/`v` can now reach the rebuilt matcher. `quoteMeta` escaped `-` as `\-`, which is an invalid escape outside a character class in Unicode mode, so a request with a literal `-` in its static prefix or postfix (for example importing a template literal like ./dir/module-${x}.js) threw and aborted the build. `-` is a literal outside `[...]` and the quoted string is only ever concatenated between anchors, so leave it unescaped. Covers both the wrapped-context (template-literal) and wrapped-expression (concatenation) paths with Unicode-mode, hyphen-in-prefix cases. Signed-off-by: Eshiv Pandey <[email protected]>
1 parent f3e01c0 commit 426aad3

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎lib/dependencies/context/ContextDependencyHelpers.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ const { parseResource } = require("../../util/identifier");
2323
*/
2424

2525
/**
26-
* Escapes regular expression metacharacters
26+
* Escapes regular expression metacharacters.
27+
*
28+
* `-` is intentionally left unescaped: outside a character class it is a
29+
* literal, and the quoted string is only ever concatenated between anchors
30+
* (never inside `[...]`). Escaping it as `\-` would also be an invalid escape
31+
* under the `u`/`v` flags, which `getWrappedContextFlags` now preserves — so
32+
* e.g. `` import(`./dir/module-${x}.js`) `` with a Unicode `wrappedContextRegExp`
33+
* would throw when the matcher is rebuilt.
2734
* @param {string} str String to quote
2835
* @returns {string} Escaped string
2936
*/
30-
const quoteMeta = (str) => str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
37+
const quoteMeta = (str) => str.replace(/[[\]\\/{}()*+?.^$|]/g, "\\$&");
3138

3239
/**
3340
* Returns the flags of a wrapped context RegExp, minus the stateful `g`/`y`

‎test/configCases/context-modules/wrapped-context-regexp-flags/index.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,17 @@ it("should keep the `i` flag for template-literal context requests", async () =>
1010
const name = "module-a.js";
1111
expect((await import(`./dir/${name}`)).default).toBe("a");
1212
});
13+
14+
it("should build a valid Unicode-mode matcher for concatenation requests with a hyphen in the prefix", async () => {
15+
const suffix = "a.js";
16+
// The static prefix `./dir/module-` contains a `-`. With the `u` flag
17+
// preserved, quoting it as `\-` would be an invalid escape and rebuilding
18+
// the wrapped-expression matcher would throw, failing the build.
19+
expect((await import("./dir/module-" + suffix)).default).toBe("a");
20+
});
21+
22+
it("should build a valid Unicode-mode matcher for template-literal requests with a hyphen in the prefix", async () => {
23+
const suffix = "a.js";
24+
// Same for the wrapped-context (template-literal) path.
25+
expect((await import(`./dir/module-${suffix}`)).default).toBe("a");
26+
});

‎test/configCases/context-modules/wrapped-context-regexp-flags/webpack.config.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ module.exports = {
55
module: {
66
parser: {
77
javascript: {
8-
// Case-insensitive: `.js` files only match `\.JS` when the `i`
9-
// flag survives into the rebuilt context RegExp.
10-
wrappedContextRegExp: /.*\.JS/i
8+
// Case-insensitive so `.js` files only match `\.JS` when the `i`
9+
// flag survives into the rebuilt context RegExp. The `u` flag must
10+
// survive too, and a literal `-` in the request prefix/postfix must
11+
// stay a valid (unescaped) pattern under Unicode mode.
12+
wrappedContextRegExp: /.*\.JS/iu
1113
}
1214
}
1315
}

0 commit comments

Comments
 (0)