1- import { Plugin , PluginBuild } from "esbuild" ;
1+ import { BuildResult , Plugin , PluginBuild } from "esbuild" ;
22import fs from "node:fs" ;
33import path from "node:path" ;
44import postcss from "postcss" ;
@@ -17,6 +17,38 @@ interface CSSModulePluginOptions {
1717 globalPrefix ?: string ;
1818}
1919
20+ function generateCombinedCSS ( result : BuildResult ) {
21+ /** generate combined server and client CSS */
22+ const serverRegExp = new RegExp ( `server\\${ path . sep } index\\.css` ) ;
23+ const serverCSSFile = result . outputFiles ?. filter ( f => f . path . match ( serverRegExp ) ) [ 0 ] ;
24+
25+ const clientRegExp = new RegExp ( `client\\${ path . sep } index\\.css` ) ;
26+ const clientCSSFile = result . outputFiles ?. filter ( f => f . path . match ( clientRegExp ) ) [ 0 ] ;
27+
28+ if ( ! serverCSSFile && ! clientCSSFile ) return ;
29+
30+ const combinedCSS = ( clientCSSFile ?. text ?? "" ) + ( serverCSSFile ?. text ?? "" ) ;
31+ let indexCSSFile ;
32+ let indexCSSFilePath : string ;
33+ if ( clientCSSFile ) {
34+ indexCSSFilePath = clientCSSFile . path . replace ( `client${ path . sep } ` , "" ) ;
35+ indexCSSFile = result . outputFiles ?. filter ( f => f . path === indexCSSFilePath ) [ 0 ] ;
36+ } else {
37+ indexCSSFilePath = serverCSSFile ?. path . replace ( `server${ path . sep } ` , "" ) ?? "" ;
38+ }
39+
40+ console . log ( { indexCSSFilePath } ) ;
41+
42+ if ( indexCSSFile ) indexCSSFile . contents = new TextEncoder ( ) . encode ( combinedCSS ) ;
43+ else
44+ result . outputFiles ?. push ( {
45+ path : indexCSSFilePath ,
46+ contents : new TextEncoder ( ) . encode ( combinedCSS ) ,
47+ text : combinedCSS ,
48+ hash : uuid ( ) ,
49+ } ) ;
50+ }
51+
2052function applyAutoPrefixer ( build : PluginBuild , options : CSSModulePluginOptions , write ?: boolean ) {
2153 build . onEnd ( async result => {
2254 if ( ! options . skipAutoPrefixer ) {
@@ -26,6 +58,8 @@ function applyAutoPrefixer(build: PluginBuild, options: CSSModulePluginOptions,
2658 }
2759 }
2860
61+ generateCombinedCSS ( result ) ;
62+
2963 /** assume true if undefined */
3064 if ( write === undefined || write ) {
3165 result . outputFiles ?. forEach ( file => {
@@ -43,13 +77,9 @@ function handleScss(build: PluginBuild) {
4377 } ) ) ;
4478}
4579
46- function handleModules (
47- build : PluginBuild ,
48- { generateScopedName } : CSSModulePluginOptions ,
49- type : "css" | "scss" | "sass" = "css" ,
50- ) {
51- const namespace = `${ type } -module` ;
52- const filter = new RegExp ( `\\.module\\.${ type } $` ) ;
80+ function handleModules ( build : PluginBuild , { generateScopedName } : CSSModulePluginOptions ) {
81+ const namespace = "scss-module" ;
82+ const filter = / \. m o d u l e \. ( s c | s a | c ) s s / ;
5383 build . onResolve ( { filter, namespace : "file" } , args => ( {
5484 path : `${ args . path } #${ namespace } ` ,
5585 namespace,
@@ -104,8 +134,6 @@ const cssPlugin: (options?: CSSModulePluginOptions) => Plugin = (options = {}) =
104134 `${ path . basename ( filename ) . split ( "." ) [ 0 ] } __${ name } ` ;
105135 }
106136 handleModules ( build , options ) ;
107- handleModules ( build , options , "scss" ) ;
108- handleModules ( build , options , "sass" ) ;
109137 handleScss ( build ) ;
110138 applyAutoPrefixer ( build , options , write ) ;
111139 } ,
0 commit comments