Skip to content

Commit 85a383c

Browse files
committed
Ensure CSS imports are inlined
1 parent 4fc9dad commit 85a383c

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

build.mjs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from 'node:fs'
2+
import { readFile } from 'node:fs/promises'
23
import * as path from 'node:path'
34
import { fileURLToPath } from 'node:url'
45
import esbuild from 'esbuild'
@@ -62,6 +63,47 @@ function patchCjsInterop() {
6263
}
6364
}
6465

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: /\.css$/ }, 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: /corePlugins\.js$/ }, 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+
65107
const __dirname = path.dirname(fileURLToPath(import.meta.url))
66108

67109
let context = await esbuild.context({
@@ -73,7 +115,7 @@ let context = await esbuild.context({
73115
entryPoints: [path.resolve(__dirname, './src/index.js')],
74116
outfile: path.resolve(__dirname, './dist/index.mjs'),
75117
format: 'esm',
76-
plugins: [patchRecast(), patchCjsInterop()],
118+
plugins: [patchRecast(), patchCjsInterop(), inlineCssImports()],
77119
})
78120

79121
await context.rebuild()

0 commit comments

Comments
 (0)