[vite-plugin] fix: allow Node.js built-ins in resolve.external for Worker environments#14218
Conversation
…rker environments Fixes cloudflare#14215. Vitest 4 automatically adds all Node.js built-in module specifiers to `resolve.external` for non-standard Vite environments via its internal `runnerTransform` plugin. The Cloudflare Vite plugin previously threw on any non-empty `resolve.external` array, causing a startup error when Vitest 4 is used alongside the plugin. Filter out Node.js built-in entries (bare and node:-prefixed forms) before validation. Only throw when resolve.external is `true` (externalize everything) or contains non-built-in package names.
🦋 Changeset detectedLatest commit: acde302 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
petebacondarwin
left a comment
There was a problem hiding this comment.
I wonder if there is a way for Vitest to signal to our vite-plugin that it is adding node builtins as external so that we can skip only in this case?
|
Codeowners approval required for this PR:
Show detailed file reviewers |
I looked into this. Vitest 4's The current check is more principled: rather than "skip if Vitest is present," it's "allow Happy to explore the Vitest-specific detection route if you'd prefer it — just let me know. |
…t4-resolve-external
…ternal' into fix/vite-plugin-vitest4-resolve-external
workers-devprod
left a comment
There was a problem hiding this comment.
Codeowners reviews satisfied
Fixes #14215.
Root cause
Vitest 4 added an internal
runnerTransformplugin that automatically setsresolve.externalto the full list of Node.js built-in module specifiers for any Vite environment not named__vitest_vm__or__vitest__. Since Cloudflare Worker environments have custom names, they receive this automaticresolve.externalinjection.The Cloudflare Vite plugin's
validateWorkerEnvironmentOptionspreviously threw on any non-emptyresolve.externalarray, causing a startup error:Fix
Node.js built-in module entries (both bare
fsandnode:fsprefixed forms) are now filtered out before validation usingnode:module'sbuiltinModuleslist. The error is only thrown whenresolve.externalistrue(externalize everything unconditionally) or contains non-built-in package names that would prevent user code from being bundled.