Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rolldown support #370

Merged
merged 1 commit into from
Mar 8, 2024
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@ampproject/remapping": "^2.3.0",
"@antfu/eslint-config": "^2.8.0",
"@antfu/ni": "^0.21.12",
"@rolldown/node": "^0.0.5",
"@rspack/cli": "^0.5.6",
"@rspack/core": "^0.5.6",
"@types/fs-extra": "^11.0.4",
Expand Down
135 changes: 135 additions & 0 deletions pnpm-lock.yaml

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

10 changes: 10 additions & 0 deletions src/define.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getEsbuildPlugin } from './esbuild'
import { getRolldownPlugin } from './rolldown'
import { getRollupPlugin } from './rollup'
import { getRspackPlugin } from './rspack'
import type { UnpluginFactory, UnpluginInstance } from './types'
Expand All @@ -18,6 +19,9 @@ export function createUnplugin<UserOptions, Nested extends boolean = boolean>(
get vite() {
return getVitePlugin(factory)
},
get rolldown() {
return getRolldownPlugin(factory)
},
get webpack() {
return getWebpackPlugin(factory)
},
Expand Down Expand Up @@ -49,6 +53,12 @@ export function createVitePlugin<UserOptions, Nested extends boolean = boolean>(
return getVitePlugin(factory)
}

export function createRolldownPlugin<UserOptions, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
return getRolldownPlugin(factory)
}

export function createWebpackPlugin<UserOptions, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
Expand Down
24 changes: 24 additions & 0 deletions src/rolldown/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { toRollupPlugin } from '../rollup'
import type { RolldownPlugin, UnpluginContextMeta, UnpluginFactory, UnpluginInstance } from '../types'
import { toArray } from '../utils'

export function getRolldownPlugin<UserOptions = Record<string, never>, Nested extends boolean = boolean>(
factory: UnpluginFactory<UserOptions, Nested>,
) {
return ((userOptions?: UserOptions) => {
const meta: UnpluginContextMeta = {
framework: 'rolldown',
}
const rawPlugins = toArray(factory(userOptions!, meta))

const plugins = rawPlugins.map((rawPlugin) => {
const plugin = toRollupPlugin(rawPlugin, false) as RolldownPlugin
if (rawPlugin.rolldown)
Object.assign(plugin, rawPlugin.rolldown)

return plugin
})

return plugins.length === 1 ? plugins[0] : plugins
}) as UnpluginInstance<UserOptions, Nested>['rolldown']
}
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AstNode, EmittedAsset, PluginContextMeta as RollupContextMeta, Plugin as RollupPlugin, SourceMapInput } from 'rollup'
import type { Compiler as WebpackCompiler, WebpackPluginInstance } from 'webpack'
import type { Plugin as VitePlugin } from 'vite'
import type { Plugin as RolldownPlugin } from '@rolldown/node'
import type { BuildOptions, Plugin as EsbuildPlugin, Loader } from 'esbuild'
import type { Compiler as RspackCompiler, RspackPluginInstance } from '@rspack/core'
import type VirtualModulesPlugin from 'webpack-virtual-modules'
Expand All @@ -9,6 +10,7 @@ import type { EsbuildPluginBuild } from './esbuild'
export {
EsbuildPlugin,
RollupPlugin,
RolldownPlugin,
VitePlugin,
WebpackPluginInstance,
RspackPluginInstance,
Expand Down Expand Up @@ -71,6 +73,7 @@ export interface UnpluginOptions {
webpack?: (compiler: WebpackCompiler) => void
rspack?: (compiler: RspackCompiler) => void
vite?: Partial<VitePlugin>
rolldown?: Partial<RolldownPlugin>
esbuild?: {
// using regexp in esbuild improves performance
onResolveFilter?: RegExp
Expand Down Expand Up @@ -99,14 +102,15 @@ export type UnpluginFactoryOutput<UserOptions, Return> = undefined extends UserO
export interface UnpluginInstance<UserOptions, Nested extends boolean = boolean> {
rollup: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RollupPlugin> : RollupPlugin>
vite: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<VitePlugin> : VitePlugin>
rolldown: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RolldownPlugin> : RolldownPlugin>
webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>
rspack: UnpluginFactoryOutput<UserOptions, RspackPluginInstance>
esbuild: UnpluginFactoryOutput<UserOptions, EsbuildPlugin>
raw: UnpluginFactory<UserOptions, Nested>
}

export type UnpluginContextMeta = Partial<RollupContextMeta> & ({
framework: 'rollup' | 'vite'
framework: 'rollup' | 'vite' | 'rolldown'
} | {
framework: 'webpack'
webpack: {
Expand Down