Merge pull request #11927 from jasonwilliams/escape_brackets

Escape brackets in the resource name so they don't get removed during…
This commit is contained in:
Tobias Koppers 2020-11-12 00:03:31 +01:00 committed by GitHub
commit f58831ee0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 12 deletions

View File

@ -9,6 +9,7 @@ const createHash = require("./util/createHash");
const ModuleFilenameHelpers = exports;
// TODO webpack 6: consider removing these
ModuleFilenameHelpers.ALL_LOADERS_RESOURCE = "[all-loaders][resource]";
ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE = /\[all-?loaders\]\[resource\]/gi;
ModuleFilenameHelpers.LOADERS_RESOURCE = "[loaders][resource]";
@ -57,6 +58,8 @@ const asRegExp = test => {
return test;
};
const REGEXP = /\[\\*([\w-]+)\\*\]/gi;
ModuleFilenameHelpers.createFilename = (
module,
options,
@ -92,6 +95,7 @@ ModuleFilenameHelpers.createFilename = (
hash = getHash(identifier);
}
const resource = shortIdentifier.split("!").pop();
const loaders = getBefore(shortIdentifier, "!");
const allLoaders = getBefore(identifier, "!");
const query = getAfter(resource, "?");
@ -110,21 +114,58 @@ ModuleFilenameHelpers.createFilename = (
namespace: opts.namespace
});
}
// TODO webpack 6: consider removing alternatives without dashes
/** @type {Map<string, string>} */
const replacements = new Map([
["identifier", identifier],
["short-identifier", shortIdentifier],
["resource", resource],
["resource-path", resourcePath],
// cSpell:words resourcepath
["resourcepath", resourcePath],
["absolute-resource-path", absoluteResourcePath],
["abs-resource-path", absoluteResourcePath],
// cSpell:words absoluteresource
["absoluteresource-path", absoluteResourcePath],
// cSpell:words absresource
["absresource-path", absoluteResourcePath],
// cSpell:words resourcepath
["absolute-resourcepath", absoluteResourcePath],
// cSpell:words resourcepath
["abs-resourcepath", absoluteResourcePath],
// cSpell:words absoluteresourcepath
["absoluteresourcepath", absoluteResourcePath],
// cSpell:words absresourcepath
["absresourcepath", absoluteResourcePath],
["all-loaders", allLoaders],
// cSpell:words allloaders
["allloaders", allLoaders],
["loaders", loaders],
["query", query],
["id", moduleId],
["hash", hash],
["namespace", opts.namespace]
]);
// TODO webpack 6: consider removing weird double placeholders
return opts.moduleFilenameTemplate
.replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, identifier)
.replace(ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE, shortIdentifier)
.replace(ModuleFilenameHelpers.REGEXP_RESOURCE, resource)
.replace(ModuleFilenameHelpers.REGEXP_RESOURCE_PATH, resourcePath)
.replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS_RESOURCE, "[identifier]")
.replace(
ModuleFilenameHelpers.REGEXP_ABSOLUTE_RESOURCE_PATH,
absoluteResourcePath
ModuleFilenameHelpers.REGEXP_LOADERS_RESOURCE,
"[short-identifier]"
)
.replace(ModuleFilenameHelpers.REGEXP_ALL_LOADERS, allLoaders)
.replace(ModuleFilenameHelpers.REGEXP_LOADERS, loaders)
.replace(ModuleFilenameHelpers.REGEXP_QUERY, query)
.replace(ModuleFilenameHelpers.REGEXP_ID, moduleId)
.replace(ModuleFilenameHelpers.REGEXP_HASH, hash)
.replace(ModuleFilenameHelpers.REGEXP_NAMESPACE, opts.namespace);
.replace(REGEXP, (match, content) => {
if (content.length + 2 === match.length) {
const replacement = replacements.get(content.toLowerCase());
if (replacement !== undefined) {
return replacement;
}
} else if (match.startsWith("[\\") && match.endsWith("\\]")) {
return `[${match.slice(2, -2)}]`;
}
return match;
});
};
ModuleFilenameHelpers.replaceDuplicates = (array, fn, comparator) => {

View File

@ -0,0 +1,3 @@
var foo = {};
module.exports = foo;

View File

@ -0,0 +1,8 @@
it("should include [id].js in SourceMap", function () {
var fs = require("fs");
var source = fs.readFileSync(__filename + ".map", "utf-8");
var map = JSON.parse(source);
expect(map.sources).toContain("webpack:///./[id].js");
});
if (Math.random() < 0) require("./[id].js");

View File

@ -0,0 +1,11 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
node: {
__dirname: false,
__filename: false
},
devtool: "source-map",
optimization: {
minimize: true
}
};