Skip to content

Commit ca8a2b0

Browse files
committed
feat: add unloader framework
See https://github.com/sxzz/unloader
1 parent de0ea62 commit ca8a2b0

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"simple-git-hooks": "^2.11.1",
7171
"tsdown": "^0.5.7",
7272
"typescript": "~5.7.3",
73+
"unloader": "^0.2.2",
7374
"unplugin": "workspace:*",
7475
"vite": "^6.0.11",
7576
"vitest": "^3.0.5",

pnpm-lock.yaml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/define.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getFarmPlugin } from './farm'
44
import { getRolldownPlugin } from './rolldown'
55
import { getRollupPlugin } from './rollup'
66
import { getRspackPlugin } from './rspack'
7+
import { getUnloaderPlugin } from './unloader'
78
import { getVitePlugin } from './vite'
89
import { getWebpackPlugin } from './webpack'
910

@@ -33,6 +34,9 @@ export function createUnplugin<UserOptions, Nested extends boolean = boolean>(
3334
get farm() {
3435
return getFarmPlugin(factory)
3536
},
37+
get unloader() {
38+
return getUnloaderPlugin(factory)
39+
},
3640
get raw() {
3741
return factory
3842
},
@@ -81,3 +85,9 @@ export function createFarmPlugin<UserOptions, Nested extends boolean = boolean>(
8185
): UnpluginInstance<UserOptions>['farm'] {
8286
return getFarmPlugin(factory)
8387
}
88+
89+
export function createUnloaderPlugin<UserOptions, Nested extends boolean = boolean>(
90+
factory: UnpluginFactory<UserOptions, Nested>,
91+
): UnpluginInstance<UserOptions>['unloader'] {
92+
return getUnloaderPlugin(factory)
93+
}

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Compilation as RspackCompilation, Compiler as RspackCompiler, Load
33
import type { BuildOptions, Plugin as EsbuildPlugin, Loader, PluginBuild } from 'esbuild'
44
import type { Plugin as RolldownPlugin } from 'rolldown'
55
import type { AstNode, EmittedAsset, PluginContextMeta as RollupContextMeta, Plugin as RollupPlugin, SourceMapInput } from 'rollup'
6+
import type { Plugin as UnloaderPlugin } from 'unloader'
67
import type { Plugin as VitePlugin } from 'vite'
78
import type { Compilation as WebpackCompilation, Compiler as WebpackCompiler, LoaderContext as WebpackLoaderContext, WebpackPluginInstance } from 'webpack'
89
import type VirtualModulesPlugin from 'webpack-virtual-modules'
@@ -13,6 +14,7 @@ export type {
1314
RollupPlugin,
1415
RspackCompiler,
1516
RspackPluginInstance,
17+
UnloaderPlugin,
1618
VitePlugin,
1719
WebpackCompiler,
1820
WebpackPluginInstance,
@@ -90,6 +92,7 @@ export interface UnpluginOptions {
9092
webpack?: (compiler: WebpackCompiler) => void
9193
rspack?: (compiler: RspackCompiler) => void
9294
vite?: Partial<VitePlugin>
95+
unloader?: Partial<UnloaderPlugin>
9396
rolldown?: Partial<RolldownPlugin>
9497
esbuild?: {
9598
// using regexp in esbuild improves performance
@@ -124,12 +127,13 @@ export interface UnpluginInstance<UserOptions, Nested extends boolean = boolean>
124127
webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>
125128
rspack: UnpluginFactoryOutput<UserOptions, RspackPluginInstance>
126129
esbuild: UnpluginFactoryOutput<UserOptions, EsbuildPlugin>
130+
unloader: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<UnloaderPlugin> : UnloaderPlugin>
127131
farm: UnpluginFactoryOutput<UserOptions, FarmPlugin>
128132
raw: UnpluginFactory<UserOptions, Nested>
129133
}
130134

131135
export type UnpluginContextMeta = Partial<RollupContextMeta> & ({
132-
framework: 'rollup' | 'vite' | 'rolldown' | 'farm'
136+
framework: 'rollup' | 'vite' | 'rolldown' | 'farm' | 'unloader'
133137
} | {
134138
framework: 'webpack'
135139
webpack: { compiler: WebpackCompiler }

src/unloader/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { UnloaderPlugin, UnpluginContextMeta, UnpluginFactory, UnpluginInstance } from '../types'
2+
import { toRollupPlugin } from '../rollup'
3+
import { toArray } from '../utils/general'
4+
5+
export function getUnloaderPlugin<UserOptions = Record<string, never>, Nested extends boolean = boolean>(
6+
factory: UnpluginFactory<UserOptions, Nested>,
7+
) {
8+
return ((userOptions?: UserOptions) => {
9+
const meta: UnpluginContextMeta = {
10+
framework: 'unloader',
11+
}
12+
const rawPlugins = toArray(factory(userOptions!, meta))
13+
14+
const plugins = rawPlugins.map((rawPlugin) => {
15+
const plugin = toRollupPlugin(rawPlugin, false) as UnloaderPlugin
16+
if (rawPlugin.unloader)
17+
Object.assign(plugin, rawPlugin.unloader)
18+
19+
return plugin
20+
})
21+
22+
return plugins.length === 1 ? plugins[0] : plugins
23+
}) as UnpluginInstance<UserOptions, Nested>['unloader']
24+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy