Skip to content

Commit

Permalink
feat: basic support for generateBundle and writeBundle (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
PengBoUESTC committed Mar 31, 2024
1 parent 1c9f9bf commit 343cfb9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ pub struct BindingPluginOptions {
Option<MaybeAsyncJsCallback<(String, RenderedChunk), Option<BindingHookRenderChunkOutput>>>,

#[serde(skip_deserializing)]
#[napi(ts_type = "(bundle: Outputs, isWrite: boolean) => MaybePromise<VoidNullable>")]
#[napi(ts_type = "(bundle: BindingOutputs, isWrite: boolean) => MaybePromise<VoidNullable>")]
pub generate_bundle: Option<MaybeAsyncJsCallback<(BindingOutputs, bool), ()>>,

#[serde(skip_deserializing)]
#[napi(ts_type = "(bundle: Outputs) => MaybePromise<VoidNullable>")]
#[napi(ts_type = "(bundle: BindingOutputs) => MaybePromise<VoidNullable>")]
pub write_bundle: Option<MaybeAsyncJsCallback<BindingOutputs, ()>>,
}

Expand Down
4 changes: 2 additions & 2 deletions packages/rolldown/src/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ export interface BindingPluginOptions {
chunk: RenderedChunk,
) => MaybePromise<VoidNullable<BindingHookRenderChunkOutput>>
generateBundle?: (
bundle: Outputs,
bundle: BindingOutputs,
isWrite: boolean,
) => MaybePromise<VoidNullable>
writeBundle?: (bundle: Outputs) => MaybePromise<VoidNullable>
writeBundle?: (bundle: BindingOutputs) => MaybePromise<VoidNullable>
}

export interface BindingRenderedModule {
Expand Down
29 changes: 29 additions & 0 deletions packages/rolldown/src/plugin/bindingify-output-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { normalizeHook } from '../utils/normalize-hook'
import type { BindingPluginOptions } from '../binding'

import type { Plugin } from './index'

export function bindingifyGenerateBundle(
hook?: Plugin['generateBundle'],
): BindingPluginOptions['generateBundle'] {
if (!hook) {
return undefined
}
const [handler, _optionsIgnoredSofar] = normalizeHook(hook)

return async (bundle, isWrite) => {
handler.call(null, bundle, isWrite)
}
}
export function bindingifyWriteBundle(
hook?: Plugin['writeBundle'],
): BindingPluginOptions['writeBundle'] {
if (!hook) {
return undefined
}
const [handler, _optionsIgnoredSofar] = normalizeHook(hook)

return async (bundle) => {
handler.call(null, bundle)
}
}
7 changes: 7 additions & 0 deletions packages/rolldown/src/plugin/bindingify-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
bindingifyTransform,
} from './bindingify-build-hooks'

import {
bindingifyGenerateBundle,
bindingifyWriteBundle,
} from './bindingify-output-hooks'

import type { Plugin } from './index'
import { RolldownNormalizedInputOptions } from '../options/input-options'

Expand All @@ -24,5 +29,7 @@ export function bindingifyPlugin(
transform: bindingifyTransform(plugin.transform),
load: bindingifyLoad(plugin.load),
renderChunk: bindingifyRenderChunk(plugin.renderChunk),
generateBundle: bindingifyGenerateBundle(plugin.generateBundle),
writeBundle: bindingifyWriteBundle(plugin.writeBundle),
}
}
6 changes: 6 additions & 0 deletions packages/rolldown/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BindingHookResolveIdExtraOptions,
BindingPluginContext,
RenderedChunk,
BindingOutputs,
} from '../binding'
import { RolldownNormalizedInputOptions } from '../options/input-options'
import { AnyFn, AnyObj } from '../types/utils'
Expand Down Expand Up @@ -82,4 +83,9 @@ export interface Plugin {

buildEnd?: Hook<(this: null, err?: string) => MaybePromise<NullValue>>
// --- Output hooks ---

generateBundle?: Hook<
(bundle: BindingOutputs, isWrite: boolean) => MaybePromise<NullValue>
>
writeBundle?: Hook<(bundle: BindingOutputs) => MaybePromise<NullValue>>
}

0 comments on commit 343cfb9

Please sign in to comment.