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
5
import { minifyCss } from "./utils/minify-css" ;
5
6
import { toCamelCase } from "./utils/to-pascal-case" ;
6
7
import { writeTo } from "./utils/write-to" ;
7
- import postcss from "postcss" ;
8
8
9
9
const createScopedStyles = ( props : { source : string ; moduleName : string } ) => {
10
10
const { source, moduleName } = props ;
@@ -28,6 +28,28 @@ const createScopedStyles = (props: { source: string; moduleName: string }) => {
28
28
] ) . process ( source ) . css ;
29
29
} ;
30
30
31
+ const createJsStyles = ( props : { moduleName : string ; content : string } ) => {
32
+ const { moduleName, content } = props ;
33
+
34
+ const css = minifyCss (
35
+ // Escape backticks for JS template literal.
36
+ content . replace ( / \` / g, "\\`" ) ,
37
+ {
38
+ remove : ( comment ) => {
39
+ if ( / ( L i c e n s e | A u t h o r ) / i. test ( comment ) ) {
40
+ // Preserve license comments.
41
+ return false ;
42
+ }
43
+
44
+ return true ;
45
+ } ,
46
+ } ,
47
+ ) ;
48
+
49
+ return `const ${ moduleName } = \`<style>${ css } </style>\`;\n
50
+ export default ${ moduleName } ;\n` ;
51
+ } ;
52
+
31
53
export type ModuleNames = Array < { name : string ; moduleName : string } > ;
32
54
33
55
export async function buildStyles ( ) {
@@ -61,22 +83,10 @@ export async function buildStyles() {
61
83
const content = await Bun . file ( absPath ) . text ( ) ;
62
84
const css_minified = minifyCss ( content ) ;
63
85
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 ) ;
86
+ await writeTo (
87
+ `src/styles/${ name } .js` ,
88
+ createJsStyles ( { moduleName, content } ) ,
89
+ ) ;
80
90
await writeTo (
81
91
`src/styles/${ name } .d.ts` ,
82
92
`export { ${ moduleName } as default } from "./";\n` ,
@@ -85,6 +95,15 @@ export async function buildStyles() {
85
95
86
96
const scoped_style = createScopedStyles ( { source : content , moduleName } ) ;
87
97
98
+ await writeTo (
99
+ `src/styles/${ name } .scoped.js` ,
100
+ createJsStyles ( { moduleName, content : scoped_style } ) ,
101
+ ) ;
102
+ await writeTo (
103
+ `src/styles/${ name } .scoped.d.ts` ,
104
+ `export { ${ moduleName } as default } from "./";\n` ,
105
+ ) ;
106
+
88
107
scoped_styles += scoped_style ;
89
108
} else {
90
109
// Copy over other file types, like images.
@@ -144,6 +163,8 @@ export async function buildStyles() {
144
163
145
164
// Don't format metadata used in docs.
146
165
await Bun . write ( "www/data/styles.json" , JSON . stringify ( styles ) ) ;
166
+
167
+ // For performance, a dedictated CSS file is used for scoped styles in docs.
147
168
await Bun . write (
148
169
"www/data/scoped-styles.css" ,
149
170
minifyCss ( scoped_styles , { removeAll : true } ) ,
0 commit comments