diff --git a/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts b/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts index 873b7f7c..c4d93fc2 100644 --- a/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts +++ b/packages/vite-plugin-cloudflare/src/cloudflare-environment.ts @@ -1,7 +1,8 @@ +import assert from 'node:assert'; import { builtinModules } from 'node:module'; import * as vite from 'vite'; import { getNodeCompatExternals } from './node-js-compat'; -import { INIT_PATH, invariant, UNKNOWN_HOST } from './shared'; +import { INIT_PATH, UNKNOWN_HOST } from './shared'; import { toMiniflareRequest } from './utils'; import type { ResolvedPluginConfig, WorkerConfig } from './plugin-config'; import type { Fetcher } from '@cloudflare/workers-types/experimental'; @@ -26,7 +27,7 @@ function createHotChannel( const client: vite.HotChannelClient = { send(payload) { const webSocket = webSocketContainer.webSocket; - invariant(webSocket, webSocketUndefinedError); + assert(webSocket, webSocketUndefinedError); webSocket.send(JSON.stringify(payload)); }, @@ -44,7 +45,7 @@ function createHotChannel( return { send(payload) { const webSocket = webSocketContainer.webSocket; - invariant(webSocket, webSocketUndefinedError); + assert(webSocket, webSocketUndefinedError); webSocket.send(JSON.stringify(payload)); }, @@ -59,13 +60,13 @@ function createHotChannel( }, listen() { const webSocket = webSocketContainer.webSocket; - invariant(webSocket, webSocketUndefinedError); + assert(webSocket, webSocketUndefinedError); webSocket.addEventListener('message', onMessage); }, close() { const webSocket = webSocketContainer.webSocket; - invariant(webSocket, webSocketUndefinedError); + assert(webSocket, webSocketUndefinedError); webSocket.removeEventListener('message', onMessage); }, @@ -98,10 +99,10 @@ export class CloudflareDevEnvironment extends vite.DevEnvironment { }, ); - invariant(response.ok, 'Failed to initialize module runner'); + assert(response.ok, 'Failed to initialize module runner'); const webSocket = response.webSocket; - invariant(webSocket, 'Failed to establish WebSocket'); + assert(webSocket, 'Failed to establish WebSocket'); webSocket.accept(); @@ -109,7 +110,7 @@ export class CloudflareDevEnvironment extends vite.DevEnvironment { } async dispatchFetch(request: Request) { - invariant(this.#worker, 'Runner not initialized'); + assert(this.#worker, 'Runner not initialized'); return this.#worker.fetch(toMiniflareRequest(request)) as any; } diff --git a/packages/vite-plugin-cloudflare/src/index.ts b/packages/vite-plugin-cloudflare/src/index.ts index ce01d6c5..7c2f9361 100644 --- a/packages/vite-plugin-cloudflare/src/index.ts +++ b/packages/vite-plugin-cloudflare/src/index.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert'; import * as fs from 'node:fs'; import path from 'node:path'; import { createMiddleware } from '@hattip/adapter-node'; @@ -18,7 +19,6 @@ import { resolveNodeAliases, } from './node-js-compat'; import { resolvePluginConfig } from './plugin-config'; -import { invariant } from './shared'; import { toMiniflareRequest } from './utils'; import type { PluginConfig, ResolvedPluginConfig } from './plugin-config'; import type { Unstable_RawConfig } from 'wrangler'; @@ -79,10 +79,7 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin { ).map((environmentName) => { const environment = builder.environments[environmentName]; - invariant( - environment, - `${environmentName} environment not found`, - ); + assert(environment, `${environmentName} environment not found`); return environment; }); diff --git a/packages/vite-plugin-cloudflare/src/miniflare-options.ts b/packages/vite-plugin-cloudflare/src/miniflare-options.ts index 450afc66..5f49c359 100644 --- a/packages/vite-plugin-cloudflare/src/miniflare-options.ts +++ b/packages/vite-plugin-cloudflare/src/miniflare-options.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert'; import * as fs from 'node:fs'; import * as fsp from 'node:fs/promises'; import * as path from 'node:path'; @@ -13,7 +14,6 @@ import { ASSET_WORKERS_COMPATIBILITY_DATE, ROUTER_WORKER_NAME, } from './assets'; -import { invariant } from './shared'; import type { CloudflareDevEnvironment } from './cloudflare-environment'; import type { ResolvedPluginConfig, WorkerConfig } from './plugin-config'; import type { MiniflareOptions, SharedOptions, WorkerOptions } from 'miniflare'; @@ -67,7 +67,7 @@ function getWorkerToWorkerEntrypointNamesMap( const entrypointNames = workerToWorkerEntrypointNamesMap.get( value.name, ); - invariant(entrypointNames, missingWorkerErrorMessage(value.name)); + assert(entrypointNames, missingWorkerErrorMessage(value.name)); entrypointNames.add(value.entrypoint); } @@ -88,7 +88,7 @@ function getWorkerToDurableObjectClassNamesMap( for (const value of Object.values(worker.durableObjects ?? {})) { if (typeof value === 'string') { const classNames = workerToDurableObjectClassNamesMap.get(worker.name); - invariant(classNames, missingWorkerErrorMessage(worker.name)); + assert(classNames, missingWorkerErrorMessage(worker.name)); classNames.add(value); } else if (typeof value === 'object') { @@ -96,14 +96,14 @@ function getWorkerToDurableObjectClassNamesMap( const classNames = workerToDurableObjectClassNamesMap.get( value.scriptName, ); - invariant(classNames, missingWorkerErrorMessage(value.scriptName)); + assert(classNames, missingWorkerErrorMessage(value.scriptName)); classNames.add(value.className); } else { const classNames = workerToDurableObjectClassNamesMap.get( worker.name, ); - invariant(classNames, missingWorkerErrorMessage(worker.name)); + assert(classNames, missingWorkerErrorMessage(worker.name)); classNames.add(value.className); } @@ -271,7 +271,7 @@ export function getDevMiniflareOptions( data: [string, string, FetchFunctionOptions]; }; - invariant( + assert( invokePayloadData.name === 'fetchModule', `Invalid invoke event: ${invokePayloadData.name}`, ); @@ -342,7 +342,7 @@ export function getDevMiniflareOptions( const entrypointNames = workerToWorkerEntrypointNamesMap.get( workerOptions.name, ); - invariant( + assert( entrypointNames, `WorkerEntrypoint names not found for worker ${workerOptions.name}`, ); @@ -356,7 +356,7 @@ export function getDevMiniflareOptions( const classNames = workerToDurableObjectClassNamesMap.get( workerOptions.name, ); - invariant( + assert( classNames, `DurableObject class names not found for worker ${workerOptions.name}`, ); @@ -390,7 +390,7 @@ export function getDevMiniflareOptions( } function getEntryModule(main: string | undefined) { - invariant( + assert( main, 'Unexpected error: missing main field in miniflareWorkerOptions', ); diff --git a/packages/vite-plugin-cloudflare/src/plugin-config.ts b/packages/vite-plugin-cloudflare/src/plugin-config.ts index 45b356b5..7eb3b4ca 100644 --- a/packages/vite-plugin-cloudflare/src/plugin-config.ts +++ b/packages/vite-plugin-cloudflare/src/plugin-config.ts @@ -1,8 +1,8 @@ +import assert from 'node:assert'; import * as fs from 'node:fs'; import * as path from 'node:path'; import * as vite from 'vite'; import { unstable_readConfig } from 'wrangler'; -import { invariant } from './shared'; import type { Unstable_Config } from 'wrangler'; export interface PluginConfig { @@ -66,7 +66,7 @@ function getConfigResult( wranglerConfigPaths.add(configPath); if (isEntryWorker && !wranglerConfig.main) { - invariant( + assert( wranglerConfig.assets, `No main or assets field provided in ${wranglerConfig.configPath}`, ); @@ -77,12 +77,12 @@ function getConfigResult( }; } - invariant( + assert( wranglerConfig.main, `No main field provided in ${wranglerConfig.configPath}`, ); - invariant( + assert( wranglerConfig.name, `No name field provided in ${wranglerConfig.configPath}`, ); @@ -124,7 +124,7 @@ export function resolvePluginConfig( ? path.join(root, pluginConfig.wranglerConfig) : findWranglerConfig(root); - invariant( + assert( configPath, `Config not found. Have you created a wrangler.json(c) or wrangler.toml file?`, ); @@ -155,7 +155,7 @@ export function resolvePluginConfig( wranglerConfigPaths, ); - invariant( + assert( configResult.type === 'worker', 'Unexpected error: received AssetsOnlyResult with auxiliary workers.', ); diff --git a/packages/vite-plugin-cloudflare/src/shared.ts b/packages/vite-plugin-cloudflare/src/shared.ts index 6bced0e3..51c4ab6d 100644 --- a/packages/vite-plugin-cloudflare/src/shared.ts +++ b/packages/vite-plugin-cloudflare/src/shared.ts @@ -1,13 +1,2 @@ export const UNKNOWN_HOST = 'http://localhost'; export const INIT_PATH = '/__vite_plugin_cloudflare_init__'; - -export function invariant( - condition: unknown, - message: string, -): asserts condition { - if (condition) { - return; - } - - throw new Error(message); -}