From 308bee5095e161496e968c7db62b1bc6b0c3fdff Mon Sep 17 00:00:00 2001 From: James Opstad <13586373+jamesopstad@users.noreply.github.com> Date: Mon, 10 Feb 2025 12:05:39 +0000 Subject: [PATCH 1/5] Simplified writeDeployConfig and called in writeBundle rather than builder.buildApp --- .../src/deploy-config.ts | 60 +++++++++---------- packages/vite-plugin-cloudflare/src/index.ts | 12 +++- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/packages/vite-plugin-cloudflare/src/deploy-config.ts b/packages/vite-plugin-cloudflare/src/deploy-config.ts index a01ce03694ca..799a777fffe6 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: ${resolvedPluginConfig.entryWorkerEnvironmentName} environment output directory 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..bd084eff4c31 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,16 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] { source: JSON.stringify(config), }); }, + writeBundle() { + 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(); From 8e6e4ef7d1d8e465f5ea32db183a0028a88167af Mon Sep 17 00:00:00 2001 From: James Opstad <13586373+jamesopstad@users.noreply.github.com> Date: Mon, 10 Feb 2025 12:13:17 +0000 Subject: [PATCH 2/5] Added changeset --- .changeset/modern-cycles-build.md | 5 +++++ 1 file changed, 5 insertions(+) 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..4380771e6d94 --- /dev/null +++ b/.changeset/modern-cycles-build.md @@ -0,0 +1,5 @@ +--- +"@cloudflare/vite-plugin": patch +--- + +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. From b2ec556fa3f41bed50861471f4fbb9559f346ca5 Mon Sep 17 00:00:00 2001 From: James Opstad <13586373+jamesopstad@users.noreply.github.com> Date: Mon, 10 Feb 2025 12:16:51 +0000 Subject: [PATCH 3/5] Edited error message --- packages/vite-plugin-cloudflare/src/deploy-config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite-plugin-cloudflare/src/deploy-config.ts b/packages/vite-plugin-cloudflare/src/deploy-config.ts index 799a777fffe6..ac729eb145cb 100644 --- a/packages/vite-plugin-cloudflare/src/deploy-config.ts +++ b/packages/vite-plugin-cloudflare/src/deploy-config.ts @@ -94,7 +94,7 @@ export function writeDeployConfig( assert( entryWorkerConfigPath, - `Unexpected error: ${resolvedPluginConfig.entryWorkerEnvironmentName} environment output directory is undefined` + `Unexpected error: entryWorkerConfigPath is undefined` ); const deployConfig: DeployConfig = { From 1f7dca6434dcd22f3421db438c4445779f035b59 Mon Sep 17 00:00:00 2001 From: James Opstad <13586373+jamesopstad@users.noreply.github.com> Date: Wed, 12 Feb 2025 16:53:42 +0000 Subject: [PATCH 4/5] Update changeset --- .changeset/modern-cycles-build.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.changeset/modern-cycles-build.md b/.changeset/modern-cycles-build.md index 4380771e6d94..bb75724ebeb6 100644 --- a/.changeset/modern-cycles-build.md +++ b/.changeset/modern-cycles-build.md @@ -2,4 +2,6 @@ "@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. From 4212d5523aa8df3d7d2ceb57e3ce4449cfc11799 Mon Sep 17 00:00:00 2001 From: James Opstad <13586373+jamesopstad@users.noreply.github.com> Date: Wed, 12 Feb 2025 17:00:43 +0000 Subject: [PATCH 5/5] Add comment --- packages/vite-plugin-cloudflare/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/vite-plugin-cloudflare/src/index.ts b/packages/vite-plugin-cloudflare/src/index.ts index bd084eff4c31..4e1b6d8039cd 100644 --- a/packages/vite-plugin-cloudflare/src/index.ts +++ b/packages/vite-plugin-cloudflare/src/index.ts @@ -229,6 +229,8 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] { }); }, 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"