11import { $ , Glob } from "bun" ;
22import path from "node:path" ;
3+ import postcss from "postcss" ;
34import { createMarkdown } from "./utils/create-markdown" ;
45import { minifyCss } from "./utils/minify-css" ;
56import { toCamelCase } from "./utils/to-pascal-case" ;
67import { writeTo } from "./utils/write-to" ;
7- import postcss from "postcss" ;
88
99const createScopedStyles = ( props : { source : string ; moduleName : string } ) => {
1010 const { source, moduleName } = props ;
@@ -28,6 +28,28 @@ const createScopedStyles = (props: { source: string; moduleName: string }) => {
2828 ] ) . process ( source ) . css ;
2929} ;
3030
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+
3153export type ModuleNames = Array < { name : string ; moduleName : string } > ;
3254
3355export async function buildStyles ( ) {
@@ -61,22 +83,10 @@ export async function buildStyles() {
6183 const content = await Bun . file ( absPath ) . text ( ) ;
6284 const css_minified = minifyCss ( content ) ;
6385
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+ ) ;
8090 await writeTo (
8191 `src/styles/${ name } .d.ts` ,
8292 `export { ${ moduleName } as default } from "./";\n` ,
@@ -85,6 +95,15 @@ export async function buildStyles() {
8595
8696 const scoped_style = createScopedStyles ( { source : content , moduleName } ) ;
8797
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+
88107 scoped_styles += scoped_style ;
89108 } else {
90109 // Copy over other file types, like images.
@@ -144,6 +163,8 @@ export async function buildStyles() {
144163
145164 // Don't format metadata used in docs.
146165 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.
147168 await Bun . write (
148169 "www/data/scoped-styles.css" ,
149170 minifyCss ( scoped_styles , { removeAll : true } ) ,
0 commit comments