Skip to content

Commit

Permalink
Merge pull request #15722 from webpack/feat/issue-15720
Browse files Browse the repository at this point in the history
hoist regexp literals in SourceMapDerToolPlugin
  • Loading branch information
TheLarkInn committed Mar 13, 2023
2 parents 8f1b5ff + 5e31761 commit 9ca77a3
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions lib/SourceMapDevToolPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,32 @@ const validate = createSchemaValidation(
* @property {ItemCacheFacade} cacheItem cache item
*/

const METACHARACTERS_REGEXP = /[-[\]\\/{}()*+?.^$|]/g;
const CONTENT_HASH_DETECT_REGEXP = /\[contenthash(:\w+)?\]/;
const CSS_AND_JS_MODULE_EXTENSIONS_REGEXP = /\.((c|m)?js|css)($|\?)/i;
const CSS_EXTENSION_DETECT_REGEXP = /\.css($|\?)/i;
const MAP_URL_COMMENT_REGEXP = /\[map\]/g;
const URL_COMMENT_REGEXP = /\[url\]/g;
const URL_FORMATTING_REGEXP = /^\n\/\/(.*)$/;

/**
* Reset's .lastIndex of stateful Regular Expressions
* For when `test` or `exec` is called on them
* @param {RegExp} regexp Stateful Regular Expression to be reset
* @returns {void}
*
*/
const resetRegexpState = regexp => {
regexp.lastIndex = -1;
};

/**
* Escapes regular expression metacharacters
* @param {string} str String to quote
* @returns {string} Escaped string
*/
const quoteMeta = str => {
return str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
return str.replace(METACHARACTERS_REGEXP, "\\$&");
};

/**
Expand Down Expand Up @@ -152,7 +171,7 @@ class SourceMapDevToolPlugin {
const fallbackModuleFilenameTemplate = this.fallbackModuleFilenameTemplate;
const requestShortener = compiler.requestShortener;
const options = this.options;
options.test = options.test || /\.((c|m)?js|css)($|\?)/i;
options.test = options.test || CSS_AND_JS_MODULE_EXTENSIONS_REGEXP;

const matchObject = ModuleFilenameHelpers.matchObject.bind(
undefined,
Expand Down Expand Up @@ -409,7 +428,9 @@ class SourceMapDevToolPlugin {
sourceMap.file = file;
const usesContentHash =
sourceMapFilename &&
/\[contenthash(:\w+)?\]/.test(sourceMapFilename);
CONTENT_HASH_DETECT_REGEXP.test(sourceMapFilename);

resetRegexpState(CONTENT_HASH_DETECT_REGEXP);

// If SourceMap and asset uses contenthash, avoid a circular dependency by hiding hash in `file`
if (usesContentHash && task.assetInfo.contenthash) {
Expand All @@ -428,13 +449,16 @@ class SourceMapDevToolPlugin {

/** @type {string | false} */
let currentSourceMappingURLComment = sourceMappingURLComment;
let cssExtensionDetected =
CSS_EXTENSION_DETECT_REGEXP.test(file);
resetRegexpState(CSS_EXTENSION_DETECT_REGEXP);
if (
currentSourceMappingURLComment !== false &&
/\.css($|\?)/i.test(file)
cssExtensionDetected
) {
currentSourceMappingURLComment =
currentSourceMappingURLComment.replace(
/^\n\/\/(.*)$/,
URL_FORMATTING_REGEXP,
"\n/*$1*/"
);
}
Expand Down Expand Up @@ -516,9 +540,9 @@ class SourceMapDevToolPlugin {
const asset = new ConcatSource(
new RawSource(source),
currentSourceMappingURLComment
.replace(/\[map\]/g, () => sourceMapString)
.replace(MAP_URL_COMMENT_REGEXP, () => sourceMapString)
.replace(
/\[url\]/g,
URL_COMMENT_REGEXP,
() =>
`data:application/json;charset=utf-8;base64,${Buffer.from(
sourceMapString,
Expand Down

0 comments on commit 9ca77a3

Please sign in to comment.