Skip to content

Commit

Permalink
feat(node/plugin): support renderStart hook (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
PengBoUESTC committed Apr 11, 2024
1 parent 8da5ab8 commit c1194e6
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 10 deletions.
1 change: 1 addition & 0 deletions crates/rolldown/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl Bundler {
async fn bundle_up(&mut self, is_write: bool) -> BatchedResult<BundleOutput> {
tracing::trace!("Options {:#?}", self.options);
let mut link_stage_output = self.try_build().await?;
self.plugin_driver.render_start().await?;

let mut bundle_stage =
BundleStage::new(&mut link_stage_output, &self.options, &self.plugin_driver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ pub struct BindingPluginOptions {
pub render_chunk:
Option<MaybeAsyncJsCallback<(String, RenderedChunk), Option<BindingHookRenderChunkOutput>>>,

#[serde(skip_deserializing)]
#[napi(ts_type = "() => void")]
pub render_start: Option<MaybeAsyncJsCallback<(), ()>>,

#[serde(skip_deserializing)]
#[napi(ts_type = "(bundle: BindingOutputs, isWrite: boolean) => MaybePromise<VoidNullable>")]
pub generate_bundle: Option<MaybeAsyncJsCallback<(BindingOutputs, bool), ()>>,
Expand Down
10 changes: 10 additions & 0 deletions crates/rolldown_binding/src/options/plugin/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ impl Plugin for JsPlugin {

// --- Output hooks ---

async fn render_start(
&self,
_ctx: &rolldown_plugin::SharedPluginContext,
) -> rolldown_plugin::HookNoopReturn {
if let Some(cb) = &self.render_start {
cb.await_call(()).await?;
}
Ok(())
}

async fn generate_bundle(
&self,
_ctx: &rolldown_plugin::SharedPluginContext,
Expand Down
5 changes: 5 additions & 0 deletions crates/rolldown_plugin/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ pub trait Plugin: Any + Debug + Send + Sync + 'static {

// --- Generate hooks ---

#[allow(clippy::ptr_arg)]
async fn render_start(&self, _ctx: &SharedPluginContext) -> HookNoopReturn {
Ok(())
}

#[allow(clippy::ptr_arg)]
async fn generate_bundle(
&self,
Expand Down
6 changes: 6 additions & 0 deletions crates/rolldown_plugin/src/plugin_driver/output_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use futures::future::join_all;
use rolldown_common::Output;

impl PluginDriver {
pub async fn render_start(&self) -> HookNoopReturn {
for (plugin, ctx) in &self.plugins {
plugin.render_start(ctx).await?;
}
Ok(())
}
pub async fn generate_bundle(&self, bundle: &Vec<Output>, is_write: bool) -> HookNoopReturn {
for (plugin, ctx) in &self.plugins {
plugin.generate_bundle(ctx, bundle, is_write).await?;
Expand Down
1 change: 1 addition & 0 deletions packages/rolldown/src/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface BindingPluginOptions {
code: string,
chunk: RenderedChunk,
) => MaybePromise<VoidNullable<BindingHookRenderChunkOutput>>
renderStart?: () => void
generateBundle?: (
bundle: BindingOutputs,
isWrite: boolean,
Expand Down
42 changes: 33 additions & 9 deletions packages/rolldown/src/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,28 @@ function requireNative() {
}
}
} else if (process.arch === 'arm') {
try {
return require('./rolldown-binding.linux-arm-gnueabihf.node')
} catch (e) {
loadErrors.push(e)
}
try {
return require('@rolldown/binding-linux-arm-gnueabihf')
} catch (e) {
loadErrors.push(e)
if (isMusl()) {
try {
return require('./rolldown-binding.linux-arm-musleabihf.node')
} catch (e) {
loadErrors.push(e)
}
try {
return require('@rolldown/binding-linux-arm-musleabihf')
} catch (e) {
loadErrors.push(e)
}
} else {
try {
return require('./rolldown-binding.linux-arm-gnueabihf.node')
} catch (e) {
loadErrors.push(e)
}
try {
return require('@rolldown/binding-linux-arm-gnueabihf')
} catch (e) {
loadErrors.push(e)
}
}
} else if (process.arch === 'riscv64') {
if (isMusl()) {
Expand All @@ -280,6 +293,17 @@ function requireNative() {
loadErrors.push(e)
}
}
} else if (process.arch === 'ppc64') {
try {
return require('./rolldown-binding.linux-ppc64-gnu.node')
} catch (e) {
loadErrors.push(e)
}
try {
return require('@rolldown/binding-linux-ppc64-gnu')
} catch (e) {
loadErrors.push(e)
}
} else if (process.arch === 's390x') {
try {
return require('./rolldown-binding.linux-s390x-gnu.node')
Expand Down
17 changes: 16 additions & 1 deletion packages/rolldown/src/plugin/bindingify-output-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { normalizeHook } from '../utils/normalize-hook'
import type { BindingPluginOptions } from '../binding'

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

export function bindingifyRenderStart(
outputOptions: NormalizedOutputOptions,
options: RolldownNormalizedInputOptions,
hook?: Plugin['renderStart'],
): BindingPluginOptions['renderStart'] {
if (!hook) {
return undefined
}
const [handler, _optionsIgnoredSofar] = normalizeHook(hook)

return async () => {
handler.call(null, outputOptions, options)
}
}
export function bindingifyGenerateBundle(
hook?: Plugin['generateBundle'],
): BindingPluginOptions['generateBundle'] {
Expand Down
6 changes: 6 additions & 0 deletions packages/rolldown/src/plugin/bindingify-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './bindingify-build-hooks'

import {
bindingifyRenderStart,
bindingifyGenerateBundle,
bindingifyWriteBundle,
} from './bindingify-output-hooks'
Expand All @@ -31,6 +32,11 @@ export function bindingifyPlugin(
transform: bindingifyTransform(plugin.transform),
load: bindingifyLoad(plugin.load),
renderChunk: bindingifyRenderChunk(outputOptions, plugin.renderChunk),
renderStart: bindingifyRenderStart(
outputOptions,
options,
plugin.renderStart,
),
generateBundle: bindingifyGenerateBundle(plugin.generateBundle),
writeBundle: bindingifyWriteBundle(plugin.writeBundle),
}
Expand Down
9 changes: 9 additions & 0 deletions packages/rolldown/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
BindingPluginContext,
RenderedChunk,
BindingOutputs,
BindingOutputOptions,
BindingInputOptions,
} from '../binding'
import { RolldownNormalizedInputOptions } from '../options/input-options'
import { AnyFn, AnyObj, NullValue } from '../types/utils'
Expand Down Expand Up @@ -94,6 +96,13 @@ export interface Plugin {
buildEnd?: Hook<(this: null, err?: string) => MaybePromise<NullValue>>
// --- Output hooks ---

renderStart?: Hook<
(
outputOptions: BindingOutputOptions,
inputOptions: RolldownNormalizedInputOptions,
) => MaybePromise<NullValue>
>

generateBundle?: Hook<
(bundle: BindingOutputs, isWrite: boolean) => MaybePromise<NullValue>
>
Expand Down
30 changes: 30 additions & 0 deletions packages/rolldown/tests/fixtures/plugin/render-start/_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, vi } from 'vitest'
import path from 'node:path'
import { defineTest } from '@tests'

const entry = path.join(__dirname, './main.js')
const entryFileNames = '[name]-render-start.js'

const renderStartFn = vi.fn()

export default defineTest({
config: {
input: entry,
output: {
entryFileNames,
},
plugins: [
{
name: 'test-plugin-render-start',
renderStart: (outputOptions, inputOptions) => {
renderStartFn()
expect((inputOptions.input as string[])[0]).toBe(entry)
expect(outputOptions.entryFileNames).toBe(entryFileNames)
},
},
],
},
afterTest: () => {
expect(renderStartFn).toHaveBeenCalledTimes(1)
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log()

0 comments on commit c1194e6

Please sign in to comment.