-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathtypes.ts
164 lines (144 loc) · 6.07 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import type { CompilationContext as FarmCompilationContext, JsPlugin as FarmPlugin } from '@farmfe/core'
import type { Compilation as RspackCompilation, Compiler as RspackCompiler, LoaderContext as RspackLoaderContext, RspackPluginInstance } from '@rspack/core'
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 VitePlugin } from 'vite'
import type { Compilation as WebpackCompilation, Compiler as WebpackCompiler, LoaderContext as WebpackLoaderContext, WebpackPluginInstance } from 'webpack'
import type VirtualModulesPlugin from 'webpack-virtual-modules'
export type {
EsbuildPlugin,
RolldownPlugin,
RollupPlugin,
RspackCompiler,
RspackPluginInstance,
VitePlugin,
WebpackCompiler,
WebpackPluginInstance,
}
export type Thenable<T> = T | Promise<T>
/**
* Null or whatever
*/
export type Nullable<T> = T | null | undefined
/**
* Array, or not yet
*/
export type Arrayable<T> = T | Array<T>
export interface SourceMapCompact {
file?: string
mappings: string
names: string[]
sourceRoot?: string
sources: string[]
// In magic-string v0.27.0, `sourcesContent` becomes nullable, while rollup haven't catch up yet
sourcesContent?: (string | null)[]
version: number
}
export type TransformResult = string | { code: string, map?: SourceMapInput | SourceMapCompact | null } | null | undefined | void
export interface ExternalIdResult { id: string, external?: boolean }
export type NativeBuildContext =
{ framework: 'webpack', compiler: WebpackCompiler, compilation?: WebpackCompilation, loaderContext?: WebpackLoaderContext<{ unpluginName: string }> } |
{ framework: 'esbuild', build: PluginBuild } |
{ framework: 'rspack', compiler: RspackCompiler, compilation: RspackCompilation, loaderContext?: RspackLoaderContext } |
{ framework: 'farm', context: FarmCompilationContext }
export interface UnpluginBuildContext {
addWatchFile: (id: string) => void
emitFile: (emittedFile: EmittedAsset) => void
getWatchFiles: () => string[]
parse: (input: string, options?: any) => AstNode
getNativeBuildContext?: () => NativeBuildContext
}
export interface UnpluginOptions {
name: string
enforce?: 'post' | 'pre' | undefined
// Build Hooks
buildStart?: (this: UnpluginBuildContext) => Promise<void> | void
buildEnd?: (this: UnpluginBuildContext) => Promise<void> | void
transform?: (this: UnpluginBuildContext & UnpluginContext, code: string, id: string) => Thenable<TransformResult>
load?: (this: UnpluginBuildContext & UnpluginContext, id: string) => Thenable<TransformResult>
resolveId?: (this: UnpluginBuildContext & UnpluginContext, id: string, importer: string | undefined, options: { isEntry: boolean }) => Thenable<string | ExternalIdResult | null | undefined>
watchChange?: (this: UnpluginBuildContext, id: string, change: { event: 'create' | 'update' | 'delete' }) => void
// Output Generation Hooks
writeBundle?: (this: void) => Promise<void> | void
/**
* Custom predicate function to filter modules to be loaded.
* When omitted, all modules will be included (might have potential perf impact on Webpack).
*/
loadInclude?: (id: string) => boolean | null | undefined
/**
* Custom predicate function to filter modules to be transformed.
* When omitted, all modules will be included (might have potential perf impact on Webpack).
*/
transformInclude?: (id: string) => boolean | null | undefined
// framework specify extends
rollup?: Partial<RollupPlugin>
webpack?: (compiler: WebpackCompiler) => void
rspack?: (compiler: RspackCompiler) => void
vite?: Partial<VitePlugin>
rolldown?: Partial<RolldownPlugin>
esbuild?: {
// using regexp in esbuild improves performance
onResolveFilter?: RegExp
onLoadFilter?: RegExp
loader?: Loader | ((code: string, id: string) => Loader)
setup?: (build: PluginBuild) => void | Promise<void>
config?: (options: BuildOptions) => void
}
farm?: Partial<FarmPlugin>
}
export interface ResolvedUnpluginOptions extends UnpluginOptions {
// injected internal objects
__vfs?: VirtualModulesPlugin
__vfsModules?: Set<string>
__virtualModulePrefix: string
}
export type UnpluginFactory<UserOptions, Nested extends boolean = boolean> = (options: UserOptions, meta: UnpluginContextMeta) =>
Nested extends true
? Array<UnpluginOptions>
: UnpluginOptions
export type UnpluginFactoryOutput<UserOptions, Return> = undefined extends UserOptions
? (options?: UserOptions) => Return
: (options: UserOptions) => Return
export interface UnpluginInstance<UserOptions, Nested extends boolean = boolean> {
rollup: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RollupPlugin> : RollupPlugin>
vite: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<VitePlugin> : VitePlugin>
rolldown: UnpluginFactoryOutput<UserOptions, Nested extends true ? Array<RolldownPlugin> : RolldownPlugin>
webpack: UnpluginFactoryOutput<UserOptions, WebpackPluginInstance>
rspack: UnpluginFactoryOutput<UserOptions, RspackPluginInstance>
esbuild: UnpluginFactoryOutput<UserOptions, EsbuildPlugin>
farm: UnpluginFactoryOutput<UserOptions, FarmPlugin>
raw: UnpluginFactory<UserOptions, Nested>
}
export type UnpluginContextMeta = Partial<RollupContextMeta> & ({
framework: 'rollup' | 'vite' | 'rolldown' | 'farm'
} | {
framework: 'webpack'
webpack: { compiler: WebpackCompiler }
} | {
framework: 'esbuild'
/** Set the host plugin name of esbuild when returning multiple plugins */
esbuildHostName?: string
} | {
framework: 'rspack'
rspack: { compiler: RspackCompiler }
})
export interface UnpluginMessage {
name?: string
id?: string
message: string
stack?: string
code?: string
plugin?: string
pluginCode?: unknown
loc?: {
column: number
file?: string
line: number
}
meta?: any
}
export interface UnpluginContext {
error: (message: string | UnpluginMessage) => void
warn: (message: string | UnpluginMessage) => void
}