Skip to content

Commit f674d58

Browse files
aliisxzz
andauthored
feat: support bun plugin (#539)
* loose start for bun plugin * implement bun * support emitFile() * fix Bun cases in integration test * add bun to other test files * remove bun-types-no-globals from github * restore bun-types-no-globals from npm @^1.2 * bun does not yet support .onEnd, so for now we shouldn't fake it with brittle workarounds * add Bun in the documentation * some missing bun references in docs * support multiple plugins * use Bun namespace instead of importing module that won't necessarily exist * Bun is a cute pink color! * fix the transform hook * fix for virtual modules * tidy up * setup bun in ci * revert unplugin require path * ignore bun in test-out folders * update tests * support onEnd * remove * implement guessLoader(), bun also now supports onEnd() * don't eat errors/warnings * we dont need to outdir for bun in this test * bun writebundle test * Update to bun@1.2.22 (supports onEnd and onResolve) * use onStart() * define onStart() in mocks * onStart * ci: run vitest in Bun so we can run bun's tests * Bun error message if building outside of Bun * skip bun specific tests when not running in bun * refactor * allow only * ci: fix typecheck --------- Co-authored-by: Kevin Deng <sxzz@sxzz.moe>
1 parent ffc1e55 commit f674d58

File tree

39 files changed

+1124
-46
lines changed

39 files changed

+1124
-46
lines changed

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,36 @@ permissions: {}
1515
jobs:
1616
unit-test:
1717
uses: sxzz/workflows/.github/workflows/unit-test.yml@v1
18+
with:
19+
typecheck: pnpm run build && pnpm run typecheck
20+
21+
unit-test-bun:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
26+
with:
27+
persist-credentials: false
28+
29+
- name: Install pnpm
30+
uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4.2.0
31+
32+
- name: Setup node
33+
uses: actions/setup-node@2028fbc5c25fe9cf00d9f06a71cc4710d4507903 # v6.0.0
34+
with:
35+
node-version: lts/*
36+
cache: pnpm
37+
38+
- name: Setup Bun
39+
uses: oven-sh/setup-bun@735343b667d3e6f658f44d0eca948eb6282f2b76 # v2.0.2
40+
with:
41+
bun-version: latest
42+
43+
- name: Install dependencies
44+
run: pnpm install
45+
46+
- name: Build
47+
run: pnpm run build
48+
49+
- name: Test with bun
50+
run: bun run scripts/buildFixtures.ts && bun -b vitest --allowOnly

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Currently supports:
1515
- [Rspack](https://www.rspack.dev/)
1616
- [Rolldown](https://rolldown.rs/)
1717
- [Farm](https://www.farmfe.org/)
18+
- [Bun](https://bun.com/)
1819
- And every framework built on top of them.
1920

2021
## Documentations

docs/.vitepress/constant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export const title = 'Unplugin'
2-
export const description = 'Unified plugin system. Support Vite, Rollup, webpack, esbuild, and every frameworks on top of them.'
2+
export const description = 'Unified plugin system. Support Vite, Rollup, webpack, esbuild, Bun, and every frameworks on top of them.'
33
export const url = 'https://unplugin.unjs.io/'
44
export const ogImage = `${url}/og.png`

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Unplugin
77
</h1>
88
<p align="center">
9-
Unified plugin system, Support Vite, Rollup, webpack, esbuild, and more
9+
Unified plugin system, Support Vite, Rollup, webpack, esbuild, Bun, and more
1010
</p>
1111

1212
<p align="center">

docs/guide/index.md

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ lastUpdated: false
1818
- [Rspack](https://www.rspack.dev/)
1919
- [Rolldown](https://rolldown.rs/)
2020
- [Farm](https://www.farmfe.org/)
21+
- [Bun](https://bun.com/)
2122

2223
## Trying It Online
2324

@@ -153,6 +154,21 @@ export default defineConfig({
153154
})
154155
```
155156

157+
```ts [Bun]
158+
// bun.config.ts
159+
import Starter from 'unplugin-starter/bun'
160+
161+
await Bun.build({
162+
entrypoints: ['./src/index.ts'],
163+
outdir: './dist',
164+
plugins: [
165+
Starter({
166+
/* options */
167+
}),
168+
],
169+
})
170+
```
171+
156172
```js [Vue-CLI]
157173
// vue.config.js
158174
module.exports = {
@@ -193,18 +209,18 @@ export default defineConfig({
193209

194210
## Supported Hooks
195211

196-
| Hook | Rollup | Vite | webpack | esbuild | Rspack | Farm | Rolldown |
197-
| --------------------------------------------------------------------------------- | :-------------: | :--: | :-----: | :-------------: | :-------------: | :--: | :------: |
198-
| [`enforce`](https://vite.dev/guide/api-plugin.html#plugin-ordering) | ❌ <sup>1</sup> ||| ❌ <sup>1</sup> ||||
199-
| [`buildStart`](https://rollupjs.org/plugin-development/#buildstart) ||||||||
200-
| [`resolveId`](https://rollupjs.org/plugin-development/#resolveid) ||||| ✅ <sup>5</sup> |||
201-
| ~~`loadInclude`~~<sup>2</sup> ||||||||
202-
| [`load`](https://rollupjs.org/plugin-development/#load) |||| ✅ <sup>3</sup> ||||
203-
| ~~`transformInclude`~~<sup>2</sup> ||||||||
204-
| [`transform`](https://rollupjs.org/plugin-development/#transform) |||| ✅ <sup>3</sup> ||||
205-
| [`watchChange`](https://rollupjs.org/plugin-development/#watchchange) ||||||||
206-
| [`buildEnd`](https://rollupjs.org/plugin-development/#buildend) ||||||||
207-
| [`writeBundle`](https://rollupjs.org/plugin-development/#writebundle)<sup>4</sup> ||||||||
212+
| Hook | Rollup | Vite | webpack | esbuild | Rspack | Farm | Rolldown | Bun |
213+
| --------------------------------------------------------------------------------- | :-------------: | :--: | :-----: | :-------------: | :-------------: | :--: | :------: | :-------------: |
214+
| [`enforce`](https://vite.dev/guide/api-plugin.html#plugin-ordering) | ❌ <sup>1</sup> ||| ❌ <sup>1</sup> |||||
215+
| [`buildStart`](https://rollupjs.org/plugin-development/#buildstart) |||||||||
216+
| [`resolveId`](https://rollupjs.org/plugin-development/#resolveid) ||||| ✅ <sup>5</sup> ||||
217+
| ~~`loadInclude`~~<sup>2</sup> |||||||||
218+
| [`load`](https://rollupjs.org/plugin-development/#load) |||| ✅ <sup>3</sup> |||||
219+
| ~~`transformInclude`~~<sup>2</sup> |||||||||
220+
| [`transform`](https://rollupjs.org/plugin-development/#transform) |||| ✅ <sup>3</sup> |||||
221+
| [`watchChange`](https://rollupjs.org/plugin-development/#watchchange) |||||||||
222+
| [`buildEnd`](https://rollupjs.org/plugin-development/#buildend) |||||||| ❌ <sup>6</sup> |
223+
| [`writeBundle`](https://rollupjs.org/plugin-development/#writebundle)<sup>4</sup> |||||||| ❌ <sup>6</sup> |
208224

209225
::: details Notice
210226

@@ -215,6 +231,7 @@ export default defineConfig({
215231
3. Although esbuild can handle both JavaScript and CSS and many other file formats, you can only return JavaScript in `load` and `transform` results.
216232
4. Currently, `writeBundle` is only serves as a hook for the timing. It doesn't pass any arguments.
217233
5. Rspack supports `resolveId` with a minimum required version of v1.0.0-alpha.1.
234+
6. Bun's plugin API doesn't have an `onEnd` hook yet, so `buildEnd` and `writeBundle` are not supported.
218235

219236
:::
220237

@@ -253,6 +270,7 @@ export const webpackPlugin = unplugin.webpack
253270
export const rspackPlugin = unplugin.rspack
254271
export const esbuildPlugin = unplugin.esbuild
255272
export const farmPlugin = unplugin.farm
273+
export const bunPlugin = unplugin.bun
256274
```
257275

258276
### Filters
@@ -289,14 +307,14 @@ More details can be found in the [Rolldown's documentation](https://rolldown.rs/
289307

290308
## Supported Context
291309

292-
| Context | Rollup | Vite | webpack | esbuild | Rspack | Farm | Rolldown |
293-
| ------------------------------------------------------------------------------------- | :----: | :--: | :-----: | :-----: | :----: | :--: | :------: |
294-
| [`this.parse`](https://rollupjs.org/plugin-development/#this-parse) ||||||||
295-
| [`this.addWatchFile`](https://rollupjs.org/plugin-development/#this-addwatchfile) ||||||||
296-
| [`this.emitFile`](https://rollupjs.org/plugin-development/#this-emitfile)<sup>1</sup> ||||||||
297-
| [`this.getWatchFiles`](https://rollupjs.org/plugin-development/#this-getwatchfiles) ||||||||
298-
| [`this.warn`](https://rollupjs.org/plugin-development/#this-warn) ||||||||
299-
| [`this.error`](https://rollupjs.org/plugin-development/#this-error) ||||||||
310+
| Context | Rollup | Vite | webpack | esbuild | Rspack | Farm | Rolldown | Bun |
311+
| ------------------------------------------------------------------------------------- | :----: | :--: | :-----: | :-----: | :----: | :--: | :------: | :-: |
312+
| [`this.parse`](https://rollupjs.org/plugin-development/#this-parse) |||||||||
313+
| [`this.addWatchFile`](https://rollupjs.org/plugin-development/#this-addwatchfile) |||||||||
314+
| [`this.emitFile`](https://rollupjs.org/plugin-development/#this-emitfile)<sup>1</sup> |||||||||
315+
| [`this.getWatchFiles`](https://rollupjs.org/plugin-development/#this-getwatchfiles) |||||||||
316+
| [`this.warn`](https://rollupjs.org/plugin-development/#this-warn) |||||||||
317+
| [`this.error`](https://rollupjs.org/plugin-development/#this-error) |||||||||
300318

301319
::: info Notice
302320

@@ -309,9 +327,9 @@ More details can be found in the [Rolldown's documentation](https://rolldown.rs/
309327

310328
### Bundler Supported
311329

312-
| Rollup | Vite | webpack | Rspack | esbuild | Farm | Rolldown |
313-
| :--------------------: | :--: | :-----: | :----: | :-----: | :--: | :------: |
314-
|`>=3.1`<sup>1</sup> |||||||
330+
| Rollup | Vite | webpack | Rspack | esbuild | Farm | Rolldown | Bun |
331+
| :--------------------: | :--: | :-----: | :----: | :-----: | :--: | :------: | :-: |
332+
|`>=3.1`<sup>1</sup> ||||||||
315333

316334
::: details Notice
317335

@@ -409,6 +427,9 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (
409427
farm: {
410428
// Farm plugin
411429
},
430+
bun: {
431+
// Bun plugin
432+
},
412433
}
413434
}
414435

@@ -424,6 +445,7 @@ Each of the function takes the same generic factory argument as `createUnplugin`
424445

425446
```ts
426447
import {
448+
createBunPlugin,
427449
createEsbuildPlugin,
428450
createFarmPlugin,
429451
createRolldownPlugin,
@@ -440,4 +462,5 @@ const esbuildPlugin = createEsbuildPlugin(/* factory */)
440462
const webpackPlugin = createWebpackPlugin(/* factory */)
441463
const rspackPlugin = createRspackPlugin(/* factory */)
442464
const farmPlugin = createFarmPlugin(/* factory */)
465+
const bunPlugin = createBunPlugin(/* factory */)
443466
```

docs/index.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar: false
55
hero:
66
name: Unplugin
77
text: The Unified<br>Plugin System
8-
tagline: Supports Vite, Rollup, webpack, esbuild, and every framework built on top of them.
8+
tagline: Supports Vite, Rollup, webpack, esbuild, Bun, and every framework built on top of them.
99
image:
1010
light: /logo_light.svg
1111
dark: /logo_dark.svg
@@ -64,6 +64,12 @@ features:
6464
icon:
6565
src: /features/rolldown.svg
6666

67+
- title: Bun
68+
details: All-in-one JavaScript runtime & toolkit
69+
link: https://bun.com/
70+
icon:
71+
src: /features/bun.svg
72+
6773
- title: More
6874
details: More supported bundlers...
6975
link: /guide/#supported-hooks

docs/public/features/bun.svg

Lines changed: 1 addition & 0 deletions
Loading

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@types/picomatch": "catalog:",
6060
"ansis": "catalog:",
6161
"bumpp": "catalog:",
62+
"bun-types-no-globals": "catalog:",
6263
"esbuild": "catalog:",
6364
"eslint": "catalog:",
6465
"eslint-plugin-format": "catalog:",

pnpm-lock.yaml

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ catalog:
1111
'@types/picomatch': ^4.0.2
1212
ansis: ^4.2.0
1313
bumpp: ^10.3.1
14+
bun-types-no-globals: ^1.2.22
1415
esbuild: ^0.25.12
1516
eslint: ^9.39.0
1617
eslint-plugin-format: ^1.0.2

0 commit comments

Comments
 (0)