Skip to content

Commit

Permalink
match resource should not include special .webpack[...] extension
Browse files Browse the repository at this point in the history
`.webpack[]` should not be used for resource path
  • Loading branch information
sokra committed Sep 3, 2021
1 parent eca903a commit 765101b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
11 changes: 7 additions & 4 deletions lib/NormalModuleFactory.js
Expand Up @@ -540,15 +540,18 @@ class NormalModuleFactory extends ModuleFactory {
for (const loader of preLoaders) allLoaders.push(loader);
let type = settings.type;
if (!type) {
const resource =
(matchResourceData && matchResourceData.resource) ||
resourceData.resource;
let resource;
let match;
if (
typeof resource === "string" &&
matchResourceData &&
typeof (resource = matchResourceData.resource) === "string" &&
(match = /\.webpack\[([^\]]+)\]$/.exec(resource))
) {
type = match[1];
matchResourceData.resource = matchResourceData.resource.slice(
0,
-type.length - 10
);
} else {
type = "javascript/auto";
}
Expand Down
42 changes: 25 additions & 17 deletions test/configCases/loader-import-module/css/webpack.config.js
Expand Up @@ -9,12 +9,15 @@ module.exports = {
url: "relative"
}
},
generator: {
asset: {
filename: "assets/[name][ext][query]"
}
},
rules: [
{
dependency: "url",
issuer: /stylesheet\.js$/,
type: "asset/resource",
generator: {
filename: "assets/[name][ext][query]"
}
},
{
oneOf: [
{
Expand Down Expand Up @@ -44,18 +47,23 @@ module.exports = {
plugins: [
compiler =>
compiler.hooks.done.tap("test case", stats => {
expect(stats.compilation.getAsset("assets/file.png")).toHaveProperty(
"info",
expect.objectContaining({ sourceFilename: "file.png" })
);
expect(stats.compilation.getAsset("assets/file.jpg")).toHaveProperty(
"info",
expect.objectContaining({ sourceFilename: "file.jpg" })
);
const { auxiliaryFiles } = stats.compilation.namedChunks.get("main");
expect(auxiliaryFiles).toContain("assets/file.png");
expect(auxiliaryFiles).toContain("assets/file.png?1");
expect(auxiliaryFiles).toContain("assets/file.jpg");
try {
expect(stats.compilation.getAsset("assets/file.png")).toHaveProperty(
"info",
expect.objectContaining({ sourceFilename: "file.png" })
);
expect(stats.compilation.getAsset("assets/file.jpg")).toHaveProperty(
"info",
expect.objectContaining({ sourceFilename: "file.jpg" })
);
const { auxiliaryFiles } = stats.compilation.namedChunks.get("main");
expect(auxiliaryFiles).toContain("assets/file.png");
expect(auxiliaryFiles).toContain("assets/file.png?1");
expect(auxiliaryFiles).toContain("assets/file.jpg");
} catch (e) {
console.log(stats.toString({ colors: true, orphanModules: true }));
throw e;
}
})
]
};

0 comments on commit 765101b

Please sign in to comment.