Skip to content

Commit

Permalink
feat: Allow options to be an array too
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Nov 6, 2021
1 parent 47ced96 commit a13305d
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 17 deletions.
6 changes: 0 additions & 6 deletions src/options/OptionHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ class OptionHelper {

async generateOptions() {
const baseOptions = await this.readRawConfig([this.taskName, "options"]);
if (Array.isArray(baseOptions)) {
throw new Error(
"webpack.options must be an object, but array was provided",
);
}

const targetOptions = await this.readRawConfig([
this.taskName,
this.target,
Expand Down
64 changes: 64 additions & 0 deletions src/options/__tests__/__snapshots__/default.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`default options mergeOptions all single objects 1`] = `
Object {
"default": 1,
"options": 1,
"target": 1,
}
`;

exports[`default options mergeOptions merges sub arrays 1`] = `
Object {
"array": Array [
1,
2,
3,
],
}
`;

exports[`default options mergeOptions options and target as array 1`] = `
Array [
Object {
"default": 1,
"options1": 1,
"target1": 1,
},
Object {
"default": 1,
"options2": 1,
"target2": 1,
},
]
`;

exports[`default options mergeOptions options as array 1`] = `
Array [
Object {
"default": 1,
"options1": 1,
"target": 1,
},
Object {
"default": 1,
"options2": 1,
"target": 1,
},
]
`;

exports[`default options mergeOptions target as array 1`] = `
Array [
Object {
"default": 1,
"options": 1,
"target1": 1,
},
Object {
"default": 1,
"options": 1,
"target2": 1,
},
]
`;
56 changes: 55 additions & 1 deletion src/options/__tests__/default.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gruntOptions } from "../default";
import { gruntOptions, mergeOptions } from "../default";

describe("default options", () => {
describe("keepalive", () => {
Expand Down Expand Up @@ -52,4 +52,58 @@ describe("default options", () => {
expect(gruntOptions.failOnError([{ watch: false }, {}, {}])).toBe(true);
});
});

describe("mergeOptions", () => {
test("all single objects", () => {
expect(
mergeOptions({ default: 1 }, { options: 1 }, { target: 1 }),
).toMatchSnapshot();
});

test("merges sub arrays", () => {
expect(
mergeOptions({ array: [1] }, { array: [2] }, { array: [3] }),
).toMatchSnapshot();
});

test("options as array", () => {
expect(
mergeOptions({ default: 1 }, [{ options1: 1 }, { options2: 1 }], {
target: 1,
}),
).toMatchSnapshot();
});

test("target as array", () => {
expect(
mergeOptions(
{ default: 1 },
{
options: 1,
},
[{ target1: 1 }, { target2: 1 }],
),
).toMatchSnapshot();
});

test("options and target as array", () => {
expect(
mergeOptions(
{ default: 1 },
[{ options1: 1 }, { options2: 1 }],
[{ target1: 1 }, { target2: 1 }],
),
).toMatchSnapshot();
});

test("fails on options and target as unequal array", () => {
expect(() =>
mergeOptions(
{ default: 1 },
[{ options1: 1 }, { options2: 1 }, { options3: 1 }],
[{ target1: 1 }, { target2: 1 }],
),
).toThrowError();
});
});
});
27 changes: 17 additions & 10 deletions src/options/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,29 @@ function mergeCustomize(a, b) {
}

function mergeOptions(defaultOptions, options, targetOptions) {
let result;
if (Array.isArray(targetOptions) && Array.isArray(options)) {
if (targetOptions.length !== options.length) {
throw new Error(
"Cannot have both `options` and `target` be an array with different length. " +
"If using arrays for both please ensure they are the same size.",
);
}
return targetOptions.map((opt, index) =>
mergeWith({}, defaultOptions, options[index], opt, mergeCustomize),
);
}

if (Array.isArray(targetOptions)) {
result = targetOptions.map((opt) =>
return targetOptions.map((opt) =>
mergeWith({}, defaultOptions, options, opt, mergeCustomize),
);
} else {
result = mergeWith(
{},
defaultOptions,
options,
targetOptions,
mergeCustomize,
} else if (Array.isArray(options)) {
return options.map((opt) =>
mergeWith({}, defaultOptions, opt, targetOptions, mergeCustomize),
);
}

return result;
return mergeWith({}, defaultOptions, options, targetOptions, mergeCustomize);
}

exports.gruntOptions = gruntOptions;
Expand Down

0 comments on commit a13305d

Please sign in to comment.