From 247ba9a53da80525521d1a4f67fca7638d1a2ce1 Mon Sep 17 00:00:00 2001 From: Jamie Bertram Date: Wed, 10 Nov 2021 09:38:09 -0500 Subject: [PATCH] Override rules --- test/webpack.config.spec.js | 107 ++++++++++++++++++++++++++++++++++++ webpack.config.js | 13 ++++- 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/test/webpack.config.spec.js b/test/webpack.config.spec.js index 672b85c..03414b3 100644 --- a/test/webpack.config.spec.js +++ b/test/webpack.config.spec.js @@ -560,4 +560,111 @@ describe("webpackConfig", () => { } `); }); + + it("Overrides matching rules", () => { + expect( + webpackConfig((env) => ({ + module: { + rules: [{ test: /\.css$/, loader: "css-loader" }], + }, + }))("production") + ).toMatchInlineSnapshot(` + Object { + "devtool": false, + "mode": "production", + "module": Object { + "rules": Array [ + Object { + "loader": "ts-loader", + "test": /\\\\\\.tsx\\?\\$/, + }, + Object { + "test": /\\\\\\.\\(png\\|svg\\|jpg\\|jpeg\\|gif\\|woff\\|woff2\\|eot\\|ttf\\|otf\\)\\$/i, + "type": "asset/resource", + }, + Object { + "loader": "css-loader", + "test": /\\\\\\.css\\$/, + }, + ], + }, + "optimization": Object { + "runtimeChunk": "single", + "splitChunks": Object { + "cacheGroups": Object { + "vendor": Object { + "chunks": "all", + "name": "vendors", + "test": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/, + }, + }, + }, + }, + "output": Object { + "filename": "[name].[contenthash].js", + "pathinfo": true, + }, + "plugins": Array [ + MiniCssExtractPlugin { + "_sortedModulesCache": WeakMap {}, + "options": Object { + "chunkFilename": "[id].css", + "experimentalUseImportModule": undefined, + "filename": "[name]-[chunkhash].css", + "ignoreOrder": false, + "runtime": true, + }, + "runtimeOptions": Object { + "attributes": undefined, + "insert": undefined, + "linkType": "text/css", + }, + }, + ErrorReportingPlugin {}, + DefinePlugin { + "definitions": Object { + "NODE_ENV": "production", + }, + }, + CleanWebpackPlugin { + "apply": [Function], + "cleanAfterEveryBuildPatterns": Array [], + "cleanOnceBeforeBuildPatterns": Array [ + "**/*", + ], + "cleanStaleWebpackAssets": true, + "currentAssets": Array [], + "dangerouslyAllowCleanPatternsOutsideProject": false, + "dry": false, + "handleDone": [Function], + "handleInitial": [Function], + "initialClean": false, + "outputPath": "", + "protectWebpackAssets": true, + "removeFiles": [Function], + "verbose": false, + }, + ], + "resolve": Object { + "extensions": Array [ + ".purs", + ".js", + ".ts", + ".tsx", + ], + "modules": Array [ + "node_modules", + ], + }, + "resolveLoader": Object { + "modules": Array [ + "node_modules", + ], + }, + "stats": Object { + "children": false, + }, + } + `); + }); }); diff --git a/webpack.config.js b/webpack.config.js index 3c76995..b788f8b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -115,7 +115,7 @@ const productionConfig = { plugins: [new ErrorReportingPlugin()], }; -function isNotOverridenBy(plugins) { +function pluginNotOverridden(plugins) { return (plugin) => !plugins.some( (p) => @@ -125,10 +125,19 @@ function isNotOverridenBy(plugins) { ); } +function ruleNotOverridden(rules) { + return (rule) => + !rules.some( + (r) => r.test && rule.test && r.test.toString() == rule.test.toString() + ); +} + const merge = mergeWithCustomize({ customizeArray: (a, b, key) => { if (key === "plugins") { - return a.filter(isNotOverridenBy(b)).concat(b); + return a.filter(pluginNotOverridden(b)).concat(b); + } else if (key === "module.rules") { + return a.filter(ruleNotOverridden(b)).concat(b); } return undefined; },