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 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 {
plugins: PostCSSPlugin[];
modules: boolean | any;
rootDir?: string;
sassOptions?: SassOptions;
lessOptions?: Less.Options;
stylusOptions?: StylusRenderOptions;
}
interface CSSModule {
@ -31,7 +36,10 @@ interface CSSModule {
const postCSSPlugin = ({
plugins = [],
modules = true,
rootDir = process.cwd()
rootDir = process.cwd(),
sassOptions = {},
lessOptions = {},
stylusOptions = {}
}: PostCSSPluginOptions): Plugin => ({
name: "postcss2",
setup(build) {
@ -96,14 +104,18 @@ const postCSSPlugin = ({
// parse files with preprocessors
if (sourceExt === ".sass" || sourceExt === ".scss")
css = (await renderSass({ file: sourceFullPath })).css.toString();
css = (
await renderSass({ ...sassOptions, file: sourceFullPath })
).css.toString();
if (sourceExt === ".styl")
css = await renderStylus(new TextDecoder().decode(fileContent), {
...stylusOptions,
filename: sourceFullPath
});
if (sourceExt === ".less")
css = (
await less.render(new TextDecoder().decode(fileContent), {
...lessOptions,
filename: sourceFullPath,
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) => {
stylus.render(str, options, (e, res) => {
if (e) reject(e);