Skip to content

Commit 5cb501e

Browse files
authored
feat: Support rules as strings (#185)
Closes #179.
1 parent 72b52ce commit 5cb501e

File tree

3 files changed

+149
-8
lines changed

3 files changed

+149
-8
lines changed

src/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import wildcard from "wildcard";
22
import mergeWith from "./merge-with";
33
import joinArrays from "./join-arrays";
44
import unique from "./unique";
5-
import { CustomizeRule, ICustomizeOptions, Key } from "./types";
5+
import {
6+
CustomizeRule,
7+
CustomizeRuleString,
8+
ICustomizeOptions,
9+
Key,
10+
} from "./types";
611
import { isPlainObject, isUndefined } from "./utils";
712

813
function merge<Configuration extends object>(
@@ -68,7 +73,9 @@ function mergeWithCustomize<Configuration extends object>(
6873
};
6974
}
7075

71-
function customizeArray(rules: { [s: string]: CustomizeRule }) {
76+
function customizeArray(rules: {
77+
[s: string]: CustomizeRule | CustomizeRuleString;
78+
}) {
7279
return (a: any, b: any, key: Key) => {
7380
const matchedRule =
7481
Object.keys(rules).find((rule) => wildcard(rule, key)) || "";
@@ -87,12 +94,12 @@ function customizeArray(rules: { [s: string]: CustomizeRule }) {
8794
};
8895
}
8996

90-
type Rules = { [s: string]: CustomizeRule | Rules };
97+
type Rules = { [s: string]: CustomizeRule | CustomizeRuleString | Rules };
9198

9299
function mergeWithRules(rules: Rules) {
93100
return mergeWithCustomize({
94101
customizeArray: (a: any, b: any, key: Key) => {
95-
let currentRule: CustomizeRule | Rules = rules;
102+
let currentRule: CustomizeRule | CustomizeRuleString | Rules = rules;
96103

97104
key.split(".").forEach((k) => {
98105
if (!currentRule) {
@@ -122,7 +129,7 @@ function mergeWithRule({
122129
a,
123130
b,
124131
}: {
125-
currentRule: CustomizeRule | Rules;
132+
currentRule: CustomizeRule | CustomizeRuleString | Rules;
126133
a: any;
127134
b: any;
128135
}) {
@@ -159,10 +166,10 @@ function mergeWithRule({
159166
return matches;
160167
});
161168

162-
if(!isPlainObject(ao)){
169+
if (!isPlainObject(ao)) {
163170
return ao;
164171
}
165-
172+
166173
Object.entries(ao).forEach(([k, v]) => {
167174
const rule = currentRule;
168175

@@ -278,7 +285,9 @@ function last(arr) {
278285
return arr[arr.length - 1];
279286
}
280287

281-
function customizeObject(rules: { [s: string]: CustomizeRule }) {
288+
function customizeObject(rules: {
289+
[s: string]: CustomizeRule | CustomizeRuleString;
290+
}) {
282291
return (a: any, b: any, key: Key) => {
283292
switch (rules[key]) {
284293
case CustomizeRule.Prepend:

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ export enum CustomizeRule {
1414
Prepend = "prepend",
1515
Replace = "replace",
1616
}
17+
18+
export type CustomizeRuleString =
19+
| "match"
20+
| "merge"
21+
| "append"
22+
| "prepend"
23+
| "replace";

test/customize.test.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,54 @@ describe("Merge strategy", function () {
1919
});
2020

2121
function mergeStrategySpecificTests(merge) {
22+
it("should work with nested arrays and string for append", function () {
23+
const a = {
24+
module: {
25+
loaders: [
26+
{
27+
test: /.jsx?$/,
28+
loaders: ["babel"],
29+
exclude: /node_modules/,
30+
},
31+
],
32+
},
33+
};
34+
const b = {
35+
module: {
36+
loaders: [
37+
{
38+
test: /.jsx?$/,
39+
loaders: ["react-hot"],
40+
exclude: /node_modules/,
41+
},
42+
],
43+
},
44+
};
45+
const result = {
46+
module: {
47+
loaders: [
48+
{
49+
test: /.jsx?$/,
50+
loaders: ["babel"],
51+
exclude: /node_modules/,
52+
},
53+
{
54+
test: /.jsx?$/,
55+
loaders: ["react-hot"],
56+
exclude: /node_modules/,
57+
},
58+
],
59+
},
60+
};
61+
62+
assert.deepStrictEqual(
63+
merge({
64+
"module.loaders": "append",
65+
})(a, b),
66+
result
67+
);
68+
});
69+
2270
it("should work with nested arrays and prepend", function () {
2371
const a = {
2472
module: {
@@ -67,6 +115,54 @@ function mergeStrategySpecificTests(merge) {
67115
);
68116
});
69117

118+
it("should work with nested arrays and string for prepend", function () {
119+
const a = {
120+
module: {
121+
loaders: [
122+
{
123+
test: /.jsx?$/,
124+
loaders: ["babel"],
125+
exclude: /node_modules/,
126+
},
127+
],
128+
},
129+
};
130+
const b = {
131+
module: {
132+
loaders: [
133+
{
134+
test: /.jsx?$/,
135+
loaders: ["react-hot"],
136+
exclude: /node_modules/,
137+
},
138+
],
139+
},
140+
};
141+
const result = {
142+
module: {
143+
loaders: [
144+
{
145+
test: /.jsx?$/,
146+
loaders: ["react-hot"],
147+
exclude: /node_modules/,
148+
},
149+
{
150+
test: /.jsx?$/,
151+
loaders: ["babel"],
152+
exclude: /node_modules/,
153+
},
154+
],
155+
},
156+
};
157+
158+
assert.deepStrictEqual(
159+
merge({
160+
"module.loaders": "prepend",
161+
})(a, b),
162+
result
163+
);
164+
});
165+
70166
it("should work with array wildcards", function () {
71167
const a = {
72168
entry: {
@@ -95,4 +191,33 @@ function mergeStrategySpecificTests(merge) {
95191
result
96192
);
97193
});
194+
195+
it("should work with array wildcards and string for replace", function () {
196+
const a = {
197+
entry: {
198+
main: ["./src\\config\\main.ts"],
199+
polyfills: ["./src\\config\\polyfills.ts"],
200+
styles: ["./src\\assets\\styles\\styles.sass"],
201+
},
202+
};
203+
const b = {
204+
entry: {
205+
main: ["./src\\config\\main.playground.ts"],
206+
},
207+
};
208+
const result = {
209+
entry: {
210+
main: ["./src\\config\\main.playground.ts"],
211+
polyfills: ["./src\\config\\polyfills.ts"],
212+
styles: ["./src\\assets\\styles\\styles.sass"],
213+
},
214+
};
215+
216+
assert.deepStrictEqual(
217+
merge({
218+
"entry.*": "replace",
219+
})(a, b),
220+
result
221+
);
222+
});
98223
}

0 commit comments

Comments
 (0)