diff --git a/src/index.ts b/src/index.ts index f6ed188..f8793cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ export * from './options' export function RequireCJS(userOptions: Options = {}): Plugin { const { include, exclude, order, shouldTransform } = resolveOptions(userOptions) + return { name: 'rolldown-plugin-require-cjs', async buildStart() { diff --git a/src/options.ts b/src/options.ts index 18e3140..cd46f64 100644 --- a/src/options.ts +++ b/src/options.ts @@ -2,6 +2,11 @@ type FilterPattern = Array | string | RegExp type Awaitable = T | Promise +export type TransformFn = ( + id: string, + importer: string, +) => Awaitable + export interface Options { include?: FilterPattern exclude?: FilterPattern @@ -15,24 +20,26 @@ export interface Options { * @param importer The module ID (path) of the importer. * @returns A boolean or a promise that resolves to a boolean, or `undefined`. */ - shouldTransform?: ( - id: string, - importer: string, - ) => Awaitable + shouldTransform?: string[] | TransformFn } type Overwrite = Pick> & U export type OptionsResolved = Overwrite< Required, - Pick + Pick & { shouldTransform?: TransformFn } > export function resolveOptions(options: Options): OptionsResolved { + let { shouldTransform } = options + if (Array.isArray(shouldTransform)) { + shouldTransform = (id) => (shouldTransform as string[]).includes(id) + } + return { include: options.include || [/\.[cm]?[jt]sx?$/], exclude: options.exclude || [/node_modules/, /\.d\.[cm]?ts$/], order: 'order' in options ? options.order : 'pre', - shouldTransform: options.shouldTransform, + shouldTransform, } }