1
1
import * as fs from 'node:fs'
2
+ import { readFile } from 'node:fs/promises'
2
3
import * as path from 'node:path'
3
4
import { fileURLToPath } from 'node:url'
4
5
import esbuild from 'esbuild'
@@ -62,6 +63,47 @@ function patchCjsInterop() {
62
63
}
63
64
}
64
65
66
+ /**
67
+ * @returns {import('esbuild').Plugin }
68
+ */
69
+ function inlineCssImports ( ) {
70
+ return {
71
+ name : 'inline-css-imports' ,
72
+ setup ( build ) {
73
+ // Inline CSS imports
74
+ build . onLoad ( { filter : / \. c s s $ / } , async ( args ) => {
75
+ let content = await readFile ( args . path , 'utf-8' )
76
+
77
+ return {
78
+ loader : 'js' ,
79
+ contents : `export default ${ JSON . stringify ( content ) } ` ,
80
+ }
81
+ } )
82
+
83
+ // Inline preflight in v3
84
+ // TODO: This needs a test
85
+ build . onLoad ( { filter : / c o r e P l u g i n s \. j s $ / } , async ( args ) => {
86
+ let preflightPath = path . resolve ( path . dirname ( args . path ) , './css/preflight.css' )
87
+ let preflightContent = await readFile ( preflightPath , 'utf-8' )
88
+
89
+ let content = await readFile ( args . path , 'utf-8' )
90
+
91
+ // This is a bit fragile but this is to inline preflight for the
92
+ // *bundled* version which means a failing test should be enough
93
+ content = content . replace (
94
+ `_fs.default.readFileSync(_path.join(__dirname, "./css/preflight.css"), "utf8")` ,
95
+ JSON . stringify ( preflightContent ) ,
96
+ )
97
+
98
+ return {
99
+ loader : 'js' ,
100
+ contents : content ,
101
+ }
102
+ } )
103
+ } ,
104
+ }
105
+ }
106
+
65
107
const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
66
108
67
109
let context = await esbuild . context ( {
@@ -73,7 +115,7 @@ let context = await esbuild.context({
73
115
entryPoints : [ path . resolve ( __dirname , './src/index.js' ) ] ,
74
116
outfile : path . resolve ( __dirname , './dist/index.mjs' ) ,
75
117
format : 'esm' ,
76
- plugins : [ patchRecast ( ) , patchCjsInterop ( ) ] ,
118
+ plugins : [ patchRecast ( ) , patchCjsInterop ( ) , inlineCssImports ( ) ] ,
77
119
} )
78
120
79
121
await context . rebuild ( )
0 commit comments