Skip to content

Commit 1b07419

Browse files
authored
Call writeDeployConfig in writeBundle rather than builder.buildApp (#8079)
1 parent 060a4db commit 1b07419

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

.changeset/modern-cycles-build.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Call `writeDeployConfig` in `writeBundle` rather than `builder.buildApp`.
6+
7+
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.

packages/vite-plugin-cloudflare/src/deploy-config.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,42 +67,40 @@ export function writeDeployConfig(
6767

6868
fs.writeFileSync(deployConfigPath, JSON.stringify(deployConfig));
6969
} else {
70-
const workerConfigPaths = Object.fromEntries(
71-
Object.keys(resolvedPluginConfig.workers).map((environmentName) => {
72-
const outputDirectory =
73-
resolvedViteConfig.environments[environmentName]?.build.outDir;
74-
75-
assert(
76-
outputDirectory,
77-
`Unexpected error: ${environmentName} environment output directory is undefined`
78-
);
79-
80-
return [
81-
environmentName,
82-
getRelativePathToWorkerConfig(
83-
deployConfigDirectory,
84-
resolvedViteConfig.root,
85-
outputDirectory
86-
),
87-
];
88-
})
89-
);
70+
let entryWorkerConfigPath: string | undefined;
71+
const auxiliaryWorkers: DeployConfig["auxiliaryWorkers"] = [];
72+
73+
for (const environmentName of Object.keys(resolvedPluginConfig.workers)) {
74+
const outputDirectory =
75+
resolvedViteConfig.environments[environmentName]?.build.outDir;
76+
77+
assert(
78+
outputDirectory,
79+
`Unexpected error: ${environmentName} environment output directory is undefined`
80+
);
9081

91-
const { entryWorkerEnvironmentName } = resolvedPluginConfig;
92-
const configPath = workerConfigPaths[entryWorkerEnvironmentName];
82+
const configPath = getRelativePathToWorkerConfig(
83+
deployConfigDirectory,
84+
resolvedViteConfig.root,
85+
outputDirectory
86+
);
87+
88+
if (environmentName === resolvedPluginConfig.entryWorkerEnvironmentName) {
89+
entryWorkerConfigPath = configPath;
90+
} else {
91+
auxiliaryWorkers.push({ configPath });
92+
}
93+
}
9394

9495
assert(
95-
configPath,
96-
`Unexpected error: ${entryWorkerEnvironmentName} environment output directory is undefined`
96+
entryWorkerConfigPath,
97+
`Unexpected error: entryWorkerConfigPath is undefined`
9798
);
9899

99-
const auxiliaryWorkers = Object.entries(workerConfigPaths)
100-
.filter(
101-
([environmentName]) => environmentName !== entryWorkerEnvironmentName
102-
)
103-
.map(([_, configPath]) => ({ configPath }));
104-
105-
const deployConfig: DeployConfig = { configPath, auxiliaryWorkers };
100+
const deployConfig: DeployConfig = {
101+
configPath: entryWorkerConfigPath,
102+
auxiliaryWorkers,
103+
};
106104

107105
fs.writeFileSync(deployConfigPath, JSON.stringify(deployConfig));
108106
}

packages/vite-plugin-cloudflare/src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
134134
)
135135
);
136136
}
137-
138-
writeDeployConfig(resolvedPluginConfig, resolvedViteConfig);
139137
},
140138
},
141139
};
@@ -230,6 +228,18 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
230228
source: JSON.stringify(config),
231229
});
232230
},
231+
writeBundle() {
232+
// These conditions ensure the deploy config is emitted once per application build as `writeBundle` is called for each environment.
233+
// If Vite introduces an additional hook that runs after the application has built then we could use that instead.
234+
if (
235+
this.environment.name ===
236+
(resolvedPluginConfig.type === "assets-only"
237+
? "client"
238+
: resolvedPluginConfig.entryWorkerEnvironmentName)
239+
) {
240+
writeDeployConfig(resolvedPluginConfig, resolvedViteConfig);
241+
}
242+
},
233243
handleHotUpdate(options) {
234244
if (resolvedPluginConfig.configPaths.has(options.file)) {
235245
options.server.restart();

0 commit comments

Comments
 (0)