Skip to content

Commit

Permalink
Call writeDeployConfig in writeBundle rather than `builder.buildA…
Browse files Browse the repository at this point in the history
…pp` (#8079)
  • Loading branch information
jamesopstad authored Feb 12, 2025
1 parent 060a4db commit 1b07419
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
7 changes: 7 additions & 0 deletions .changeset/modern-cycles-build.md
Original file line number Diff line number Diff line change
@@ -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.
60 changes: 29 additions & 31 deletions packages/vite-plugin-cloudflare/src/deploy-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
14 changes: 12 additions & 2 deletions packages/vite-plugin-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
)
);
}

writeDeployConfig(resolvedPluginConfig, resolvedViteConfig);
},
},
};
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1b07419

Please sign in to comment.