From 1b07419b5ac90657af1cc5fbcdfcc680021cfd73 Mon Sep 17 00:00:00 2001 From: James Opstad <13586373+jamesopstad@users.noreply.github.com> Date: Wed, 12 Feb 2025 17:28:56 +0000 Subject: [PATCH] Call `writeDeployConfig` in `writeBundle` rather than `builder.buildApp` (#8079) --- .changeset/modern-cycles-build.md | 7 +++ .../src/deploy-config.ts | 60 +++++++++---------- packages/vite-plugin-cloudflare/src/index.ts | 14 ++++- 3 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 .changeset/modern-cycles-build.md diff --git a/.changeset/modern-cycles-build.md b/.changeset/modern-cycles-build.md new file mode 100644 index 000000000000..bb75724ebeb6 --- /dev/null +++ b/.changeset/modern-cycles-build.md @@ -0,0 +1,7 @@ +--- +"@cloudflare/vite-plugin": patch +--- + +Call `writeDeployConfig` in `writeBundle` rather than `builder.buildApp`. + +The deploy config file is now written in the `writeBundle` hook rather than `builder.buildApp`. This ensures that the file is still written if other plugins override `builder` in the Vite config. diff --git a/packages/vite-plugin-cloudflare/src/deploy-config.ts b/packages/vite-plugin-cloudflare/src/deploy-config.ts index a01ce03694ca..ac729eb145cb 100644 --- a/packages/vite-plugin-cloudflare/src/deploy-config.ts +++ b/packages/vite-plugin-cloudflare/src/deploy-config.ts @@ -67,42 +67,40 @@ export function writeDeployConfig( fs.writeFileSync(deployConfigPath, JSON.stringify(deployConfig)); } else { - const workerConfigPaths = Object.fromEntries( - Object.keys(resolvedPluginConfig.workers).map((environmentName) => { - const outputDirectory = - resolvedViteConfig.environments[environmentName]?.build.outDir; - - assert( - outputDirectory, - `Unexpected error: ${environmentName} environment output directory is undefined` - ); - - return [ - environmentName, - getRelativePathToWorkerConfig( - deployConfigDirectory, - resolvedViteConfig.root, - outputDirectory - ), - ]; - }) - ); + let entryWorkerConfigPath: string | undefined; + const auxiliaryWorkers: DeployConfig["auxiliaryWorkers"] = []; + + for (const environmentName of Object.keys(resolvedPluginConfig.workers)) { + const outputDirectory = + resolvedViteConfig.environments[environmentName]?.build.outDir; + + assert( + outputDirectory, + `Unexpected error: ${environmentName} environment output directory is undefined` + ); - const { entryWorkerEnvironmentName } = resolvedPluginConfig; - const configPath = workerConfigPaths[entryWorkerEnvironmentName]; + const configPath = getRelativePathToWorkerConfig( + deployConfigDirectory, + resolvedViteConfig.root, + outputDirectory + ); + + if (environmentName === resolvedPluginConfig.entryWorkerEnvironmentName) { + entryWorkerConfigPath = configPath; + } else { + auxiliaryWorkers.push({ configPath }); + } + } assert( - configPath, - `Unexpected error: ${entryWorkerEnvironmentName} environment output directory is undefined` + entryWorkerConfigPath, + `Unexpected error: entryWorkerConfigPath is undefined` ); - const auxiliaryWorkers = Object.entries(workerConfigPaths) - .filter( - ([environmentName]) => environmentName !== entryWorkerEnvironmentName - ) - .map(([_, configPath]) => ({ configPath })); - - const deployConfig: DeployConfig = { configPath, auxiliaryWorkers }; + const deployConfig: DeployConfig = { + configPath: entryWorkerConfigPath, + auxiliaryWorkers, + }; fs.writeFileSync(deployConfigPath, JSON.stringify(deployConfig)); } diff --git a/packages/vite-plugin-cloudflare/src/index.ts b/packages/vite-plugin-cloudflare/src/index.ts index a65d6f8c4079..4e1b6d8039cd 100644 --- a/packages/vite-plugin-cloudflare/src/index.ts +++ b/packages/vite-plugin-cloudflare/src/index.ts @@ -134,8 +134,6 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] { ) ); } - - writeDeployConfig(resolvedPluginConfig, resolvedViteConfig); }, }, }; @@ -230,6 +228,18 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] { source: JSON.stringify(config), }); }, + writeBundle() { + // These conditions ensure the deploy config is emitted once per application build as `writeBundle` is called for each environment. + // If Vite introduces an additional hook that runs after the application has built then we could use that instead. + if ( + this.environment.name === + (resolvedPluginConfig.type === "assets-only" + ? "client" + : resolvedPluginConfig.entryWorkerEnvironmentName) + ) { + writeDeployConfig(resolvedPluginConfig, resolvedViteConfig); + } + }, handleHotUpdate(options) { if (resolvedPluginConfig.configPaths.has(options.file)) { options.server.restart();