Vite plugin v2 - Support child environments - #15506
Conversation
|
@cloudflare/autoconfig
@cloudflare/build-output-utils
@cloudflare/codemods
@cloudflare/config
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-functions
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-plugin
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
| const workerEnvironments = [ | ||
| ...ctx.resolvedPluginConfig.environmentNameToWorkerMap.entries(), | ||
| ] | ||
| .filter(([_, worker]) => !resolveDevOnly(worker.devOnly)) | ||
| .map(([environmentName]) => { | ||
| const environment = builder.environments[environmentName]; | ||
| assert(environment, `"${environmentName}" environment not found`); | ||
|
|
||
| return environment; | ||
| }); |
There was a problem hiding this comment.
[P1] The default createBuildApp() only builds parent environments, and this fallback repeats that same parent-only set. Consequently, a standard build with viteEnvironment.childEnvironments but no framework-provided buildApp never emits a configured child entry; the production fallback import then points at a file that does not exist. Include each non-dev-only worker's children here so the post hook builds the missing child environments.
| const workerEnvironments = [ | |
| ...ctx.resolvedPluginConfig.environmentNameToWorkerMap.entries(), | |
| ] | |
| .filter(([_, worker]) => !resolveDevOnly(worker.devOnly)) | |
| .map(([environmentName]) => { | |
| const environment = builder.environments[environmentName]; | |
| assert(environment, `"${environmentName}" environment not found`); | |
| return environment; | |
| }); | |
| const workerEnvironments = [ | |
| ...ctx.resolvedPluginConfig.environmentNameToWorkerMap.entries(), | |
| ].flatMap(([environmentName, worker]) => { | |
| if (resolveDevOnly(worker.devOnly)) { | |
| return []; | |
| } | |
| const environmentNames = [ | |
| environmentName, | |
| ...(ctx.resolvedPluginConfig.environmentNameToChildEnvironmentNamesMap.get( | |
| environmentName | |
| ) ?? []), | |
| ]; | |
| return environmentNames.map((name) => { | |
| const environment = builder.environments[name]; | |
| assert(environment, `"${name}" environment not found`); | |
| return environment; | |
| }); | |
| }); |
| if (!parentEnvironment) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
[P1] Vite clears an environment's outDir on its first build. Now that a child output directory is nested below its parent, a valid custom buildApp that builds the child before the parent loses the already-emitted child bundle when the parent starts. The playground's emptyOutDir: false test setup and parent-first custom build mask this. The plugin already cleans the complete Build Output directory in configResolved, so prevent the parent build from deleting child output.
| if (!parentEnvironment) { | |
| continue; | |
| } | |
| if (!parentEnvironment) { | |
| continue; | |
| } | |
| parentEnvironment.build.emptyOutDir = false; |
|
I'm Bonk, and I've done a quick review of your PR. Adds Build Output support for child Vite environments and typed additional modules.
Posted 2 suggestion comments. |
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| childEnvironment.build.outDir = path.join( | ||
| parentEnvironment.build.outDir, | ||
| childEnvironmentName | ||
| ); |
There was a problem hiding this comment.
🟡 Parent builds erase child output
When builder.build() runs a child before its parent, the parent cleanup deletes the nested child output. The final Worker then lacks required modules.
Prompt for agents
Child environment output directories are nested under their parent in packages/vite-plugin-cloudflare/src/plugins/config.ts:forceBuildOutputDirs. Vite empties an environment's outDir when its build starts. A custom buildApp that builds a child before its parent, or builds them concurrently, therefore lets the parent build delete already-emitted child chunks and additional modules. PluginContext retains the child module metadata, so build-output.ts can also write manifest entries for files no longer present. Make child output preservation independent of build order. Possible approaches include enforcing parent-before-child ordering, preventing the parent build from emptying nested child output, or using temporary/sibling outputs and assembling the final Worker bundle after all builds.
Was this helpful? React with 👍 or 👎 to provide feedback.
Vite plugin v2 - Support child environments
Updates build output implementation to support child environments. A partial manifest is now emitted containing only additional modules (Cloudflare specific module types). ESM modules are collected when reading the build output. This improves compatibility with frameworks, where modules are sometimes emitted outside the Vite pipeline.
A picture of a cute animal (not mandatory, but encouraged)