fix(cli): ensure type:module before Vite loads vite.config.ts (fixes #184)#197
Open
yunus25jmi1 wants to merge 1 commit intocloudflare:mainfrom
Open
Conversation
…loudflare#184) Projects that lack "type":"module" in package.json caused Vite to bundle vite.config.ts through esbuild's CJS path, producing a .vite-temp/*.mjs file that contains a CJS require() for @cloudflare/vite-plugin. On Node 22 in CI (GitHub Actions ubuntu-latest), require()-ing an .mjs file is not supported and the build fails with: Error: Dynamic require of "…/@cloudflare/vite-plugin/dist/index.mjs" is not supported Fix: call ensureESModule(root) + renameCJSConfigs(root) in both the dev() and buildApp() CLI commands, before Vite is loaded. This mirrors what vinext init already does, but applies lazily for projects that were set up manually or before the init step was introduced. - renameCJSConfigs renames postcss.config.js / tailwind.config.js / .eslintrc.js etc. to .cjs first, to avoid breakage in those files after ESM mode is enabled - ensureESModule adds "type":"module" to package.json - Both are no-ops when the project is already correctly configured - A warning is emitted when a change is made, pointing users to vinext init - 9 new unit tests cover the full scenario including multi-file rename Fixes cloudflare#184
Collaborator
|
/bigbonk review |
commit: |
Contributor
Author
|
@copilot The failing job encountered repeated 500 Internal Server Error responses from GitHub during git fetch operations, resulting in exit code 128. This is not an issue with your code or workflow configuration. |
Contributor
Author
|
@southpolesteve @elithrar kindly review the changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Projects without
"type": "module"inpackage.jsoncause Vite to bundlevite.config.tsthrough esbuild's CJS path, producing a.vite-temp/*.timestamp.mjsfile that containsrequire()calls for the imported plugins. On Node 22 in CI (GitHub Actions, ubuntu-latest),require()-ing a.mjsfile is not supported and the build fails immediately:The affected user had set up their project manually (without
vinext init) or before theinitstep added"type": "module".vinext initalready adds this field, butvinext dev/vinext buildwere not enforcing it.Fix
Call
ensureESModule(root)+renameCJSConfigs(root)in both thedev()andbuildApp()CLI commands, before Vite is loaded. This reuses the same logic already used byvinext init.renameCJSConfigsrenamespostcss.config.js/tailwind.config.js/.eslintrc.jsetc. →.cjsfirst, preventing breakage in those files once"type": "module"is addedensureESModuleadds"type": "module"topackage.jsonvinext initto make it permanentTests
9 new unit tests added to
tests/deploy.test.tsunder the describe block "ensureESModule + renameCJSConfigs — issue #184 compatibility":type:modulewhen missingfalsewhen nopackage.jsonexistspostcss.config.js→postcss.config.cjsbefore addingtype:modulepostcss.config.js(nomodule.exportsorrequire())tailwind.config.jswhen CJS.eslintrc.jswhen CJSrequire()(not onlymodule.exports)All 169 tests pass (
pnpm exec vitest run tests/deploy.test.ts).Fixes #184