Skip to content

Commit

Permalink
feat: Throw if trying to append/prepend non-arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
bebraw committed Dec 12, 2020
1 parent 157b9a0 commit aa9fd96
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,14 @@ function mergeWithRule({
});
break;
case CustomizeRule.Append:
const appendValue = last(bMatches)[k];

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

ret[k] =
bMatches.length > 0
? (v as Array<any>).concat(last(bMatches)[k])
: v;
bMatches.length > 0 ? (v as Array<any>).concat(appendValue) : v;
break;
case CustomizeRule.Merge:
const lastValue = last(bMatches)[k];
Expand All @@ -193,7 +197,13 @@ function mergeWithRule({
ret[k] = { ...v, ...lastValue };
break;
case CustomizeRule.Prepend:
ret[k] = bMatches.length > 0 ? last(bMatches)[k].concat(v) : v;
const prependValue = last(bMatches)[k];

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

ret[k] = bMatches.length > 0 ? prependValue.concat(v) : v;
break;
case CustomizeRule.Replace:
ret[k] = bMatches.length > 0 ? last(bMatches)[k] : v;
Expand Down
98 changes: 98 additions & 0 deletions test/merge-with-rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,104 @@ describe("Merge with rules", function () {
});
});

it("should throw if trying to append non-arrays", function () {
const _mergeWithExplicitRule = mergeWithRules({
module: {
rules: {
test: CustomizeRule.Match,
use: {
loader: CustomizeRule.Match,
options: CustomizeRule.Append,
},
},
},
});
const a = {
resolve: { extensions: [".js"] },
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: "style-loader", options: {} },
{ loader: "sass-loader" },
],
},
],
},
};
const b = {
resolve: { extensions: [".css"] },
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: "style-loader",
options: {},
},
],
},
],
},
};

assert.throws(() => _mergeWithExplicitRule(a, b), {
name: "TypeError",
message: "Trying to append non-arrays",
});
});

it("should throw if trying to prepend non-arrays", function () {
const _mergeWithExplicitRule = mergeWithRules({
module: {
rules: {
test: CustomizeRule.Match,
use: {
loader: CustomizeRule.Match,
options: CustomizeRule.Prepend,
},
},
},
});
const a = {
resolve: { extensions: [".js"] },
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: "style-loader", options: {} },
{ loader: "sass-loader" },
],
},
],
},
};
const b = {
resolve: { extensions: [".css"] },
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: "style-loader",
options: {},
},
],
},
],
},
};

assert.throws(() => _mergeWithExplicitRule(a, b), {
name: "TypeError",
message: "Trying to prepend non-arrays",
});
});

it("should work with multi-level match (#153)", function () {
const a = {
module: {
Expand Down

0 comments on commit aa9fd96

Please sign in to comment.