Skip to content

Commit

Permalink
Override rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jhbertra committed Nov 10, 2021
1 parent 5cb7406 commit 247ba9a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
107 changes: 107 additions & 0 deletions test/webpack.config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
`);
});
});
13 changes: 11 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const productionConfig = {
plugins: [new ErrorReportingPlugin()],
};

function isNotOverridenBy(plugins) {
function pluginNotOverridden(plugins) {
return (plugin) =>
!plugins.some(
(p) =>
Expand All @@ -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;
},
Expand Down

0 comments on commit 247ba9a

Please sign in to comment.