-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpostcss.ts
More file actions
43 lines (34 loc) · 1.19 KB
/
postcss.ts
File metadata and controls
43 lines (34 loc) · 1.19 KB
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
import tailwindcss from '@tailwindcss/postcss';
import type { AcceptedPlugin, Plugin, PluginCreator } from 'postcss';
export interface TailwindCSSPostCSSOptions {
theme: string;
}
const injectTailwindThemePlugin: PluginCreator<TailwindCSSPostCSSOptions> = (
options?: TailwindCSSPostCSSOptions,
): Plugin => {
const themePath = options?.theme.replace(/\\/g, '/');
return {
postcssPlugin: 'rsbuild-inject-tailwind-theme',
Once(root, { AtRule }) {
if (!themePath) {
return;
}
const file = root.source?.input.file || '';
// Use @reference for CSS modules to avoid duplicating theme variables in every module.
// Use @import for global CSS and regular CSS files to ensure theme variables are emitted.
const isCssModule = /\.module\.(css|scss|sass|less|styl)$/i.test(file);
root.prepend(
new AtRule({
name: isCssModule ? 'reference' : 'import',
params: JSON.stringify(themePath),
}),
);
},
};
};
injectTailwindThemePlugin.postcss = true;
export function tailwindPostCSSPlugins(
options: TailwindCSSPostCSSOptions,
): AcceptedPlugin[] {
return [injectTailwindThemePlugin(options), tailwindcss()];
}