Skip to content

Commit

Permalink
feat(config): Add plugins option
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Feb 9, 2024
1 parent c540bf3 commit ea375c7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@iarna/toml": "^2.2.5",
"@kosko/common-utils": "workspace:^",
"@kosko/log": "workspace:^",
"superstruct": "^0.15.3"
"superstruct": "^1.0.3"
},
"devDependencies": {
"@kosko/build-scripts": "workspace:^",
Expand Down
29 changes: 20 additions & 9 deletions packages/config/src/__tests__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ describe("getConfig", () => {
expect(getConfig({}, "dev")).toEqual({
components: [],
require: [],
loaders: []
loaders: [],
plugins: []
});
});

test("should return the global config when env is not defined", () => {
const input = {
components: ["foo"],
require: ["bar"],
loaders: ["baz"]
loaders: ["baz"],
plugins: [{ name: "a" }]
};

expect(getConfig(input, "dev")).toEqual(input);
Expand All @@ -69,15 +71,17 @@ describe("getConfig", () => {
const globalConf = {
components: ["foo"],
require: ["bar"],
loaders: ["baz"]
loaders: ["baz"],
plugins: [{ name: "a" }]
};
const input = {
...globalConf,
environments: {
prod: {
components: ["aaa"],
require: ["bbb"],
loaders: ["ccc"]
loaders: ["ccc"],
plugins: [{ name: "b" }]
}
}
};
Expand All @@ -90,19 +94,22 @@ describe("getConfig", () => {
components: ["foo"],
require: ["bar"],
loaders: ["baz"],
plugins: [{ name: "a" }],
environments: {
dev: {
components: ["aaa"],
require: ["bbb"],
loaders: ["ccc"]
loaders: ["ccc"],
plugins: [{ name: "b" }]
}
}
};

expect(getConfig(input, "dev")).toEqual({
components: ["foo", "aaa"],
require: ["bar", "bbb"],
loaders: ["baz", "ccc"]
loaders: ["baz", "ccc"],
plugins: [{ name: "a" }, { name: "b" }]
});
});

Expand All @@ -111,24 +118,28 @@ describe("getConfig", () => {
components: ["foo"],
require: ["bar"],
loaders: ["baz"],
plugins: [{ name: "a" }],
environments: {
a: {
components: ["aa"],
require: ["ab"],
loaders: ["ac"]
loaders: ["ac"],
plugins: [{ name: "ad" }]
},
c: {
components: ["ca"],
require: ["cb"],
loaders: ["cc"]
loaders: ["cc"],
plugins: [{ name: "cd" }]
}
}
};

expect(getConfig(input, ["a", "b", "c"])).toEqual({
components: ["foo", "aa", "ca"],
require: ["bar", "ab", "cb"],
loaders: ["baz", "ac", "cc"]
loaders: ["baz", "ac", "cc"],
plugins: [{ name: "a" }, { name: "ad" }, { name: "cd" }]
});
});
});
11 changes: 2 additions & 9 deletions packages/config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,13 @@ export function getConfig(
.map((env) => environments[env])
.filter(Boolean);

if (!envConfigs.length) {
return {
require: config.require ?? [],
components: config.components ?? [],
loaders: config.loaders ?? []
};
}

return {
require: flatten(config.require, ...envConfigs.map((e) => e.require)),
components: flatten(
config.components,
...envConfigs.map((e) => e.components)
),
loaders: flatten(config.loaders, ...envConfigs.map((e) => e.loaders))
loaders: flatten(config.loaders, ...envConfigs.map((e) => e.loaders)),
plugins: flatten(config.plugins, ...envConfigs.map((e) => e.plugins))
};
}
2 changes: 1 addition & 1 deletion packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
*/

export * from "./config";
export type { EnvironmentConfig, Config } from "./types";
export type { EnvironmentConfig, PluginConfig, Config } from "./types";
export * from "./validate";
22 changes: 20 additions & 2 deletions packages/config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,25 @@ import {
record,
boolean,
integer,
min
min,
unknown
} from "superstruct";

/**
* Plugin config type.
*
* @public
*/
export interface PluginConfig {
name: string;
config?: unknown;
}

export const pluginConfigSchema = object({
name: string(),
config: optional(unknown())
});

/**
* Environment config type.
*
Expand All @@ -19,12 +35,14 @@ export interface EnvironmentConfig {
require?: string[];
components?: string[];
loaders?: string[];
plugins?: PluginConfig[];
}

export const environmentConfigSchema = object({
require: optional(array(string())),
components: optional(array(string())),
loaders: optional(array(string()))
loaders: optional(array(string())),
plugins: optional(array(pluginConfigSchema))
});

/**
Expand Down
9 changes: 5 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ea375c7

Please sign in to comment.