Skip to content

Commit c1e962b

Browse files
committed
fix(dependencies): preserve wrappedContextRegExp flags in context matcher
The wrapped-request branches of ContextDependencyHelpers rebuilt the matcher from wrappedContextRegExp.source only, dropping the user's i/m/s/u flags, while the fallback path passed the RegExp object intact. So module.parser.javascript.wrappedContextRegExp: /\.jsx?$/i silently became case-sensitive. Reuse the flags (minus stateful g/y) when rebuilding the RegExp. Signed-off-by: Eshiv Pandey <[email protected]>
1 parent 8735b62 commit c1e962b

5 files changed

Lines changed: 49 additions & 2 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 `wrappedContextRegExp` flags when rebuilding context request matchers.

‎lib/dependencies/ContextDependencyHelpers.js‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ const { parseResource } = require("../util/identifier");
2929
*/
3030
const quoteMeta = (str) => str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
3131

32+
/**
33+
* Returns the flags of a wrapped context RegExp, minus the stateful `g`/`y`
34+
* flags, so the rebuilt matcher keeps `i`/`m`/`s`/`u` but stays safe to reuse
35+
* across ContextModuleFactory's per-candidate `regExp.test()` calls.
36+
* @param {RegExp} regExp the user's wrappedContextRegExp
37+
* @returns {string} flags to apply to the rebuilt RegExp
38+
*/
39+
const getWrappedContextFlags = (regExp) => regExp.flags.replace(/[gy]/g, "");
40+
3241
/**
3342
* Split context from prefix.
3443
* @param {string} prefix prefix
@@ -111,7 +120,10 @@ module.exports.create = (
111120
// expressions the gaps between them, so `./context/pre${e}inner${e}post`
112121
// matches `/^\.\/pre.*inner.*post$/` under the context `./context`.
113122
const regExp = new RegExp(
114-
`^${quoteMeta(prefix)}${innerRegExp}${quoteMeta(postfix)}$`
123+
`^${quoteMeta(prefix)}${innerRegExp}${quoteMeta(postfix)}$`,
124+
getWrappedContextFlags(
125+
/** @type {RegExp} */ (options.wrappedContextRegExp)
126+
)
115127
);
116128
const dep = new Dep(
117129
{
@@ -209,7 +221,10 @@ module.exports.create = (
209221
const regExp = new RegExp(
210222
`^${quoteMeta(prefix)}${
211223
/** @type {RegExp} */ (options.wrappedContextRegExp).source
212-
}${quoteMeta(postfix)}$`
224+
}${quoteMeta(postfix)}$`,
225+
getWrappedContextFlags(
226+
/** @type {RegExp} */ (options.wrappedContextRegExp)
227+
)
213228
);
214229
const dep = new Dep(
215230
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "a";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
it("should keep the `i` flag for concatenation context requests", async () => {
2+
const name = "module-a.js";
3+
// `import("./dir/" + name)` builds a wrapped context whose matcher is
4+
// rebuilt from `wrappedContextRegExp`; dropping its flags would exclude
5+
// `module-a.js` from the case-insensitive `/.*\.JS/i`.
6+
expect((await import("./dir/" + name)).default).toBe("a");
7+
});
8+
9+
it("should keep the `i` flag for template-literal context requests", async () => {
10+
const name = "module-a.js";
11+
expect((await import(`./dir/${name}`)).default).toBe("a");
12+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
module: {
6+
parser: {
7+
javascript: {
8+
// Case-insensitive: `.js` files only match `\.JS` when the `i`
9+
// flag survives into the rebuilt context RegExp.
10+
wrappedContextRegExp: /.*\.JS/i
11+
}
12+
}
13+
}
14+
};

0 commit comments

Comments
 (0)