Skip to content

Commit

Permalink
fix: oneOf rule has been picked multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiaojx committed Nov 15, 2022
1 parent 8241da7 commit 2719ecc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/NormalModuleFactory.js
Expand Up @@ -509,6 +509,11 @@ class NormalModuleFactory extends ModuleFactory {
issuerLayer: contextInfo.issuerLayer || ""
});
for (const r of result) {
// https://github.com/webpack/webpack/issues/16466
// if a request exists PrePostAutoLoaders, should disable modifying Rule.type
if (r.type === "type" && noPrePostAutoLoaders) {
continue;
}
if (r.type === "use") {
if (!noAutoLoaders && !noPrePostAutoLoaders) {
useLoaders.push(r.value);
Expand Down
4 changes: 4 additions & 0 deletions test/configCases/rule-set/oneOf/css-loader.js
@@ -0,0 +1,4 @@
/** @type {import("../../../../").LoaderDefinition<{ get(): string }>} */
module.exports = function (source) {
return "module.exports='__css__'"
}
3 changes: 3 additions & 0 deletions test/configCases/rule-set/oneOf/index.css
@@ -0,0 +1,3 @@
body {
color: red;
}
4 changes: 4 additions & 0 deletions test/configCases/rule-set/oneOf/index.js
@@ -0,0 +1,4 @@
it("should return the content processed by css-loader instead of asset/resource", function () {
var a1 = require("./index.css");
expect(a1).toEqual("__css__");
});
16 changes: 16 additions & 0 deletions test/configCases/rule-set/oneOf/style-loader.js
@@ -0,0 +1,16 @@
/** @type {import("../../../../types").LoaderDefinition<{ get(): string }>} */
module.exports.pitch = function (request) {
return `
var content = require(${stringifyRequest(this, `!!${request}`)});
module.exports = content;
`
};

function stringifyRequest(loaderContext, request) {
return JSON.stringify(
loaderContext.utils.contextify(
loaderContext.context || loaderContext.rootContext,
request
)
)
}
24 changes: 24 additions & 0 deletions test/configCases/rule-set/oneOf/webpack.config.js
@@ -0,0 +1,24 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
output: {
assetModuleFilename: "[name][ext]"
},
module: {
rules: [
{
test: /\.css$/,
oneOf: [
{
use: ["./style-loader", "./css-loader"],
issuer: /\.(js)$/
},
{
type: "asset/resource",
issuer: /\.(css|scss|sass)$/
}
]
}
]
}
};

0 comments on commit 2719ecc

Please sign in to comment.