Merge pull request #15145 from markjm/markjm/context-module-hash

fix: context regex non-deterministic with |
This commit is contained in:
Tobias Koppers 2022-01-17 17:51:29 +01:00 committed by GitHub
commit 4ed40f9b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 15 deletions

View File

@ -149,12 +149,9 @@ class ContextModule extends Module {
this.resolveDependencies = undefined;
}
prettyRegExp(regexString) {
// remove the "/" at the front and the beginning
// "/foo/" -> "foo"
return regexString
.substring(1, regexString.length - 1)
.replace(/!/g, "%21");
_prettyRegExp(regexString, stripSlash = true) {
const str = (regexString + "").replace(/!/g, "%21").replace(/\|/g, "%7C");
return stripSlash ? str.substring(1, str.length - 1) : str;
}
_createIdentifier() {
@ -175,13 +172,19 @@ class ContextModule extends Module {
identifier += `|${this.options.addon}`;
}
if (this.options.regExp) {
identifier += `|${this.options.regExp}`;
identifier += `|${this._prettyRegExp(this.options.regExp, false)}`;
}
if (this.options.include) {
identifier += `|include: ${this.options.include}`;
identifier += `|include: ${this._prettyRegExp(
this.options.include,
false
)}`;
}
if (this.options.exclude) {
identifier += `|exclude: ${this.options.exclude}`;
identifier += `|exclude: ${this._prettyRegExp(
this.options.exclude,
false
)}`;
}
if (this.options.referencedExports) {
identifier += `|referencedExports: ${JSON.stringify(
@ -231,13 +234,13 @@ class ContextModule extends Module {
identifier += ` ${requestShortener.shorten(this.options.addon)}`;
}
if (this.options.regExp) {
identifier += ` ${this.prettyRegExp(this.options.regExp + "")}`;
identifier += ` ${this._prettyRegExp(this.options.regExp)}`;
}
if (this.options.include) {
identifier += ` include: ${this.prettyRegExp(this.options.include + "")}`;
identifier += ` include: ${this._prettyRegExp(this.options.include)}`;
}
if (this.options.exclude) {
identifier += ` exclude: ${this.prettyRegExp(this.options.exclude + "")}`;
identifier += ` exclude: ${this._prettyRegExp(this.options.exclude)}`;
}
if (this.options.referencedExports) {
identifier += ` referencedExports: ${this.options.referencedExports
@ -286,13 +289,13 @@ class ContextModule extends Module {
)}`;
}
if (this.options.regExp) {
identifier += ` ${this.prettyRegExp(this.options.regExp + "")}`;
identifier += ` ${this._prettyRegExp(this.options.regExp)}`;
}
if (this.options.include) {
identifier += ` include: ${this.prettyRegExp(this.options.include + "")}`;
identifier += ` include: ${this._prettyRegExp(this.options.include)}`;
}
if (this.options.exclude) {
identifier += ` exclude: ${this.prettyRegExp(this.options.exclude + "")}`;
identifier += ` exclude: ${this._prettyRegExp(this.options.exclude)}`;
}
if (this.options.referencedExports) {
identifier += ` referencedExports: ${this.options.referencedExports

View File

@ -0,0 +1,22 @@
"use strict";
const ContextModule = require("../lib/ContextModule");
describe("contextModule", () => {
let contextModule;
let request;
beforeEach(() => {
request = "/some/request";
});
describe("#identifier", () => {
it("returns an safe identifier for this module", () => {
contextModule = new ContextModule(() => {}, {
type: "javascript/auto",
request,
mode: "lazy",
regExp: /a|b/
});
expect(contextModule.identifier()).toContain("/a%7Cb/");
});
});
});