Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call writeDeployConfig in writeBundle rather than builder.buildApp #8079

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
jamesopstad marked this conversation as resolved.
Show resolved Hide resolved
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);
}
jamesopstad marked this conversation as resolved.
Show resolved Hide resolved
},
handleHotUpdate(options) {
if (resolvedPluginConfig.configPaths.has(options.file)) {
options.server.restart();
Expand Down
Loading