diff --git a/package.json b/package.json index a911e33c..b2e77b2b 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "simple-git-hooks": "^2.11.1", "tsdown": "^0.5.7", "typescript": "~5.7.3", + "unloader": "^0.2.2", "unplugin": "workspace:*", "vite": "^6.0.11", "vitest": "^3.0.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5697bbd0..8183201a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -87,6 +87,9 @@ importers: typescript: specifier: ~5.7.3 version: 5.7.3 + unloader: + specifier: ^0.2.2 + version: 0.2.2 unplugin: specifier: workspace:* version: 'link:' @@ -1994,6 +1997,9 @@ packages: birpc@0.2.19: resolution: {integrity: sha512-5WeXXAvTmitV1RqJFppT5QtUiz2p1mRSYU000Jkft5ZUCLJIk4uQriYNO50HknxKwM6jd8utNc66K1qGIwwWBQ==} + birpc@2.2.0: + resolution: {integrity: sha512-1/22obknhoj56PcE+pZPp6AbWDdY55M81/ofpPW3Ltlp9Eh4zoFFLswvZmNpRTb790CY5tsNfgbYeNOqIARJfQ==} + bl@4.1.0: resolution: {integrity: sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==} @@ -4792,6 +4798,9 @@ packages: unconfig@0.6.1: resolution: {integrity: sha512-cVU+/sPloZqOyJEAfNwnQSFCzFrZm85vcVkryH7lnlB/PiTycUkAjt5Ds79cfIshGOZ+M5v3PBDnKgpmlE5DtA==} + unconfig@7.0.0: + resolution: {integrity: sha512-G5CJSoG6ZTxgzCJblEfgpdRK2tos9+UdD2WtecDUVfImzQ0hFjwpH5RVvGMhP4pRpC9ML7NrC4qBsBl0Ttj35A==} + undici-types@6.20.0: resolution: {integrity: sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==} @@ -4818,6 +4827,10 @@ packages: resolution: {integrity: sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==} engines: {node: '>= 10.0.0'} + unloader@0.2.2: + resolution: {integrity: sha512-kCpDq9Dr+yyOBrV4l+nnbhAsT1C10yWokhbzJjqRNowSt8LQTE+O3h+jIB7C50TC3ZYGqfYqETxB0RnpuCsdQA==} + engines: {node: ^18.19.0 || >=20.6.0} + unocss@65.4.3: resolution: {integrity: sha512-mwSVi0ovPxaDv58yFB7Vm5v1x/q/pUc7aTh7SJbeYoRrpbUGdKiVf20YSQfMqmBNXV9CFDr4o6tabP/58as6RQ==} engines: {node: '>=14'} @@ -7256,6 +7269,8 @@ snapshots: birpc@0.2.19: {} + birpc@2.2.0: {} + bl@4.1.0: dependencies: buffer: 5.7.1 @@ -10435,6 +10450,12 @@ snapshots: transitivePeerDependencies: - supports-color + unconfig@7.0.0: + dependencies: + '@antfu/utils': 8.1.0 + defu: 6.1.4 + jiti: 2.4.2 + undici-types@6.20.0: {} unist-util-is@6.0.0: @@ -10464,6 +10485,15 @@ snapshots: universalify@2.0.1: {} + unloader@0.2.2: + dependencies: + '@ampproject/remapping': 2.3.0 + birpc: 2.2.0 + debug: 4.4.0 + unconfig: 7.0.0 + transitivePeerDependencies: + - supports-color + unocss@65.4.3(postcss@8.5.1)(rollup@4.34.2)(vite@6.0.11(@types/node@22.13.1)(jiti@2.4.2)(terser@5.37.0)(tsx@4.19.2)(yaml@2.7.0))(vue@3.5.13(typescript@5.7.3)): dependencies: '@unocss/astro': 65.4.3(rollup@4.34.2)(vite@6.0.11(@types/node@22.13.1)(jiti@2.4.2)(terser@5.37.0)(tsx@4.19.2)(yaml@2.7.0))(vue@3.5.13(typescript@5.7.3)) diff --git a/src/define.ts b/src/define.ts index 46fcd009..8eff312d 100644 --- a/src/define.ts +++ b/src/define.ts @@ -4,6 +4,7 @@ import { getFarmPlugin } from './farm' import { getRolldownPlugin } from './rolldown' import { getRollupPlugin } from './rollup' import { getRspackPlugin } from './rspack' +import { getUnloaderPlugin } from './unloader' import { getVitePlugin } from './vite' import { getWebpackPlugin } from './webpack' @@ -33,6 +34,9 @@ export function createUnplugin( get farm() { return getFarmPlugin(factory) }, + get unloader() { + return getUnloaderPlugin(factory) + }, get raw() { return factory }, @@ -81,3 +85,9 @@ export function createFarmPlugin( ): UnpluginInstance['farm'] { return getFarmPlugin(factory) } + +export function createUnloaderPlugin( + factory: UnpluginFactory, +): UnpluginInstance['unloader'] { + return getUnloaderPlugin(factory) +} diff --git a/src/types.ts b/src/types.ts index accc1f47..8e7bf026 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,6 +3,7 @@ import type { Compilation as RspackCompilation, Compiler as RspackCompiler, Load import type { BuildOptions, Plugin as EsbuildPlugin, Loader, PluginBuild } from 'esbuild' import type { Plugin as RolldownPlugin } from 'rolldown' import type { AstNode, EmittedAsset, PluginContextMeta as RollupContextMeta, Plugin as RollupPlugin, SourceMapInput } from 'rollup' +import type { Plugin as UnloaderPlugin } from 'unloader' import type { Plugin as VitePlugin } from 'vite' import type { Compilation as WebpackCompilation, Compiler as WebpackCompiler, LoaderContext as WebpackLoaderContext, WebpackPluginInstance } from 'webpack' import type VirtualModulesPlugin from 'webpack-virtual-modules' @@ -13,6 +14,7 @@ export type { RollupPlugin, RspackCompiler, RspackPluginInstance, + UnloaderPlugin, VitePlugin, WebpackCompiler, WebpackPluginInstance, @@ -90,6 +92,7 @@ export interface UnpluginOptions { webpack?: (compiler: WebpackCompiler) => void rspack?: (compiler: RspackCompiler) => void vite?: Partial + unloader?: Partial rolldown?: Partial esbuild?: { // using regexp in esbuild improves performance @@ -124,12 +127,13 @@ export interface UnpluginInstance webpack: UnpluginFactoryOutput rspack: UnpluginFactoryOutput esbuild: UnpluginFactoryOutput + unloader: UnpluginFactoryOutput : UnloaderPlugin> farm: UnpluginFactoryOutput raw: UnpluginFactory } export type UnpluginContextMeta = Partial & ({ - framework: 'rollup' | 'vite' | 'rolldown' | 'farm' + framework: 'rollup' | 'vite' | 'rolldown' | 'farm' | 'unloader' } | { framework: 'webpack' webpack: { compiler: WebpackCompiler } diff --git a/src/unloader/index.ts b/src/unloader/index.ts new file mode 100644 index 00000000..4a723bea --- /dev/null +++ b/src/unloader/index.ts @@ -0,0 +1,24 @@ +import type { UnloaderPlugin, UnpluginContextMeta, UnpluginFactory, UnpluginInstance } from '../types' +import { toRollupPlugin } from '../rollup' +import { toArray } from '../utils/general' + +export function getUnloaderPlugin, Nested extends boolean = boolean>( + factory: UnpluginFactory, +) { + return ((userOptions?: UserOptions) => { + const meta: UnpluginContextMeta = { + framework: 'unloader', + } + const rawPlugins = toArray(factory(userOptions!, meta)) + + const plugins = rawPlugins.map((rawPlugin) => { + const plugin = toRollupPlugin(rawPlugin, false) as UnloaderPlugin + if (rawPlugin.unloader) + Object.assign(plugin, rawPlugin.unloader) + + return plugin + }) + + return plugins.length === 1 ? plugins[0] : plugins + }) as UnpluginInstance['unloader'] +}