Skip to content

Commit 249fc5d

Browse files
authored
fix - Fix undefined use (#166)
Closes #165.
1 parent 88d9bc1 commit 249fc5d

File tree

2 files changed

+164
-3
lines changed

2 files changed

+164
-3
lines changed

src/index.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,19 @@ function mergeWithRule({
177177
});
178178
break;
179179
case CustomizeRule.Append:
180+
if (!bMatches.length) {
181+
ret[k] = v;
182+
183+
break;
184+
}
185+
180186
const appendValue = last(bMatches)[k];
181187

182188
if (!isArray(v) || !isArray(appendValue)) {
183189
throw new TypeError("Trying to append non-arrays");
184190
}
185191

186-
ret[k] =
187-
bMatches.length > 0 ? (v as Array<any>).concat(appendValue) : v;
192+
ret[k] = v.concat(appendValue);
188193
break;
189194
case CustomizeRule.Merge:
190195
const lastValue = last(bMatches)[k];
@@ -197,13 +202,19 @@ function mergeWithRule({
197202
ret[k] = { ...v, ...lastValue };
198203
break;
199204
case CustomizeRule.Prepend:
205+
if (!bMatches.length) {
206+
ret[k] = v;
207+
208+
break;
209+
}
210+
200211
const prependValue = last(bMatches)[k];
201212

202213
if (!isArray(v) || !isArray(prependValue)) {
203214
throw new TypeError("Trying to prepend non-arrays");
204215
}
205216

206-
ret[k] = bMatches.length > 0 ? prependValue.concat(v) : v;
217+
ret[k] = prependValue.concat(v);
207218
break;
208219
case CustomizeRule.Replace:
209220
ret[k] = bMatches.length > 0 ? last(bMatches)[k] : v;

test/merge-with-rules.test.ts

+150
Original file line numberDiff line numberDiff line change
@@ -893,4 +893,154 @@ describe("Merge with rules", function () {
893893

894894
expect(mergeRules(a, b)).toEqual(result);
895895
});
896+
897+
it("should append with local match (#165)", function () {
898+
const base = {
899+
module: {
900+
rules: [
901+
{
902+
test: /\.s(a|c)ss$/,
903+
use: [
904+
{
905+
loader: "css-loader",
906+
},
907+
{
908+
loader: "sass-loader",
909+
},
910+
],
911+
},
912+
{
913+
test: /\.(png|jpe?g|gif|svg)$/i,
914+
use: [
915+
{
916+
loader: "file-loader",
917+
},
918+
],
919+
},
920+
],
921+
},
922+
};
923+
const development = {
924+
module: {
925+
rules: [
926+
{
927+
test: /\.s(a|c)ss$/,
928+
use: ["style-loader"],
929+
},
930+
],
931+
},
932+
};
933+
const result = {
934+
module: {
935+
rules: [
936+
{
937+
test: /\.s(a|c)ss$/,
938+
use: [
939+
{
940+
loader: "css-loader",
941+
},
942+
{
943+
loader: "sass-loader",
944+
},
945+
"style-loader",
946+
],
947+
},
948+
{
949+
test: /\.(png|jpe?g|gif|svg)$/i,
950+
use: [
951+
{
952+
loader: "file-loader",
953+
},
954+
],
955+
},
956+
],
957+
},
958+
};
959+
960+
expect(
961+
mergeWithRules({
962+
module: {
963+
rules: {
964+
test: CustomizeRule.Match,
965+
use: CustomizeRule.Append,
966+
},
967+
},
968+
})(base, development)
969+
).toEqual(result);
970+
});
971+
972+
it("should prepend with local match (#165)", function () {
973+
const base = {
974+
module: {
975+
rules: [
976+
{
977+
test: /\.s(a|c)ss$/,
978+
use: [
979+
{
980+
loader: "css-loader",
981+
},
982+
{
983+
loader: "sass-loader",
984+
},
985+
],
986+
},
987+
{
988+
test: /\.(png|jpe?g|gif|svg)$/i,
989+
use: [
990+
{
991+
loader: "file-loader",
992+
},
993+
],
994+
},
995+
],
996+
},
997+
};
998+
const development = {
999+
module: {
1000+
rules: [
1001+
{
1002+
test: /\.s(a|c)ss$/,
1003+
use: ["style-loader"],
1004+
},
1005+
],
1006+
},
1007+
};
1008+
const result = {
1009+
module: {
1010+
rules: [
1011+
{
1012+
test: /\.s(a|c)ss$/,
1013+
use: [
1014+
"style-loader",
1015+
{
1016+
loader: "css-loader",
1017+
},
1018+
{
1019+
loader: "sass-loader",
1020+
},
1021+
],
1022+
},
1023+
{
1024+
test: /\.(png|jpe?g|gif|svg)$/i,
1025+
use: [
1026+
{
1027+
loader: "file-loader",
1028+
},
1029+
],
1030+
},
1031+
],
1032+
},
1033+
};
1034+
1035+
expect(
1036+
mergeWithRules({
1037+
module: {
1038+
rules: {
1039+
test: CustomizeRule.Match,
1040+
use: CustomizeRule.Prepend,
1041+
},
1042+
},
1043+
})(base, development)
1044+
).toEqual(result);
1045+
});
8961046
});

0 commit comments

Comments
 (0)