Skip to content

Commit 247ba9a

Browse files
author
Jamie Bertram
committed
Override rules
1 parent 5cb7406 commit 247ba9a

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

test/webpack.config.spec.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,4 +560,111 @@ describe("webpackConfig", () => {
560560
}
561561
`);
562562
});
563+
564+
it("Overrides matching rules", () => {
565+
expect(
566+
webpackConfig((env) => ({
567+
module: {
568+
rules: [{ test: /\.css$/, loader: "css-loader" }],
569+
},
570+
}))("production")
571+
).toMatchInlineSnapshot(`
572+
Object {
573+
"devtool": false,
574+
"mode": "production",
575+
"module": Object {
576+
"rules": Array [
577+
Object {
578+
"loader": "ts-loader",
579+
"test": /\\\\\\.tsx\\?\\$/,
580+
},
581+
Object {
582+
"test": /\\\\\\.\\(png\\|svg\\|jpg\\|jpeg\\|gif\\|woff\\|woff2\\|eot\\|ttf\\|otf\\)\\$/i,
583+
"type": "asset/resource",
584+
},
585+
Object {
586+
"loader": "css-loader",
587+
"test": /\\\\\\.css\\$/,
588+
},
589+
],
590+
},
591+
"optimization": Object {
592+
"runtimeChunk": "single",
593+
"splitChunks": Object {
594+
"cacheGroups": Object {
595+
"vendor": Object {
596+
"chunks": "all",
597+
"name": "vendors",
598+
"test": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
599+
},
600+
},
601+
},
602+
},
603+
"output": Object {
604+
"filename": "[name].[contenthash].js",
605+
"pathinfo": true,
606+
},
607+
"plugins": Array [
608+
MiniCssExtractPlugin {
609+
"_sortedModulesCache": WeakMap {},
610+
"options": Object {
611+
"chunkFilename": "[id].css",
612+
"experimentalUseImportModule": undefined,
613+
"filename": "[name]-[chunkhash].css",
614+
"ignoreOrder": false,
615+
"runtime": true,
616+
},
617+
"runtimeOptions": Object {
618+
"attributes": undefined,
619+
"insert": undefined,
620+
"linkType": "text/css",
621+
},
622+
},
623+
ErrorReportingPlugin {},
624+
DefinePlugin {
625+
"definitions": Object {
626+
"NODE_ENV": "production",
627+
},
628+
},
629+
CleanWebpackPlugin {
630+
"apply": [Function],
631+
"cleanAfterEveryBuildPatterns": Array [],
632+
"cleanOnceBeforeBuildPatterns": Array [
633+
"**/*",
634+
],
635+
"cleanStaleWebpackAssets": true,
636+
"currentAssets": Array [],
637+
"dangerouslyAllowCleanPatternsOutsideProject": false,
638+
"dry": false,
639+
"handleDone": [Function],
640+
"handleInitial": [Function],
641+
"initialClean": false,
642+
"outputPath": "",
643+
"protectWebpackAssets": true,
644+
"removeFiles": [Function],
645+
"verbose": false,
646+
},
647+
],
648+
"resolve": Object {
649+
"extensions": Array [
650+
".purs",
651+
".js",
652+
".ts",
653+
".tsx",
654+
],
655+
"modules": Array [
656+
"node_modules",
657+
],
658+
},
659+
"resolveLoader": Object {
660+
"modules": Array [
661+
"node_modules",
662+
],
663+
},
664+
"stats": Object {
665+
"children": false,
666+
},
667+
}
668+
`);
669+
});
563670
});

webpack.config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const productionConfig = {
115115
plugins: [new ErrorReportingPlugin()],
116116
};
117117

118-
function isNotOverridenBy(plugins) {
118+
function pluginNotOverridden(plugins) {
119119
return (plugin) =>
120120
!plugins.some(
121121
(p) =>
@@ -125,10 +125,19 @@ function isNotOverridenBy(plugins) {
125125
);
126126
}
127127

128+
function ruleNotOverridden(rules) {
129+
return (rule) =>
130+
!rules.some(
131+
(r) => r.test && rule.test && r.test.toString() == rule.test.toString()
132+
);
133+
}
134+
128135
const merge = mergeWithCustomize({
129136
customizeArray: (a, b, key) => {
130137
if (key === "plugins") {
131-
return a.filter(isNotOverridenBy(b)).concat(b);
138+
return a.filter(pluginNotOverridden(b)).concat(b);
139+
} else if (key === "module.rules") {
140+
return a.filter(ruleNotOverridden(b)).concat(b);
132141
}
133142
return undefined;
134143
},

0 commit comments

Comments
 (0)