-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a89cee5
commit d9d2359
Showing
3 changed files
with
51 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
import type { BunPlugin } from 'bun' | ||
import type { UserConfig } from 'unocss' | ||
import { loadConfig } from '@unocss/config' | ||
import { createGenerator } from '@unocss/core' | ||
|
||
export const plugin: BunPlugin = { | ||
name: 'bun-plugin-unocss', | ||
async setup(build) { | ||
const { config } = await loadConfig() | ||
const generator = await createGenerator(config) | ||
export function plugin(userConfig?: UserConfig): BunPlugin { | ||
return { | ||
name: 'bun-plugin-unocss', | ||
async setup(build) { | ||
let conf: UserConfig | ||
|
||
build.onLoad({ filter: /\.html$/ }, async ({ path }) => { | ||
const html = await Bun.file(path).text() | ||
const { css } = await generator.generate(html) | ||
const contents = html.replace('</head>', `<style>${css}</style>\n</head>`) | ||
|
||
return { | ||
contents, | ||
loader: 'html', | ||
if (userConfig) { | ||
conf = userConfig | ||
} | ||
else { | ||
const { config } = await loadConfig() | ||
conf = config | ||
} | ||
}) | ||
}, | ||
|
||
const generator = await createGenerator(conf) | ||
|
||
build.onLoad({ filter: /\.html$/ }, async ({ path }) => { | ||
const html = await Bun.file(path).text() | ||
const { css } = await generator.generate(html) | ||
const contents = html.replace('</head>', `<style>${css}</style>\n</head>`) | ||
|
||
return { | ||
contents, | ||
loader: 'html', | ||
} | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
export default plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters