diff --git a/lib/ContextModule.js b/lib/ContextModule.js index e9c167217..145a7018e 100644 --- a/lib/ContextModule.js +++ b/lib/ContextModule.js @@ -144,7 +144,9 @@ class ContextModule extends Module { prettyRegExp(regexString) { // remove the "/" at the front and the beginning // "/foo/" -> "foo" - return regexString.substring(1, regexString.length - 1); + return regexString + .substring(1, regexString.length - 1) + .replace(/!/g, "%21"); } _createIdentifier() { diff --git a/test/cases/context/issue-10969/folder/file1.js b/test/cases/context/issue-10969/folder/file1.js new file mode 100644 index 000000000..e69de29bb diff --git a/test/cases/context/issue-10969/index.js b/test/cases/context/issue-10969/index.js new file mode 100644 index 000000000..b0403cb8c --- /dev/null +++ b/test/cases/context/issue-10969/index.js @@ -0,0 +1,21 @@ +expect.extend({ + toBeValidModuleId(received, moduleIdString) { + const pass = typeof received === "number" || received === moduleIdString; + if (pass) { + return { + message: () => `expected ${received} not to be a valid module id`, + pass: true + }; + } else { + return { + message: () => `expected ${received} to be a valid module id`, + pass: false + }; + } + } +}); + +it("should replace ! with %21 in the module id string of the context module", function () { + const moduleId = require.context("./folder", true, /^(?!file1\.js$).*$/i, "lazy").id; + expect(moduleId).toBeValidModuleId("./context/issue-10969/folder lazy recursive ^(?%21file1\\.js$).*$/"); +});