Move escaping to getReplacer and update un-escaping

This commit is contained in:
JJ Kasper 2019-07-23 10:42:36 -05:00
parent b7a7c9de29
commit b72a308a08
No known key found for this signature in database
GPG Key ID: 6CFC4C3DAD69C3E4
1 changed files with 8 additions and 18 deletions

View File

@ -47,7 +47,7 @@ const getReplacer = (value, allowEmpty) => {
}
return "";
} else {
return `${value}`;
return `${escapePathVariables(value)}`;
}
};
return fn;
@ -55,17 +55,14 @@ const getReplacer = (value, allowEmpty) => {
const escapePathVariables = value => {
return typeof value === "string"
? value.replace(
/\[(\\*[\w:]+\\*)\]/gi,
"[\\$1\\]"
)
? value.replace(/\[(\\*[\w:]+\\*)\]/gi, "[\\$1\\]")
: value;
};
const replacePathVariables = (path, data) => {
const chunk = data.chunk;
let chunkId = chunk && chunk.id;
let chunkName = chunk && (chunk.name || chunk.id);
const chunkId = chunk && chunk.id;
const chunkName = chunk && (chunk.name || chunk.id);
const chunkHash = chunk && (chunk.renderedHash || chunk.hash);
const chunkHashWithLength = chunk && chunk.hashWithLength;
const contentHashType = data.contentHashType;
@ -78,7 +75,7 @@ const replacePathVariables = (path, data) => {
chunk.contentHashWithLength[contentHashType]) ||
data.contentHashWithLength;
const module = data.module;
let moduleId = module && module.id;
const moduleId = module && module.id;
const moduleHash = module && (module.renderedHash || module.hash);
const moduleHashWithLength = module && module.hashWithLength;
@ -96,12 +93,6 @@ const replacePathVariables = (path, data) => {
);
}
// we need to escape path variables e.g. [name] in replaced values
// to prevent unexpected extra replaces from a filename having the variable
chunkId = escapePathVariables(chunkId);
moduleId = escapePathVariables(moduleId);
chunkName = escapePathVariables(chunkName);
return (
path
.replace(
@ -123,14 +114,13 @@ const replacePathVariables = (path, data) => {
.replace(REGEXP_ID, getReplacer(chunkId))
.replace(REGEXP_MODULEID, getReplacer(moduleId))
.replace(REGEXP_NAME, getReplacer(chunkName))
.replace(REGEXP_FILE, getReplacer(escapePathVariables(data.filename)))
.replace(REGEXP_FILEBASE, getReplacer(escapePathVariables(data.basename)))
.replace(REGEXP_FILE, getReplacer(data.filename))
.replace(REGEXP_FILEBASE, getReplacer(data.basename))
// query is optional, it's OK if it's in a path but there's nothing to replace it with
.replace(REGEXP_QUERY, getReplacer(data.query, true))
// only available in sourceMappingURLComment
.replace(REGEXP_URL, getReplacer(data.url))
.replace(/\[\\/g, "[")
.replace(/\\\]/g, "]")
.replace(/\[\\(\\*[\w:]+\\*)\\\]/gi, "[$1]")
);
};