-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstylus.ts
55 lines (53 loc) · 1.31 KB
/
stylus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* The Stylus preprocessor for the esbuild PostCSS Plugin.
*
* @module
*/
import type { Preprocessor, PreprocessorResults } from "./postcss.ts";
import stylus from "stylus";
/**
* Creates a Stylus preprocessor for the esbuild PostCSS Plugin.
*
* ```ts
* import esbuild from "esbuild";
* import { postCSSPlugin } from "@udibo/esbuild-plugin-postcss";
* import { stylusPreprocessor } from "@udibo/esbuild-plugin-postcss/stylus";
*
* esbuild.build({
* plugins: [postCSSPlugin({
* preprocessors: [stylusPreprocessor()],
* })],
* entryPoints: ["./src/index.styl"],
* outdir: "./dist",
* bundle: true,
* });
* ```
*
* @param options - The options for the stylus preprocessor.
* @returns The stylus preprocessor.
*/
export function stylusPreprocessor(
options?: Omit<stylus.RenderOptions, "filename">,
): Preprocessor {
return {
filter: /\.styl$/,
async compile(
path: string,
fileContent: string,
): Promise<PreprocessorResults> {
return await new Promise((resolve, reject) => {
stylus.render(
fileContent,
{
...options,
filename: path,
},
(e: Error, css: string, _js: string) => {
if (e) reject(e);
else resolve({ css });
},
);
});
},
};
}