11import { $ , Glob } from "bun" ;
22import path from "node:path" ;
3+ import postcss from "postcss" ;
34import { 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" ;
57import { toCamelCase } from "./utils/to-pascal-case" ;
68import { 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 ;
814
915const createScopedStyles = ( props : { source : string ; moduleName : string } ) => {
1016 const { source, moduleName } = props ;
1117
1218 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 ) ,
2821 ] ) . process ( source ) . css ;
2922} ;
3023
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+
3137export type ModuleNames = Array < { name : string ; moduleName : string } > ;
3238
3339export async function buildStyles ( ) {
@@ -61,22 +67,10 @@ export async function buildStyles() {
6167 const content = await Bun . file ( absPath ) . text ( ) ;
6268 const css_minified = minifyCss ( content ) ;
6369
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+ ) ;
8074 await writeTo (
8175 `src/styles/${ name } .d.ts` ,
8276 `export { ${ moduleName } as default } from "./";\n` ,
@@ -85,6 +79,17 @@ export async function buildStyles() {
8579
8680 const scoped_style = createScopedStyles ( { source : content , moduleName } ) ;
8781
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+
8893 scoped_styles += scoped_style ;
8994 } else {
9095 // Copy over other file types, like images.
@@ -144,6 +149,8 @@ export async function buildStyles() {
144149
145150 // Don't format metadata used in docs.
146151 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.
147154 await Bun . write (
148155 "www/data/scoped-styles.css" ,
149156 minifyCss ( scoped_styles , { removeAll : true } ) ,
0 commit comments