import type * as rolldown from "rolldown"; import type * as vite from "vite"; import type { BasePluginOptions } from "./options.js"; export interface PluginInput { shared?: Omit, "name">; rolldown?: Omit, "name">; vite?: Omit, "name">; } export interface NullablePluginOutput { rolldown: (options: BasePluginOptions) => rolldown.Plugin | null; vite: (options: BasePluginOptions) => vite.Plugin | null; } export interface PluginOutput { rolldown: (options: BasePluginOptions) => rolldown.Plugin; vite: (options: BasePluginOptions) => vite.Plugin; } export function createPlugin( pluginName: TName, make: (options: BasePluginOptions) => PluginInput, ): PluginOutput; export function createPlugin( pluginName: TName, make: (options: BasePluginOptions) => PluginInput | undefined, ): NullablePluginOutput; export function createPlugin( pluginName: TName, make: (options: BasePluginOptions) => PluginInput | undefined, ): PluginOutput | NullablePluginOutput { const name = `distilled-cloudflare:${pluginName}`; return { rolldown: (options: BasePluginOptions) => { const plugin = make(options); if (!plugin) return null; return { name, ...plugin.shared, ...plugin.rolldown, }; }, vite: (options: BasePluginOptions) => { const plugin = make(options); if (!plugin) return null; return { name, sharedDuringBuild: true, applyToEnvironment(environment) { return environment.name !== "client"; }, ...plugin.shared, ...plugin.vite, } as vite.Plugin; }, }; }