Skip to content

Commit

Permalink
feat: Support rules as strings (#185)
Browse files Browse the repository at this point in the history
Closes #179.
  • Loading branch information
bebraw authored Jun 7, 2021
1 parent 72b52ce commit 5cb501e
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import wildcard from "wildcard";
import mergeWith from "./merge-with";
import joinArrays from "./join-arrays";
import unique from "./unique";
import { CustomizeRule, ICustomizeOptions, Key } from "./types";
import {
CustomizeRule,
CustomizeRuleString,
ICustomizeOptions,
Key,
} from "./types";
import { isPlainObject, isUndefined } from "./utils";

function merge<Configuration extends object>(
Expand Down Expand Up @@ -68,7 +73,9 @@ function mergeWithCustomize<Configuration extends object>(
};
}

function customizeArray(rules: { [s: string]: CustomizeRule }) {
function customizeArray(rules: {
[s: string]: CustomizeRule | CustomizeRuleString;
}) {
return (a: any, b: any, key: Key) => {
const matchedRule =
Object.keys(rules).find((rule) => wildcard(rule, key)) || "";
Expand All @@ -87,12 +94,12 @@ function customizeArray(rules: { [s: string]: CustomizeRule }) {
};
}

type Rules = { [s: string]: CustomizeRule | Rules };
type Rules = { [s: string]: CustomizeRule | CustomizeRuleString | Rules };

function mergeWithRules(rules: Rules) {
return mergeWithCustomize({
customizeArray: (a: any, b: any, key: Key) => {
let currentRule: CustomizeRule | Rules = rules;
let currentRule: CustomizeRule | CustomizeRuleString | Rules = rules;

key.split(".").forEach((k) => {
if (!currentRule) {
Expand Down Expand Up @@ -122,7 +129,7 @@ function mergeWithRule({
a,
b,
}: {
currentRule: CustomizeRule | Rules;
currentRule: CustomizeRule | CustomizeRuleString | Rules;
a: any;
b: any;
}) {
Expand Down Expand Up @@ -159,10 +166,10 @@ function mergeWithRule({
return matches;
});

if(!isPlainObject(ao)){
if (!isPlainObject(ao)) {
return ao;
}

Object.entries(ao).forEach(([k, v]) => {
const rule = currentRule;

Expand Down Expand Up @@ -278,7 +285,9 @@ function last(arr) {
return arr[arr.length - 1];
}

function customizeObject(rules: { [s: string]: CustomizeRule }) {
function customizeObject(rules: {
[s: string]: CustomizeRule | CustomizeRuleString;
}) {
return (a: any, b: any, key: Key) => {
switch (rules[key]) {
case CustomizeRule.Prepend:
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ export enum CustomizeRule {
Prepend = "prepend",
Replace = "replace",
}

export type CustomizeRuleString =
| "match"
| "merge"
| "append"
| "prepend"
| "replace";
125 changes: 125 additions & 0 deletions test/customize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,54 @@ describe("Merge strategy", function () {
});

function mergeStrategySpecificTests(merge) {
it("should work with nested arrays and string for append", function () {
const a = {
module: {
loaders: [
{
test: /.jsx?$/,
loaders: ["babel"],
exclude: /node_modules/,
},
],
},
};
const b = {
module: {
loaders: [
{
test: /.jsx?$/,
loaders: ["react-hot"],
exclude: /node_modules/,
},
],
},
};
const result = {
module: {
loaders: [
{
test: /.jsx?$/,
loaders: ["babel"],
exclude: /node_modules/,
},
{
test: /.jsx?$/,
loaders: ["react-hot"],
exclude: /node_modules/,
},
],
},
};

assert.deepStrictEqual(
merge({
"module.loaders": "append",
})(a, b),
result
);
});

it("should work with nested arrays and prepend", function () {
const a = {
module: {
Expand Down Expand Up @@ -67,6 +115,54 @@ function mergeStrategySpecificTests(merge) {
);
});

it("should work with nested arrays and string for prepend", function () {
const a = {
module: {
loaders: [
{
test: /.jsx?$/,
loaders: ["babel"],
exclude: /node_modules/,
},
],
},
};
const b = {
module: {
loaders: [
{
test: /.jsx?$/,
loaders: ["react-hot"],
exclude: /node_modules/,
},
],
},
};
const result = {
module: {
loaders: [
{
test: /.jsx?$/,
loaders: ["react-hot"],
exclude: /node_modules/,
},
{
test: /.jsx?$/,
loaders: ["babel"],
exclude: /node_modules/,
},
],
},
};

assert.deepStrictEqual(
merge({
"module.loaders": "prepend",
})(a, b),
result
);
});

it("should work with array wildcards", function () {
const a = {
entry: {
Expand Down Expand Up @@ -95,4 +191,33 @@ function mergeStrategySpecificTests(merge) {
result
);
});

it("should work with array wildcards and string for replace", function () {
const a = {
entry: {
main: ["./src\\config\\main.ts"],
polyfills: ["./src\\config\\polyfills.ts"],
styles: ["./src\\assets\\styles\\styles.sass"],
},
};
const b = {
entry: {
main: ["./src\\config\\main.playground.ts"],
},
};
const result = {
entry: {
main: ["./src\\config\\main.playground.ts"],
polyfills: ["./src\\config\\polyfills.ts"],
styles: ["./src\\assets\\styles\\styles.sass"],
},
};

assert.deepStrictEqual(
merge({
"entry.*": "replace",
})(a, b),
result
);
});
}

0 comments on commit 5cb501e

Please sign in to comment.