1
1
import { $ , Glob } from "bun" ;
2
2
import path from "node:path" ;
3
+ import postcss from "postcss" ;
3
4
import { createMarkdown } from "./utils/create-markdown" ;
4
- import { minifyCss } from "./utils/minify-css" ;
5
+ import { minifyCss , minifyPreset } from "./utils/minify-css" ;
6
+ import { pluginScopedStyles } from "./utils/plugin-scoped-styles" ;
5
7
import { toCamelCase } from "./utils/to-pascal-case" ;
6
8
import { writeTo } from "./utils/write-to" ;
7
- import postcss from "postcss" ;
9
+
10
+ const preserveLicenseComments = {
11
+ // Preserve license comments.
12
+ remove : ( comment : string ) => ! / ( L i c e n s e | A u t h o r ) / i. test ( comment ) ,
13
+ } as const ;
8
14
9
15
const createScopedStyles = ( props : { source : string ; moduleName : string } ) => {
10
16
const { source, moduleName } = props ;
11
17
12
18
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 ( / ^ p r e / . test ( selector ) ) {
19
- selector = `pre.${ moduleName } ${ selector . replace ( / ^ p r e / , " " ) } ` ;
20
- } else {
21
- selector = `.${ moduleName } ${ selector } ` ;
22
- }
23
- return selector ;
24
- } ) ;
25
- } ) ;
26
- } ,
27
- } ,
19
+ pluginScopedStyles ( { moduleName } ) ,
20
+ ...minifyPreset ( preserveLicenseComments ) ,
28
21
] ) . process ( source ) . css ;
29
22
} ;
30
23
24
+ const createJsStyles = ( props : { moduleName : string ; content : string } ) => {
25
+ const { moduleName, content } = props ;
26
+
27
+ const css = minifyCss (
28
+ // Escape backticks for JS template literal.
29
+ content . replace ( / \` / g, "\\`" ) ,
30
+ preserveLicenseComments ,
31
+ ) ;
32
+
33
+ return `const ${ moduleName } = \`<style>${ css } </style>\`;\n
34
+ export default ${ moduleName } ;\n` ;
35
+ } ;
36
+
31
37
export type ModuleNames = Array < { name : string ; moduleName : string } > ;
32
38
33
39
export async function buildStyles ( ) {
@@ -61,22 +67,10 @@ export async function buildStyles() {
61
67
const content = await Bun . file ( absPath ) . text ( ) ;
62
68
const css_minified = minifyCss ( content ) ;
63
69
64
- // Escape backticks for JS template literal.
65
- const content_css_for_js = minifyCss ( content . replace ( / \` / g, "\\`" ) , {
66
- remove : ( comment ) => {
67
- if ( / ( L i c e n s e | A u t h o r ) / i. test ( comment ) ) {
68
- // Preserve license comments.
69
- return false ;
70
- }
71
-
72
- return true ;
73
- } ,
74
- } ) ;
75
-
76
- const exportee = `const ${ moduleName } = \`<style>${ content_css_for_js } </style>\`;\n
77
- export default ${ moduleName } ;\n` ;
78
-
79
- await writeTo ( `src/styles/${ name } .js` , exportee ) ;
70
+ await writeTo (
71
+ `src/styles/${ name } .js` ,
72
+ createJsStyles ( { moduleName, content } ) ,
73
+ ) ;
80
74
await writeTo (
81
75
`src/styles/${ name } .d.ts` ,
82
76
`export { ${ moduleName } as default } from "./";\n` ,
@@ -85,6 +79,17 @@ export async function buildStyles() {
85
79
86
80
const scoped_style = createScopedStyles ( { source : content , moduleName } ) ;
87
81
82
+ await writeTo (
83
+ `src/styles/${ name } .scoped.js` ,
84
+ createJsStyles ( { moduleName, content : scoped_style } ) ,
85
+ ) ;
86
+ await writeTo (
87
+ `src/styles/${ name } .scoped.d.ts` ,
88
+ `export { ${ moduleName } as default } from "./";\n` ,
89
+ ) ;
90
+
91
+ await writeTo ( `src/styles/${ name } .scoped.css` , scoped_style ) ;
92
+
88
93
scoped_styles += scoped_style ;
89
94
} else {
90
95
// Copy over other file types, like images.
@@ -144,6 +149,8 @@ export async function buildStyles() {
144
149
145
150
// Don't format metadata used in docs.
146
151
await Bun . write ( "www/data/styles.json" , JSON . stringify ( styles ) ) ;
152
+
153
+ // For performance, a dedictated CSS file is used for scoped styles in docs.
147
154
await Bun . write (
148
155
"www/data/scoped-styles.css" ,
149
156
minifyCss ( scoped_styles , { removeAll : true } ) ,
0 commit comments