|
| 1 | +import assert from "node:assert"; |
| 2 | +import { Miniflare } from "miniflare"; |
| 3 | +import { debuglog } from "./utils"; |
| 4 | +import type { NodeJsCompat } from "./nodejs-compat"; |
| 5 | +import type { |
| 6 | + AssetsOnlyResolvedConfig, |
| 7 | + PreviewResolvedConfig, |
| 8 | + ResolvedPluginConfig, |
| 9 | + WorkerConfig, |
| 10 | + WorkersResolvedConfig, |
| 11 | +} from "./plugin-config"; |
| 12 | +import type { MiniflareOptions } from "miniflare"; |
| 13 | +import type * as vite from "vite"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Used to store state that should persist across server restarts. |
| 17 | + * This is then accessed via `PluginContext`. |
| 18 | + */ |
| 19 | +export interface SharedContext { |
| 20 | + miniflare?: Miniflare; |
| 21 | + hasShownWorkerConfigWarnings: boolean; |
| 22 | + /** Used to track whether hooks are being called because of a server restart or a server close event */ |
| 23 | + isRestartingDevServer: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Used to provide context to internal plugins. |
| 28 | + * It should be reinstantiated each time the main plugin is created. |
| 29 | + */ |
| 30 | +export class PluginContext { |
| 31 | + #sharedContext: SharedContext; |
| 32 | + #resolvedPluginConfig?: ResolvedPluginConfig; |
| 33 | + #resolvedViteConfig?: vite.ResolvedConfig; |
| 34 | + |
| 35 | + constructor(sharedContext: SharedContext) { |
| 36 | + this.#sharedContext = sharedContext; |
| 37 | + } |
| 38 | + |
| 39 | + /** Creates a new Miniflare instance or updates the existing instance */ |
| 40 | + async startOrUpdateMiniflare(options: MiniflareOptions): Promise<void> { |
| 41 | + if (!this.#sharedContext.miniflare) { |
| 42 | + debuglog("Creating new Miniflare instance"); |
| 43 | + this.#sharedContext.miniflare = new Miniflare(options); |
| 44 | + } else { |
| 45 | + debuglog("Updating the existing Miniflare instance"); |
| 46 | + await this.#sharedContext.miniflare.setOptions(options); |
| 47 | + } |
| 48 | + debuglog("Miniflare is ready"); |
| 49 | + } |
| 50 | + |
| 51 | + async disposeMiniflare(): Promise<void> { |
| 52 | + await this.#sharedContext.miniflare?.dispose(); |
| 53 | + this.#sharedContext.miniflare = undefined; |
| 54 | + } |
| 55 | + |
| 56 | + get miniflare(): Miniflare { |
| 57 | + assert(this.#sharedContext.miniflare, "Expected `miniflare` to be defined"); |
| 58 | + |
| 59 | + return this.#sharedContext.miniflare; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets the resolved inspector port provided by Miniflare |
| 64 | + */ |
| 65 | + async getResolvedInspectorPort(): Promise<number | null> { |
| 66 | + if ( |
| 67 | + this.resolvedPluginConfig.inspectorPort === false || |
| 68 | + !this.#sharedContext.miniflare |
| 69 | + ) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + const miniflareInspectorUrl = |
| 74 | + await this.#sharedContext.miniflare.getInspectorURL(); |
| 75 | + |
| 76 | + return Number.parseInt(miniflareInspectorUrl.port); |
| 77 | + } |
| 78 | + |
| 79 | + setHasShownWorkerConfigWarnings(hasShownWorkerConfigWarnings: boolean): void { |
| 80 | + this.#sharedContext.hasShownWorkerConfigWarnings = |
| 81 | + hasShownWorkerConfigWarnings; |
| 82 | + } |
| 83 | + |
| 84 | + get hasShownWorkerConfigWarnings(): boolean { |
| 85 | + return this.#sharedContext.hasShownWorkerConfigWarnings; |
| 86 | + } |
| 87 | + |
| 88 | + setIsRestartingDevServer(isRestartingDevServer: boolean): void { |
| 89 | + this.#sharedContext.isRestartingDevServer = isRestartingDevServer; |
| 90 | + } |
| 91 | + |
| 92 | + get isRestartingDevServer(): boolean { |
| 93 | + return this.#sharedContext.isRestartingDevServer; |
| 94 | + } |
| 95 | + |
| 96 | + setResolvedPluginConfig(resolvedPluginConfig: ResolvedPluginConfig): void { |
| 97 | + this.#resolvedPluginConfig = resolvedPluginConfig; |
| 98 | + } |
| 99 | + |
| 100 | + get resolvedPluginConfig(): ResolvedPluginConfig { |
| 101 | + assert( |
| 102 | + this.#resolvedPluginConfig, |
| 103 | + "Expected `resolvedPluginConfig` to be defined" |
| 104 | + ); |
| 105 | + |
| 106 | + return this.#resolvedPluginConfig; |
| 107 | + } |
| 108 | + |
| 109 | + setResolvedViteConfig(resolvedViteConfig: vite.ResolvedConfig): void { |
| 110 | + this.#resolvedViteConfig = resolvedViteConfig; |
| 111 | + } |
| 112 | + |
| 113 | + get resolvedViteConfig(): vite.ResolvedConfig { |
| 114 | + assert( |
| 115 | + this.#resolvedViteConfig, |
| 116 | + "Expected `resolvedViteConfig` to be defined" |
| 117 | + ); |
| 118 | + |
| 119 | + return this.#resolvedViteConfig; |
| 120 | + } |
| 121 | + |
| 122 | + getWorkerConfig(environmentName: string): WorkerConfig | undefined { |
| 123 | + return this.resolvedPluginConfig.type === "workers" |
| 124 | + ? this.resolvedPluginConfig.workers[environmentName] |
| 125 | + : undefined; |
| 126 | + } |
| 127 | + |
| 128 | + get entryWorkerConfig(): WorkerConfig | undefined { |
| 129 | + if (this.resolvedPluginConfig.type !== "workers") { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + return this.resolvedPluginConfig.workers[ |
| 134 | + this.resolvedPluginConfig.entryWorkerEnvironmentName |
| 135 | + ]; |
| 136 | + } |
| 137 | + |
| 138 | + getNodeJsCompat(environmentName: string): NodeJsCompat | undefined { |
| 139 | + return this.resolvedPluginConfig.type === "workers" |
| 140 | + ? this.resolvedPluginConfig.nodeJsCompatMap.get(environmentName) |
| 141 | + : undefined; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +interface NarrowedPluginContext<T extends ResolvedPluginConfig> |
| 146 | + extends PluginContext { |
| 147 | + readonly resolvedPluginConfig: T; |
| 148 | +} |
| 149 | + |
| 150 | +export type AssetsOnlyPluginContext = |
| 151 | + NarrowedPluginContext<AssetsOnlyResolvedConfig>; |
| 152 | +export type WorkersPluginContext = NarrowedPluginContext<WorkersResolvedConfig>; |
| 153 | +export type PreviewPluginContext = NarrowedPluginContext<PreviewResolvedConfig>; |
| 154 | + |
| 155 | +export function assertIsNotPreview( |
| 156 | + ctx: PluginContext |
| 157 | +): asserts ctx is AssetsOnlyPluginContext | WorkersPluginContext { |
| 158 | + assert( |
| 159 | + ctx.resolvedPluginConfig.type !== "preview", |
| 160 | + `Expected "assets-only" or "workers" plugin config` |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +export function assertIsPreview( |
| 165 | + ctx: PluginContext |
| 166 | +): asserts ctx is PreviewPluginContext { |
| 167 | + assert( |
| 168 | + ctx.resolvedPluginConfig.type === "preview", |
| 169 | + `Expected "preview" plugin config` |
| 170 | + ); |
| 171 | +} |
0 commit comments