diff --git a/README.md b/README.md index 769d7d7..ee667bc 100644 --- a/README.md +++ b/README.md @@ -30,14 +30,19 @@ You may now use the plugin now via `Bun.build`: ```ts // build.ts +import type { UserConfig } from 'unocss' import { plugin as unocss } from 'bun-plugin-unocss' // import unocss from 'bun-plugin-unocss' +const config: UserConfig = { + // Your UnoCSS config +} + Bun.build({ entrypoints: ['./src/index.html'], outdir: './dist', plugins: [ - unocss() + unocss(config), // by default, it will look for the Uno config file in the project root ], }) ``` @@ -51,20 +56,13 @@ Additionally, it can also be used in conjunction with HTML imports, via `Bun.ser import home from './home.html' const server = Bun.serve({ - // Add HTML imports to `static` static: { // Bundle & route home.html to "/home" '/': home, }, - // Enable development mode for: - // - Detailed error messages - // - Rebuild on request - development: true, - - // Handle API requests async fetch(req) { - console.log('on server') + console.log('any other request', req.url) // Return 404 for unmatched routes return new Response('Not Found', { status: 404 }) diff --git a/src/index.ts b/src/index.ts index aeb6c4d..86f445a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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('', `\n`) - - 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('', `\n`) + + return { + contents, + loader: 'html', + } + }) + }, + } } export default plugin diff --git a/test/unocss.test.ts b/test/unocss.test.ts index cbc237b..24f84d9 100644 --- a/test/unocss.test.ts +++ b/test/unocss.test.ts @@ -1,34 +1,29 @@ import { afterAll, describe, expect, it } from 'bun:test' import { plugin as unocss } from '../src/index' +import unoConfig from '../uno.config' describe('bun-plugin-unocss', () => { it('should inject generated CSS into HTML', async () => { - const html = ` - - -