Skip to content

Commit

Permalink
feat: allow passing array of array in config.plugins (#1938)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description
1. This could allow the user or developer to compose two or more
different types plugin into one, this is easy for distributing.
2. See test as an example.
3. Progressive porting js plugin.
4. Prerequisite of porting `vite:build-import-analysis`
<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
  • Loading branch information
IWANABETHATGUY authored Aug 11, 2024
1 parent 9974034 commit b4e33d5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/rolldown/src/options/input-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RolldownPlugin } from '../plugin'
import type { RolldownPluginRec } from '../plugin'
import { z } from 'zod'
import * as zodExt from '../utils/zod-ext'
import {
Expand Down Expand Up @@ -27,7 +27,7 @@ const externalSchema = zodExt

const inputOptionsSchema = z.strictObject({
input: inputOptionSchema.optional(),
plugins: zodExt.phantom<RolldownPlugin>().array().optional(),
plugins: zodExt.phantom<RolldownPluginRec>().array().optional(),
external: externalSchema.optional(),
resolve: z
.strictObject({
Expand Down
4 changes: 3 additions & 1 deletion packages/rolldown/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,6 @@ export interface Plugin<A = any> extends OutputPlugin, Partial<PluginHooks> {
api?: A
}

export type RolldownPlugin<A = any> = Plugin<A> | ParallelPlugin | BuiltinPlugin
export type RolldownPlugin<A = any> = Plugin<A> | BuiltinPlugin | ParallelPlugin
// A recursive type definition for `RolldownPlugin`, this type is used internally for `config.plugins`
export type RolldownPluginRec<A = any> = RolldownPlugin<A> | RolldownPlugin<A>[]
4 changes: 2 additions & 2 deletions packages/rolldown/src/plugin/plugin-driver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getLogHandler, normalizeLog } from '../log/logHandler'
import { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARN } from '../log/logging'
import { Plugin, RolldownPlugin } from './'
import { Plugin, RolldownPluginRec } from './'
import { error, logPluginError } from '../log/logs'
import { NormalizedInputOptions } from '../options/normalized-input-options'
import { RollupError } from '../rollup'
Expand Down Expand Up @@ -87,7 +87,7 @@ export class PluginDriver {
}
}

export function getObjectPlugins(plugins: RolldownPlugin[]): Plugin[] {
export function getObjectPlugins(plugins: RolldownPluginRec[]): Plugin[] {
return plugins.filter((plugin) => {
if ('_parallel' in plugin) {
return undefined
Expand Down
35 changes: 35 additions & 0 deletions packages/rolldown/tests/fixtures/plugin/hybrid-plugin/_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect } from 'vitest'
import path from 'node:path'
import { defineTest } from '@tests'
import { loadFallbackPlugin } from 'rolldown/experimental'
import { RolldownPlugin } from '@src/plugin'

const entry = path.join(__dirname, './main.js')

function removeConsoleForPathWithQuery(): RolldownPlugin[] {
return [
loadFallbackPlugin(),
{
name: 'remove-console',
transform(code) {
return code.replace('console.log', '')
},
},
]
}
export default defineTest({
config: {
input: entry,
plugins: [
{
name: 'test-plugin',
banner: () => '/* Lorem ipsum */',
},
removeConsoleForPathWithQuery(),
],
},
afterTest: async (output) => {
expect(output.output[0].code).toContain('/* Lorem ipsum */')
await import('./assert.mjs')
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { a } from './dist/main'
import assert from 'node:assert'

assert(a === 1)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = console.log(1)

0 comments on commit b4e33d5

Please sign in to comment.