From 7e8bd569706fbb7055a124757e3091cf9d9b7866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juho=20Veps=C3=A4l=C3=A4inen?= Date: Wed, 16 Dec 2020 15:04:18 +0100 Subject: [PATCH] fix: Fix rule merging without a match (#168) Closes #167. --- src/index.ts | 6 +++ test/merge-with-rules.test.ts | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/index.ts b/src/index.ts index a23a51c..58413d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -192,6 +192,12 @@ function mergeWithRule({ ret[k] = v.concat(appendValue); break; case CustomizeRule.Merge: + if (!bMatches.length) { + ret[k] = v; + + break; + } + const lastValue = last(bMatches)[k]; if (!isPlainObject(v) || !isPlainObject(lastValue)) { diff --git a/test/merge-with-rules.test.ts b/test/merge-with-rules.test.ts index 5b7ac10..8292bbf 100644 --- a/test/merge-with-rules.test.ts +++ b/test/merge-with-rules.test.ts @@ -1043,4 +1043,78 @@ describe("Merge with rules", function () { })(base, development) ).toEqual(result); }); + + it("should fall back to default behavior without a match when merging (#167)", function () { + const conf1 = { + module: { + rules: [ + { + test: "/\\.scss$|\\.sass$/", + use: [ + { + loader: "sass-loader", + options: { + sourceMap: true, + }, + }, + ], + }, + ], + }, + }; + const conf2 = { + module: { + rules: [ + { + test: "/\\.scss$|\\.sass$/", + use: [ + { + loader: "sass-resources-loader", + options: { + resources: ["src/styles/includes.scss"], + }, + }, + ], + }, + ], + }, + }; + const result = { + module: { + rules: [ + { + test: "/\\.scss$|\\.sass$/", + use: [ + { + loader: "sass-loader", + options: { + sourceMap: true, + }, + }, + { + loader: "sass-resources-loader", + options: { + resources: ["src/styles/includes.scss"], + }, + }, + ], + }, + ], + }, + }; + + expect( + mergeWithRules({ + module: { + rules: { + test: CustomizeRule.Match, + use: { + loader: CustomizeRule.Match, + options: CustomizeRule.Merge, + }, + }, + }, + })(conf1, conf2) + ).toEqual(result); + }); });