Skip to content

Commit f1e6c79

Browse files
committed
feat: add rolldown support
1 parent 32888b0 commit f1e6c79

File tree

5 files changed

+175
-1
lines changed

5 files changed

+175
-1
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@ampproject/remapping": "^2.3.0",
4949
"@antfu/eslint-config": "^2.8.0",
5050
"@antfu/ni": "^0.21.12",
51+
"@rolldown/node": "^0.0.5",
5152
"@rspack/cli": "^0.5.6",
5253
"@rspack/core": "^0.5.6",
5354
"@types/fs-extra": "^11.0.4",

pnpm-lock.yaml

+135
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/define.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getEsbuildPlugin } from './esbuild'
2+
import { getRolldownPlugin } from './rolldown'
23
import { getRollupPlugin } from './rollup'
34
import { getRspackPlugin } from './rspack'
45
import type { UnpluginFactory, UnpluginInstance } from './types'
@@ -18,6 +19,9 @@ export function createUnplugin<UserOptions, Nested extends boolean = boolean>(
1819
get vite() {
1920
return getVitePlugin(factory)
2021
},
22+
get rolldown() {
23+
return getRolldownPlugin(factory)
24+
},
2125
get webpack() {
2226
return getWebpackPlugin(factory)
2327
},
@@ -49,6 +53,12 @@ export function createVitePlugin<UserOptions, Nested extends boolean = boolean>(
4953
return getVitePlugin(factory)
5054
}
5155

56+
export function createRolldownPlugin<UserOptions, Nested extends boolean = boolean>(
57+
factory: UnpluginFactory<UserOptions, Nested>,
58+
) {
59+
return getRolldownPlugin(factory)
60+
}
61+
5262
export function createWebpackPlugin<UserOptions, Nested extends boolean = boolean>(
5363
factory: UnpluginFactory<UserOptions, Nested>,
5464
) {

src/rolldown/index.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { toRollupPlugin } from '../rollup'
2+
import type { RolldownPlugin, UnpluginContextMeta, UnpluginFactory, UnpluginInstance } from '../types'
3+
import { toArray } from '../utils'
4+
5+
export function getRolldownPlugin<UserOptions = Record<string, never>, Nested extends boolean = boolean>(
6+
factory: UnpluginFactory<UserOptions, Nested>,
7+
) {
8+
return ((userOptions?: UserOptions) => {
9+
const meta: UnpluginContextMeta = {
10+
framework: 'rolldown',
11+
}
12+
const rawPlugins = toArray(factory(userOptions!, meta))
13+
14+
const plugins = rawPlugins.map((rawPlugin) => {
15+
const plugin = toRollupPlugin(rawPlugin, false) as RolldownPlugin
16+
if (rawPlugin.rolldown)
17+
Object.assign(plugin, rawPlugin.rolldown)
18+
19+
return plugin
20+
})
21+
22+
return plugins.length === 1 ? plugins[0] : plugins
23+
}) as UnpluginInstance<UserOptions, Nested>['rolldown']
24+
}

src/types.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AstNode, EmittedAsset, PluginContextMeta as RollupContextMeta, Plugin as RollupPlugin, SourceMapInput } from 'rollup'
22
import type { Compiler as WebpackCompiler, WebpackPluginInstance } from 'webpack'
33
import type { Plugin as VitePlugin } from 'vite'
4+
import type { Plugin as RolldownPlugin } from '@rolldown/node'
45
import type { BuildOptions, Plugin as EsbuildPlugin, Loader } from 'esbuild'
56
import type { Compiler as RspackCompiler, RspackPluginInstance } from '@rspack/core'
67
import type VirtualModulesPlugin from 'webpack-virtual-modules'
@@ -9,6 +10,7 @@ import type { EsbuildPluginBuild } from './esbuild'
910
export {
1011
EsbuildPlugin,
1112
RollupPlugin,
13+
RolldownPlugin,
1214
VitePlugin,
1315
WebpackPluginInstance,
1416
RspackPluginInstance,
@@ -71,6 +73,7 @@ export interface UnpluginOptions {
7173
webpack?: (compiler: WebpackCompiler) => void
7274
rspack?: (compiler: RspackCompiler) => void
7375
vite?: Partial<VitePlugin>
76+
rolldown?: Partial<RolldownPlugin>
7477
esbuild?: {
7578
// using regexp in esbuild improves performance
7679
onResolveFilter?: RegExp
@@ -99,14 +102,15 @@ export type UnpluginFactoryOutput<UserOptions, Return> = undefined extends UserO
99102
export interface UnpluginInstance<UserOptions, Nested extends boolean = boolean> {
100103
rollup: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RollupPlugin> : RollupPlugin>
101104
vite: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<VitePlugin> : VitePlugin>
105+
rolldown: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RolldownPlugin> : RolldownPlugin>
102106
webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>
103107
rspack: UnpluginFactoryOutput<UserOptions, RspackPluginInstance>
104108
esbuild: UnpluginFactoryOutput<UserOptions, EsbuildPlugin>
105109
raw: UnpluginFactory<UserOptions, Nested>
106110
}
107111

108112
export type UnpluginContextMeta = Partial<RollupContextMeta> & ({
109-
framework: 'rollup' | 'vite'
113+
framework: 'rollup' | 'vite' | 'rolldown'
110114
} | {
111115
framework: 'webpack'
112116
webpack: {

0 commit comments

Comments
 (0)