Skip to content

Commit

Permalink
feat(postcss): support both object and array for plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed Jun 26, 2020
1 parent b1e4b9d commit 7ce53b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/loaders/postcss/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface Options {
}

interface Config extends Options {
plugins?: { [p: string]: Record<string, unknown> };
plugins?: { [p: string]: Record<string, unknown> } | (string | postcss.Plugin<unknown>)[];
}

interface Result {
Expand All @@ -33,7 +33,7 @@ export default async function (

if (!found || found.isEmpty) return { plugins: [], options: {} };

const { plugins: _plugins, parser, syntax, stringifier } =
const { plugins, parser, syntax, stringifier } =
typeof found.config === "function"
? found.config({
cwd: process.cwd(),
Expand All @@ -43,7 +43,6 @@ export default async function (
})
: found.config;

const plugins = _plugins && Object.entries(_plugins);
const result: Result = { plugins: ensurePCSSPlugins(plugins), options: {} };
if (parser) result.options.parser = ensurePCSSOption(parser, "parser");
if (syntax) result.options.syntax = ensurePCSSOption(syntax, "syntax");
Expand Down
22 changes: 13 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,17 @@ export interface Options {
* A list of plugins for PostCSS,
* which are used before plugins loaded from PostCSS config file, if any
*/
plugins?: (
| postcss.Transformer
| postcss.Processor
| string
| [string | postcss.Plugin<unknown>]
| [string | postcss.Plugin<unknown>, Record<string, unknown>]
| null
| undefined
)[];
plugins?:
| Record<string, unknown>
| (
| postcss.Transformer
| postcss.Processor
| string
| [string | postcss.Plugin<unknown>]
| [string | postcss.Plugin<unknown>, Record<string, unknown>]
| null
| undefined
)[];
/**
* Select mode for this plugin:
* - `"inject"` *(default)* - Embeds CSS inside JS and injects it into `<head>` at runtime.
Expand All @@ -134,8 +136,10 @@ export interface Options {
*/
mode?:
| "inject"
| ["inject"]
| ["inject", InjectOptions | ((varname: string, id: string) => string)]
| "extract"
| ["extract"]
| ["extract", string]
| "emit"
| ["emit"];
Expand Down
5 changes: 3 additions & 2 deletions src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ export function ensurePCSSPlugins(
plugins: Options["plugins"],
): (postcss.Transformer | postcss.Processor)[] {
if (typeof plugins === "undefined") return [];
else if (!Array.isArray(plugins)) throw new TypeError("`plugins` option must be an array!");
else if (typeof plugins !== "object")
throw new TypeError("`plugins` option must be an array or an object!");

const ps: (postcss.Transformer | postcss.Processor)[] = [];
for (const p of plugins) {
for (const p of !Array.isArray(plugins) ? Object.entries(plugins) : plugins) {
if (!p) continue;

if (!Array.isArray(p)) {
Expand Down

0 comments on commit 7ce53b9

Please sign in to comment.