|
| 1 | +/** |
| 2 | + * Mostly copied from the cloudflare adapter |
| 3 | + * ESBuild stops calling `onLoad` hooks after the first hook returns an updated content. |
| 4 | + * |
| 5 | + * The updater allows multiple plugins to update the content. |
| 6 | + */ |
| 7 | + |
| 8 | +import { readFile } from "node:fs/promises"; |
| 9 | + |
| 10 | +import type { OnLoadArgs, OnLoadOptions, Plugin, PluginBuild } from "esbuild"; |
| 11 | +import type { BuildOptions } from "../build/helper"; |
| 12 | +import { |
| 13 | + type VersionedField, |
| 14 | + extractVersionedField, |
| 15 | +} from "../build/patch/codePatcher"; |
| 16 | + |
| 17 | +/** |
| 18 | + * The callbacks returns either an updated content or undefined if the content is unchanged. |
| 19 | + */ |
| 20 | +export type Callback = (args: { |
| 21 | + contents: string; |
| 22 | + path: string; |
| 23 | +}) => string | undefined | Promise<string | undefined>; |
| 24 | + |
| 25 | +/** |
| 26 | + * The callback is called only when `contentFilter` matches the content. |
| 27 | + * It can be used as a fast heuristic to prevent an expensive update. |
| 28 | + */ |
| 29 | +export type OnUpdateOptions = OnLoadOptions & { |
| 30 | + contentFilter: RegExp; |
| 31 | +}; |
| 32 | + |
| 33 | +export type Updater = OnUpdateOptions & { callback: Callback }; |
| 34 | + |
| 35 | +export class ContentUpdater { |
| 36 | + updaters = new Map<string, Updater[]>(); |
| 37 | + |
| 38 | + constructor(private buildOptions: BuildOptions) {} |
| 39 | + |
| 40 | + /** |
| 41 | + * Register a callback to update the file content. |
| 42 | + * |
| 43 | + * The callbacks are called in order of registration. |
| 44 | + * |
| 45 | + * @param name The name of the plugin (must be unique). |
| 46 | + * @param updater A versioned field with the callback and `OnUpdateOptions`. |
| 47 | + * @returns A noop ESBuild plugin. |
| 48 | + */ |
| 49 | + updateContent( |
| 50 | + name: string, |
| 51 | + versionedUpdaters: VersionedField<Updater>[], |
| 52 | + ): Plugin { |
| 53 | + if (this.updaters.has(name)) { |
| 54 | + throw new Error(`Plugin "${name}" already registered`); |
| 55 | + } |
| 56 | + const updaters = extractVersionedField( |
| 57 | + versionedUpdaters, |
| 58 | + this.buildOptions.nextVersion, |
| 59 | + ); |
| 60 | + this.updaters.set(name, updaters); |
| 61 | + return { |
| 62 | + name, |
| 63 | + setup() {}, |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns an ESBuild plugin applying the registered updates. |
| 69 | + */ |
| 70 | + get plugin() { |
| 71 | + return { |
| 72 | + name: "aggregate-on-load", |
| 73 | + |
| 74 | + setup: async (build: PluginBuild) => { |
| 75 | + build.onLoad( |
| 76 | + { filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/ }, |
| 77 | + async (args: OnLoadArgs) => { |
| 78 | + let contents = await readFile(args.path, "utf-8"); |
| 79 | + // biome-ignore lint/correctness/noFlatMapIdentity: <explanation> |
| 80 | + const updaters = this.updaters.values().flatMap((u) => u); |
| 81 | + for (const { |
| 82 | + filter, |
| 83 | + namespace, |
| 84 | + contentFilter, |
| 85 | + callback, |
| 86 | + } of updaters) { |
| 87 | + if (namespace !== undefined && args.namespace !== namespace) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + if (!args.path.match(filter)) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + if (!contents.match(contentFilter)) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + contents = |
| 97 | + (await callback({ contents, path: args.path })) ?? contents; |
| 98 | + } |
| 99 | + return { contents }; |
| 100 | + }, |
| 101 | + ); |
| 102 | + }, |
| 103 | + }; |
| 104 | + } |
| 105 | +} |
0 commit comments