/
PluginDriver.ts
429 lines (396 loc) · 14.1 KB
/
PluginDriver.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import type Chunk from '../Chunk';
import type Graph from '../Graph';
import type Module from '../Module';
import type {
AddonHookFunction,
AddonHooks,
AsyncPluginHooks,
EmitFile,
FirstPluginHooks,
FunctionPluginHooks,
NormalizedInputOptions,
NormalizedOutputOptions,
ParallelPluginHooks,
Plugin,
PluginContext,
SequentialPluginHooks,
SerializablePluginCache,
SyncPluginHooks
} from '../rollup/types';
import { InputPluginHooks } from '../rollup/types';
import { FileEmitter } from './FileEmitter';
import { getPluginContext } from './PluginContext';
import {
errInputHookInOutputPlugin,
errInvalidAddonPluginHook,
errInvalidFunctionPluginHook,
error
} from './error';
import { getOrCreate } from './getOrCreate';
import { OutputBundleWithPlaceholders } from './outputBundle';
import { throwPluginError, warnDeprecatedHooks } from './pluginUtils';
/**
* Coerce a promise union to always be a promise.
* @example EnsurePromise<string | Promise<string>> -> Promise<string>
*/
type EnsurePromise<T> = Promise<Awaited<T>>;
/**
* Get the type of the first argument in a function.
* @example Arg0<(a: string, b: number) => void> -> string
*/
type Arg0<H extends keyof FunctionPluginHooks> = Parameters<FunctionPluginHooks[H]>[0];
// This will make sure no input hook is omitted
const inputHookNames: {
[P in InputPluginHooks]: 1;
} = {
buildEnd: 1,
buildStart: 1,
closeBundle: 1,
closeWatcher: 1,
load: 1,
moduleParsed: 1,
options: 1,
resolveDynamicImport: 1,
resolveId: 1,
shouldTransformCachedModule: 1,
transform: 1,
watchChange: 1
};
const inputHooks = Object.keys(inputHookNames);
export type ReplaceContext = (context: PluginContext, plugin: Plugin) => PluginContext;
export type HookAction = [plugin: string, hook: string, args: unknown[]];
export class PluginDriver {
public readonly emitFile: EmitFile;
public finaliseAssets: () => void;
public getFileName: (fileReferenceId: string) => string;
public readonly setOutputBundle: (
outputBundle: OutputBundleWithPlaceholders,
outputOptions: NormalizedOutputOptions,
facadeChunkByModule: ReadonlyMap<Module, Chunk>
) => void;
private readonly fileEmitter: FileEmitter;
private readonly pluginContexts: ReadonlyMap<Plugin, PluginContext>;
private readonly plugins: readonly Plugin[];
private readonly sortedPlugins = new Map<AsyncPluginHooks, Plugin[]>();
private readonly unfulfilledActions = new Set<HookAction>();
constructor(
private readonly graph: Graph,
private readonly options: NormalizedInputOptions,
userPlugins: readonly Plugin[],
private readonly pluginCache: Record<string, SerializablePluginCache> | undefined,
basePluginDriver?: PluginDriver
) {
warnDeprecatedHooks(userPlugins, options);
this.fileEmitter = new FileEmitter(
graph,
options,
basePluginDriver && basePluginDriver.fileEmitter
);
this.emitFile = this.fileEmitter.emitFile.bind(this.fileEmitter);
this.getFileName = this.fileEmitter.getFileName.bind(this.fileEmitter);
this.finaliseAssets = this.fileEmitter.assertAssetsFinalized.bind(this.fileEmitter);
this.setOutputBundle = this.fileEmitter.setOutputBundle.bind(this.fileEmitter);
this.plugins = userPlugins.concat(basePluginDriver ? basePluginDriver.plugins : []);
const existingPluginNames = new Set<string>();
this.pluginContexts = new Map(
this.plugins.map(plugin => [
plugin,
getPluginContext(plugin, pluginCache, graph, options, this.fileEmitter, existingPluginNames)
])
);
if (basePluginDriver) {
for (const plugin of userPlugins) {
for (const hook of inputHooks) {
if (hook in plugin) {
options.onwarn(errInputHookInOutputPlugin(plugin.name, hook));
}
}
}
}
}
public createOutputPluginDriver(plugins: readonly Plugin[]): PluginDriver {
return new PluginDriver(this.graph, this.options, plugins, this.pluginCache, this);
}
getUnfulfilledHookActions(): Set<HookAction> {
return this.unfulfilledActions;
}
// chains, first non-null result stops and returns
hookFirst<H extends AsyncPluginHooks & FirstPluginHooks>(
hookName: H,
args: Parameters<FunctionPluginHooks[H]>,
replaceContext?: ReplaceContext | null,
skipped?: ReadonlySet<Plugin> | null
): Promise<ReturnType<FunctionPluginHooks[H]> | null> {
let promise: Promise<ReturnType<FunctionPluginHooks[H]> | null> = Promise.resolve(null);
for (const plugin of this.getSortedPlugins(hookName)) {
if (skipped && skipped.has(plugin)) continue;
promise = promise.then(result => {
if (result != null) return result;
return this.runHook(hookName, args, plugin, replaceContext);
});
}
return promise;
}
// chains synchronously, first non-null result stops and returns
hookFirstSync<H extends SyncPluginHooks & FirstPluginHooks>(
hookName: H,
args: Parameters<FunctionPluginHooks[H]>,
replaceContext?: ReplaceContext
): ReturnType<FunctionPluginHooks[H]> | null {
for (const plugin of this.getSortedPlugins(hookName)) {
const result = this.runHookSync(hookName, args, plugin, replaceContext);
if (result != null) return result;
}
return null;
}
// parallel, ignores returns
async hookParallel<H extends AsyncPluginHooks & ParallelPluginHooks>(
hookName: H,
args: Parameters<FunctionPluginHooks[H]>,
replaceContext?: ReplaceContext
): Promise<void> {
const parallelPromises: Promise<unknown>[] = [];
for (const plugin of this.getSortedPlugins(hookName)) {
if ((plugin[hookName] as { sequential?: boolean }).sequential) {
await Promise.all(parallelPromises);
parallelPromises.length = 0;
await this.runHook(hookName, args, plugin, replaceContext);
} else {
parallelPromises.push(this.runHook(hookName, args, plugin, replaceContext));
}
}
await Promise.all(parallelPromises);
}
// chains, reduces returned value, handling the reduced value as the first hook argument
hookReduceArg0<H extends AsyncPluginHooks & SequentialPluginHooks>(
hookName: H,
[arg0, ...rest]: Parameters<FunctionPluginHooks[H]>,
reduce: (
reduction: Arg0<H>,
result: ReturnType<FunctionPluginHooks[H]>,
plugin: Plugin
) => Arg0<H>,
replaceContext?: ReplaceContext
): Promise<Arg0<H>> {
let promise = Promise.resolve(arg0);
for (const plugin of this.getSortedPlugins(hookName)) {
promise = promise.then(arg0 =>
this.runHook(
hookName,
[arg0, ...rest] as Parameters<FunctionPluginHooks[H]>,
plugin,
replaceContext
).then(result => reduce.call(this.pluginContexts.get(plugin), arg0, result, plugin))
);
}
return promise;
}
// chains synchronously, reduces returned value, handling the reduced value as the first hook argument
hookReduceArg0Sync<H extends SyncPluginHooks & SequentialPluginHooks>(
hookName: H,
[arg0, ...rest]: Parameters<FunctionPluginHooks[H]>,
reduce: (
reduction: Arg0<H>,
result: ReturnType<FunctionPluginHooks[H]>,
plugin: Plugin
) => Arg0<H>,
replaceContext?: ReplaceContext
): Arg0<H> {
for (const plugin of this.getSortedPlugins(hookName)) {
const args = [arg0, ...rest] as Parameters<FunctionPluginHooks[H]>;
const result = this.runHookSync(hookName, args, plugin, replaceContext);
arg0 = reduce.call(this.pluginContexts.get(plugin), arg0, result, plugin);
}
return arg0;
}
// chains, reduces returned value to type string, handling the reduced value separately. permits hooks as values.
async hookReduceValue<H extends AddonHooks>(
hookName: H,
initialValue: string | Promise<string>,
args: Parameters<AddonHookFunction>,
reducer: (result: string, next: string) => string
): Promise<string> {
const results: string[] = [];
const parallelResults: (string | Promise<string>)[] = [];
for (const plugin of this.getSortedPlugins(hookName, validateAddonPluginHandler)) {
if ((plugin[hookName] as { sequential?: boolean }).sequential) {
results.push(...(await Promise.all(parallelResults)));
parallelResults.length = 0;
results.push(await this.runHook(hookName, args, plugin));
} else {
parallelResults.push(this.runHook(hookName, args, plugin));
}
}
results.push(...(await Promise.all(parallelResults)));
return results.reduce(reducer, await initialValue);
}
// chains synchronously, reduces returned value to type T, handling the reduced value separately. permits hooks as values.
hookReduceValueSync<H extends SyncPluginHooks & SequentialPluginHooks, T>(
hookName: H,
initialValue: T,
args: Parameters<FunctionPluginHooks[H]>,
reduce: (reduction: T, result: ReturnType<FunctionPluginHooks[H]>, plugin: Plugin) => T,
replaceContext?: ReplaceContext
): T {
let acc = initialValue;
for (const plugin of this.getSortedPlugins(hookName)) {
const result = this.runHookSync(hookName, args, plugin, replaceContext);
acc = reduce.call(this.pluginContexts.get(plugin), acc, result, plugin);
}
return acc;
}
// chains, ignores returns
hookSeq<H extends AsyncPluginHooks & SequentialPluginHooks>(
hookName: H,
args: Parameters<FunctionPluginHooks[H]>,
replaceContext?: ReplaceContext
): Promise<void> {
let promise: Promise<unknown> = Promise.resolve();
for (const plugin of this.getSortedPlugins(hookName)) {
promise = promise.then(() => this.runHook(hookName, args, plugin, replaceContext));
}
return promise.then(noReturn);
}
private getSortedPlugins(
hookName: keyof FunctionPluginHooks | AddonHooks,
validateHandler?: (handler: unknown, hookName: string, plugin: Plugin) => void
): Plugin[] {
return getOrCreate(this.sortedPlugins, hookName, () =>
getSortedValidatedPlugins(hookName, this.plugins, validateHandler)
);
}
/**
* Run an async plugin hook and return the result.
* @param hookName Name of the plugin hook. Must be either in `PluginHooks` or `OutputPluginValueHooks`.
* @param args Arguments passed to the plugin hook.
* @param plugin The actual pluginObject to run.
* @param replaceContext When passed, the plugin context can be overridden.
*/
private runHook<H extends AddonHooks>(
hookName: H,
args: Parameters<AddonHookFunction>,
plugin: Plugin
): EnsurePromise<ReturnType<AddonHookFunction>>;
private runHook<H extends AsyncPluginHooks>(
hookName: H,
args: Parameters<FunctionPluginHooks[H]>,
plugin: Plugin,
replaceContext?: ReplaceContext | null
): Promise<ReturnType<FunctionPluginHooks[H]>>;
// Implementation signature
private runHook<H extends AsyncPluginHooks | AddonHooks>(
hookName: H,
args: unknown[],
plugin: Plugin,
replaceContext?: ReplaceContext | null
): Promise<unknown> {
// We always filter for plugins that support the hook before running it
const hook = plugin[hookName]!;
const handler = typeof hook === 'object' ? hook.handler : hook;
let context = this.pluginContexts.get(plugin)!;
if (replaceContext) {
context = replaceContext(context, plugin);
}
let action: [string, string, Parameters<any>] | null = null;
return Promise.resolve()
.then(() => {
if (typeof handler !== 'function') {
return handler;
}
// eslint-disable-next-line @typescript-eslint/ban-types
const hookResult = (handler as Function).apply(context, args);
if (!hookResult?.then) {
// short circuit for non-thenables and non-Promises
return hookResult;
}
// Track pending hook actions to properly error out when
// unfulfilled promises cause rollup to abruptly and confusingly
// exit with a successful 0 return code but without producing any
// output, errors or warnings.
action = [plugin.name, hookName, args];
this.unfulfilledActions.add(action);
// Although it would be more elegant to just return hookResult here
// and put the .then() handler just above the .catch() handler below,
// doing so would subtly change the defacto async event dispatch order
// which at least one test and some plugins in the wild may depend on.
return Promise.resolve(hookResult).then(result => {
// action was fulfilled
this.unfulfilledActions.delete(action!);
return result;
});
})
.catch(err => {
if (action !== null) {
// action considered to be fulfilled since error being handled
this.unfulfilledActions.delete(action);
}
return throwPluginError(err, plugin.name, { hook: hookName });
});
}
/**
* Run a sync plugin hook and return the result.
* @param hookName Name of the plugin hook. Must be in `PluginHooks`.
* @param args Arguments passed to the plugin hook.
* @param plugin The acutal plugin
* @param replaceContext When passed, the plugin context can be overridden.
*/
private runHookSync<H extends SyncPluginHooks>(
hookName: H,
args: Parameters<FunctionPluginHooks[H]>,
plugin: Plugin,
replaceContext?: ReplaceContext
): ReturnType<FunctionPluginHooks[H]> {
const hook = plugin[hookName]!;
const handler = typeof hook === 'object' ? hook.handler : hook;
let context = this.pluginContexts.get(plugin)!;
if (replaceContext) {
context = replaceContext(context, plugin);
}
try {
// eslint-disable-next-line @typescript-eslint/ban-types
return (handler as Function).apply(context, args);
} catch (err: any) {
return throwPluginError(err, plugin.name, { hook: hookName });
}
}
}
export function getSortedValidatedPlugins(
hookName: keyof FunctionPluginHooks | AddonHooks,
plugins: readonly Plugin[],
validateHandler = validateFunctionPluginHandler
): Plugin[] {
const pre: Plugin[] = [];
const normal: Plugin[] = [];
const post: Plugin[] = [];
for (const plugin of plugins) {
const hook = plugin[hookName];
if (hook) {
if (typeof hook === 'object') {
validateHandler(hook.handler, hookName, plugin);
if (hook.order === 'pre') {
pre.push(plugin);
continue;
}
if (hook.order === 'post') {
post.push(plugin);
continue;
}
} else {
validateHandler(hook, hookName, plugin);
}
normal.push(plugin);
}
}
return [...pre, ...normal, ...post];
}
function validateFunctionPluginHandler(handler: unknown, hookName: string, plugin: Plugin) {
if (typeof handler !== 'function') {
error(errInvalidFunctionPluginHook(hookName, plugin.name));
}
}
function validateAddonPluginHandler(handler: unknown, hookName: string, plugin: Plugin) {
if (typeof handler !== 'string' && typeof handler !== 'function') {
return error(errInvalidAddonPluginHook(hookName, plugin.name));
}
}
function noReturn() {}