Skip to content

Vite plugin v2 - Support child environments - #15506

Open
jamesopstad wants to merge 1 commit into
vite-plugin-v2-05-stable-prerenderfrom
vite-plugin-v2-06-child-environments
Open

Vite plugin v2 - Support child environments#15506
jamesopstad wants to merge 1 commit into
vite-plugin-v2-05-stable-prerenderfrom
vite-plugin-v2-06-child-environments

Conversation

@jamesopstad

Copy link
Copy Markdown
Contributor

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.


  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: release notes to follow

A picture of a cute animal (not mandatory, but encouraged)

@changeset-bot

changeset-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 763741a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-project-automation github-project-automation Bot moved this to Untriaged in workers-sdk Sep 4, 2026
@jamesopstad jamesopstad added the ci:no-changeset-required Skip pull request checks for a valid changeset label Sep 4, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 4, 2026

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@15506

@cloudflare/build-output-utils

npm i https://pkg.pr.new/@cloudflare/build-output-utils@15506

@cloudflare/codemods

npm i https://pkg.pr.new/@cloudflare/codemods@15506

@cloudflare/config

npm i https://pkg.pr.new/@cloudflare/config@15506

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@15506

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@15506

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@15506

miniflare

npm i https://pkg.pr.new/miniflare@15506

@cloudflare/pages-functions

npm i https://pkg.pr.new/@cloudflare/pages-functions@15506

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@15506

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@15506

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@15506

@cloudflare/vitest-plugin

npm i https://pkg.pr.new/@cloudflare/vitest-plugin@15506

@cloudflare/workers-auth

npm i https://pkg.pr.new/@cloudflare/workers-auth@15506

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@15506

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@15506

wrangler

npm i https://pkg.pr.new/wrangler@15506

commit: 763741a

Comment on lines +62 to +71
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;
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Suggested change
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;
});
});

Comment on lines +234 to +236
if (!parentEnvironment) {
continue;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Suggested change
if (!parentEnvironment) {
continue;
}
if (!parentEnvironment) {
continue;
}
parentEnvironment.build.emptyOutDir = false;

@ask-bonk

ask-bonk Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

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.

  1. P1 build-output.ts:62 does not build child environments under the default builder, leaving production child imports missing.
  2. P1 config.ts:234 allows a parent build to delete a child bundle when custom builders build the child first.

Posted 2 suggestion comments.

github run

@jamesopstad
jamesopstad marked this pull request as ready for review September 4, 2026 09:03

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +242 to +245
childEnvironment.build.outDir = path.join(
parentEnvironment.build.outDir,
childEnvironmentName
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.
Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:no-changeset-required Skip pull request checks for a valid changeset

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

2 participants