|
1 | 1 | import { $, Glob } from "bun"; |
2 | 2 | import path from "node:path"; |
3 | | -import postcss from "postcss"; |
4 | 3 | import { createMarkdown } from "./utils/create-markdown"; |
5 | 4 | import { minifyCss } from "./utils/minify-css"; |
| 5 | +import { postcssScopedStyles } from "./utils/postcss-scoped-styles"; |
6 | 6 | import { toCamelCase } from "./utils/to-camel-case"; |
7 | 7 | import { writeTo } from "./utils/write-to"; |
8 | 8 |
|
9 | | -const createScopedStyles = (props: { source: string; moduleName: string }) => { |
10 | | - const { source, moduleName } = props; |
11 | | - |
12 | | - return postcss([ |
13 | | - { |
14 | | - postcssPlugin: "postcss-plugin:scoped-styles", |
15 | | - Once(root) { |
16 | | - root.walkRules((rule) => { |
17 | | - rule.selectors = rule.selectors.map((selector) => { |
18 | | - if (/^pre /.test(selector)) { |
19 | | - selector = `pre.${moduleName}${selector.replace(/^pre /, " ")}`; |
20 | | - } else { |
21 | | - selector = `.${moduleName} ${selector}`; |
22 | | - } |
23 | | - return selector; |
24 | | - }); |
25 | | - }); |
26 | | - }, |
27 | | - }, |
28 | | - ]).process(source).css; |
29 | | -}; |
30 | | - |
31 | 9 | export type ModuleNames = Array<{ name: string; moduleName: string }>; |
32 | 10 |
|
33 | 11 | export async function buildStyles() { |
@@ -78,7 +56,10 @@ export async function buildStyles() { |
78 | 56 | ); |
79 | 57 | await writeTo(`src/styles/${name}.css`, css_minified); |
80 | 58 |
|
81 | | - const scoped_style = createScopedStyles({ source: content, moduleName }); |
| 59 | + const scoped_style = minifyCss(content, { |
| 60 | + discardComments: "remove-all", |
| 61 | + plugins: [postcssScopedStyles(moduleName)], |
| 62 | + }); |
82 | 63 |
|
83 | 64 | scoped_styles += scoped_style; |
84 | 65 | } else { |
@@ -139,10 +120,7 @@ export async function buildStyles() { |
139 | 120 |
|
140 | 121 | // Don't format metadata used in docs. |
141 | 122 | await Bun.write("www/data/styles.json", JSON.stringify(styles)); |
142 | | - await Bun.write( |
143 | | - "www/data/scoped-styles.css", |
144 | | - minifyCss(scoped_styles, { discardComments: "remove-all" }), |
145 | | - ); |
| 123 | + await Bun.write("www/data/scoped-styles.css", scoped_styles); |
146 | 124 |
|
147 | 125 | console.timeEnd("build styles"); |
148 | 126 | } |
0 commit comments