Merge pull request #9 from nobrayner/render-options

feat: add plugin options for sass, less, and stylus render options
This commit is contained in:
Marton Lederer 2021-04-07 10:38:48 +02:00 committed by GitHub
commit 933d501f82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,10 +15,15 @@ import less from "less";
import stylus from "stylus"; import stylus from "stylus";
import resolveFile from "resolve-file"; import resolveFile from "resolve-file";
type StylusRenderOptions = Parameters<typeof stylus.render>[1]; // The Stylus.RenderOptions interface doesn't seem to be exported... So next best
interface PostCSSPluginOptions { interface PostCSSPluginOptions {
plugins: PostCSSPlugin[]; plugins: PostCSSPlugin[];
modules: boolean | any; modules: boolean | any;
rootDir?: string; rootDir?: string;
sassOptions?: SassOptions;
lessOptions?: Less.Options;
stylusOptions?: StylusRenderOptions;
} }
interface CSSModule { interface CSSModule {
@ -31,7 +36,10 @@ interface CSSModule {
const postCSSPlugin = ({ const postCSSPlugin = ({
plugins = [], plugins = [],
modules = true, modules = true,
rootDir = process.cwd() rootDir = process.cwd(),
sassOptions = {},
lessOptions = {},
stylusOptions = {}
}: PostCSSPluginOptions): Plugin => ({ }: PostCSSPluginOptions): Plugin => ({
name: "postcss2", name: "postcss2",
setup(build) { setup(build) {
@ -96,14 +104,18 @@ const postCSSPlugin = ({
// parse files with preprocessors // parse files with preprocessors
if (sourceExt === ".sass" || sourceExt === ".scss") if (sourceExt === ".sass" || sourceExt === ".scss")
css = (await renderSass({ file: sourceFullPath })).css.toString(); css = (
await renderSass({ ...sassOptions, file: sourceFullPath })
).css.toString();
if (sourceExt === ".styl") if (sourceExt === ".styl")
css = await renderStylus(new TextDecoder().decode(fileContent), { css = await renderStylus(new TextDecoder().decode(fileContent), {
...stylusOptions,
filename: sourceFullPath filename: sourceFullPath
}); });
if (sourceExt === ".less") if (sourceExt === ".less")
css = ( css = (
await less.render(new TextDecoder().decode(fileContent), { await less.render(new TextDecoder().decode(fileContent), {
...lessOptions,
filename: sourceFullPath, filename: sourceFullPath,
rootpath: path.dirname(args.path) rootpath: path.dirname(args.path)
}) })
@ -160,7 +172,10 @@ function renderSass(options: SassOptions): Promise<SassResult> {
}); });
} }
function renderStylus(str: string, options): Promise<string> { function renderStylus(
str: string,
options: StylusRenderOptions
): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
stylus.render(str, options, (e, res) => { stylus.render(str, options, (e, res) => {
if (e) reject(e); if (e) reject(e);