Skip to content

Commit 8486bef

Browse files
committed
build(scripts): apply scope styles with minify process
1 parent d6d976c commit 8486bef

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

scripts/build-styles.ts

+6-28
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
11
import { $, Glob } from "bun";
22
import path from "node:path";
3-
import postcss from "postcss";
43
import { createMarkdown } from "./utils/create-markdown";
54
import { minifyCss } from "./utils/minify-css";
5+
import { postcssScopedStyles } from "./utils/postcss-scoped-styles";
66
import { toCamelCase } from "./utils/to-camel-case";
77
import { writeTo } from "./utils/write-to";
88

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-
319
export type ModuleNames = Array<{ name: string; moduleName: string }>;
3210

3311
export async function buildStyles() {
@@ -78,7 +56,10 @@ export async function buildStyles() {
7856
);
7957
await writeTo(`src/styles/${name}.css`, css_minified);
8058

81-
const scoped_style = createScopedStyles({ source: content, moduleName });
59+
const scoped_style = minifyCss(content, {
60+
discardComments: "remove-all",
61+
plugins: [postcssScopedStyles(moduleName)],
62+
});
8263

8364
scoped_styles += scoped_style;
8465
} else {
@@ -139,10 +120,7 @@ export async function buildStyles() {
139120

140121
// Don't format metadata used in docs.
141122
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);
146124

147125
console.timeEnd("build styles");
148126
}

scripts/utils/minify-css.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import cssnano from "cssnano";
22
import litePreset from "cssnano-preset-lite";
3-
import postcss from "postcss";
3+
import postcss, { type Plugin } from "postcss";
44
import discardDuplicates from "postcss-discard-duplicates";
55
import mergeRules from "postcss-merge-rules";
66
import { postcssInlineCssVars } from "./postcss-inline-css-vars";
77

88
export const minifyCss = (
99
css: string,
1010
options?: {
11+
plugins?: Plugin[];
12+
scopeStyles?: boolean;
1113
discardComments?: "preserve-license" | "remove-all";
1214
},
1315
) => {
1416
return postcss([
1517
postcssInlineCssVars(),
18+
...(options?.plugins ?? []),
1619
discardDuplicates(),
1720
mergeRules(),
1821
cssnano({

0 commit comments

Comments
 (0)