Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions packages/bun-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@devup-ui/plugin-utils'
import {
codeExtract,
getCss,
getThemeInterface,
hasDevupUI,
registerShorthands,
Expand All @@ -22,6 +23,7 @@ const libPackage = '@devup-ui/react'
const devupFile = 'devup.json'
const distDir = 'df'
const cssDir = resolve(distDir, 'devup-ui')
const baseCssFile = join(cssDir, 'devup-ui.css')
const singleCss = true
const importAliases = mergeImportAliases()

Expand Down Expand Up @@ -49,6 +51,17 @@ async function writeDataFiles() {
if (!existsSync(cssDir)) {
await mkdir(cssDir, { recursive: true })
}
// `onResolve` points every devup-ui.css import at this file, so it has to
// exist before the first source file is loaded. Without it the very first
// import fails to resolve and the whole run dies before any style is
// collected.
if (!existsSync(baseCssFile)) {
await writeFile(baseCssFile, getCss(null, false), 'utf-8')
}
}

async function writeBaseCss() {
await writeFile(baseCssFile, getCss(null, false), 'utf-8')
}

async function initialize({ shorthands }: DevupUIBunPluginOptions = {}) {
Expand Down Expand Up @@ -82,7 +95,7 @@ async function loadSourceFile(filePath: string) {
const contents = await Bun.file(filePath).text()

if (hasDevupUI(filePath, contents, libPackage)) {
const code = codeExtract(
const { code, updatedBaseStyle } = codeExtract(
filePath,
contents,
libPackage,
Expand All @@ -92,7 +105,11 @@ async function loadSourceFile(filePath: string) {
false,
importAliases,
)
return { contents: code.code, loader }
// The extracted styles only reach the browser if they are written out.
// Every other plugin does this; here they were collected and dropped, so
// the file the imports resolve to stayed whatever it was.
if (updatedBaseStyle) await writeBaseCss()
return { contents: code, loader }
}
return { contents, loader }
}
Expand Down
Loading