Skip to content

Commit

Permalink
feat: replace postcss-load-config with custom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed Jun 17, 2020
1 parent 15f0b79 commit 50b19bb
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/loaders/postcss/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import path from "path";
import postcss from "postcss";
import { cosmiconfig } from "cosmiconfig";
import { PostCSSConfigLoaderOptions } from "../../types";
import { ensurePCSSPlugins, ensurePCSSOption } from "../../utils/options";

interface Options {
parser?: postcss.Parser;
syntax?: postcss.Syntax;
stringifier?: postcss.Stringifier;
}

interface Config extends Options {
plugins?: { [p: string]: Record<string, unknown> };
}

interface Result {
plugins: (postcss.Transformer | postcss.Processor)[];
options: Options;
}

export default async function (
id: string,
config: false | PostCSSConfigLoaderOptions,
): Promise<Result> {
if (!config) return { plugins: [], options: {} };

const { ext, dir, base } = path.parse(id);

type Found = { config: Config | ((ctx: Record<string, unknown>) => Config); isEmpty?: boolean };
const searchPath = config.path ? path.resolve(config.path) : dir;
const found: Found | null = await cosmiconfig("postcss").search(searchPath);

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

const { plugins: _plugins, parser, syntax, stringifier } =
typeof found.config === "function"
? found.config({
cwd: process.cwd(),
env: process.env["NODE_ENV"] ?? "development",
file: { extname: ext, dirname: dir, basename: base },
options: config.ctx ?? {},
})
: 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");
if (stringifier) result.options.stringifier = ensurePCSSOption(stringifier, "stringifier");

return result;
}

0 comments on commit 50b19bb

Please sign in to comment.