Skip to content

Commit

Permalink
fix - Fix undefined use (#166)
Browse files Browse the repository at this point in the history
Closes #165.
  • Loading branch information
bebraw authored Dec 16, 2020
1 parent 88d9bc1 commit 249fc5d
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,19 @@ function mergeWithRule({
});
break;
case CustomizeRule.Append:
if (!bMatches.length) {
ret[k] = v;

break;
}

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(appendValue) : v;
ret[k] = v.concat(appendValue);
break;
case CustomizeRule.Merge:
const lastValue = last(bMatches)[k];
Expand All @@ -197,13 +202,19 @@ function mergeWithRule({
ret[k] = { ...v, ...lastValue };
break;
case CustomizeRule.Prepend:
if (!bMatches.length) {
ret[k] = v;

break;
}

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;
ret[k] = prependValue.concat(v);
break;
case CustomizeRule.Replace:
ret[k] = bMatches.length > 0 ? last(bMatches)[k] : v;
Expand Down
150 changes: 150 additions & 0 deletions test/merge-with-rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,4 +893,154 @@ describe("Merge with rules", function () {

expect(mergeRules(a, b)).toEqual(result);
});

it("should append with local match (#165)", function () {
const base = {
module: {
rules: [
{
test: /\.s(a|c)ss$/,
use: [
{
loader: "css-loader",
},
{
loader: "sass-loader",
},
],
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: "file-loader",
},
],
},
],
},
};
const development = {
module: {
rules: [
{
test: /\.s(a|c)ss$/,
use: ["style-loader"],
},
],
},
};
const result = {
module: {
rules: [
{
test: /\.s(a|c)ss$/,
use: [
{
loader: "css-loader",
},
{
loader: "sass-loader",
},
"style-loader",
],
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: "file-loader",
},
],
},
],
},
};

expect(
mergeWithRules({
module: {
rules: {
test: CustomizeRule.Match,
use: CustomizeRule.Append,
},
},
})(base, development)
).toEqual(result);
});

it("should prepend with local match (#165)", function () {
const base = {
module: {
rules: [
{
test: /\.s(a|c)ss$/,
use: [
{
loader: "css-loader",
},
{
loader: "sass-loader",
},
],
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: "file-loader",
},
],
},
],
},
};
const development = {
module: {
rules: [
{
test: /\.s(a|c)ss$/,
use: ["style-loader"],
},
],
},
};
const result = {
module: {
rules: [
{
test: /\.s(a|c)ss$/,
use: [
"style-loader",
{
loader: "css-loader",
},
{
loader: "sass-loader",
},
],
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: "file-loader",
},
],
},
],
},
};

expect(
mergeWithRules({
module: {
rules: {
test: CustomizeRule.Match,
use: CustomizeRule.Prepend,
},
},
})(base, development)
).toEqual(result);
});
});

0 comments on commit 249fc5d

Please sign in to comment.