[PR #197] [MERGED] fix(cli): ensure type:module before Vite loads vite.config.ts (fixes #184) #381

Closed
opened 2026-05-06 12:39:31 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/197
Author: @yunus25jmi1
Created: 2/28/2026
Status: Merged
Merged: 3/8/2026
Merged by: @james-elicx

Base: mainHead: fix/issue-184-cloudflare-plugin-esm-require


📝 Commits (1)

  • c322986 fix(cli): ensure type:module before Vite loads vite.config.ts (fixes #184)

📊 Changes

3 files changed (+226 additions, -1 deletions)

View changed files

📝 packages/vinext/src/cli.ts (+39 -1)
📝 packages/vinext/src/utils/project.ts (+52 -0)
📝 tests/deploy.test.ts (+135 -0)

📄 Description

Problem

Projects without "type": "module" in package.json cause Vite to bundle vite.config.ts through esbuild's CJS path, producing a .vite-temp/*.timestamp.mjs file that contains require() calls for the imported plugins. On Node 22 in CI (GitHub Actions, ubuntu-latest), require()-ing a .mjs file is not supported and the build fails immediately:

failed to load config from /home/runner/work/.../vite.config.ts
Error: Dynamic require of "file:///…/@cloudflare/vite-plugin/dist/index.mjs" is not supported
    at file:///.../node_modules/.vite-temp/vite.config.ts.timestamp-xxx.mjs:5:9

The affected user had set up their project manually (without vinext init) or before the init step added "type": "module". vinext init already adds this field, but vinext dev / vinext build were not enforcing it.

Fix

Call ensureESModule(root) + renameCJSConfigs(root) in both the dev() and buildApp() CLI commands, before Vite is loaded. This reuses the same logic already used by vinext init.

  • renameCJSConfigs renames postcss.config.js / tailwind.config.js / .eslintrc.js etc. → .cjs first, preventing breakage in those files once "type": "module" is added
  • ensureESModule adds "type": "module" to package.json
  • Both are no-ops when the project is already correctly configured (no extra I/O on the hot path)
  • A warning is printed when a change is made, pointing users to run vinext init to make it permanent

Tests

9 new unit tests added to tests/deploy.test.ts under the describe block "ensureESModule + renameCJSConfigs — issue #184 compatibility":

  • Adds type:module when missing
  • Idempotent — safe to call multiple times
  • Returns false when no package.json exists
  • Renames CJS postcss.config.jspostcss.config.cjs before adding type:module
  • Skips rename for ESM-style postcss.config.js (no module.exports or require())
  • Renames tailwind.config.js when CJS
  • Renames .eslintrc.js when CJS
  • Renames CJS config that uses require() (not only module.exports)
  • Handles multiple CJS config files simultaneously

All 169 tests pass (pnpm exec vitest run tests/deploy.test.ts).

Fixes #184


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/197 **Author:** [@yunus25jmi1](https://github.com/yunus25jmi1) **Created:** 2/28/2026 **Status:** ✅ Merged **Merged:** 3/8/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `fix/issue-184-cloudflare-plugin-esm-require` --- ### 📝 Commits (1) - [`c322986`](https://github.com/cloudflare/vinext/commit/c322986a9e115b45c6d60da818c8e949bfd6daaf) fix(cli): ensure type:module before Vite loads vite.config.ts (fixes #184) ### 📊 Changes **3 files changed** (+226 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/cli.ts` (+39 -1) 📝 `packages/vinext/src/utils/project.ts` (+52 -0) 📝 `tests/deploy.test.ts` (+135 -0) </details> ### 📄 Description ## Problem Projects without `"type": "module"` in `package.json` cause Vite to bundle `vite.config.ts` through esbuild's **CJS path**, producing a `.vite-temp/*.timestamp.mjs` file that contains `require()` calls for the imported plugins. On **Node 22** in CI (GitHub Actions, ubuntu-latest), `require()`-ing a `.mjs` file is not supported and the build fails immediately: ``` failed to load config from /home/runner/work/.../vite.config.ts Error: Dynamic require of "file:///…/@cloudflare/vite-plugin/dist/index.mjs" is not supported at file:///.../node_modules/.vite-temp/vite.config.ts.timestamp-xxx.mjs:5:9 ``` The affected user had set up their project manually (without `vinext init`) or before the `init` step added `"type": "module"`. `vinext init` already adds this field, but `vinext dev` / `vinext build` were not enforcing it. ## Fix Call `ensureESModule(root)` + `renameCJSConfigs(root)` in both the `dev()` and `buildApp()` CLI commands, **before** Vite is loaded. This reuses the same logic already used by `vinext init`. - `renameCJSConfigs` renames `postcss.config.js` / `tailwind.config.js` / `.eslintrc.js` etc. → `.cjs` first, preventing breakage in those files once `"type": "module"` is added - `ensureESModule` adds `"type": "module"` to `package.json` - Both are **no-ops** when the project is already correctly configured (no extra I/O on the hot path) - A warning is printed when a change is made, pointing users to run `vinext init` to make it permanent ## Tests 9 new unit tests added to `tests/deploy.test.ts` under the describe block **"ensureESModule + renameCJSConfigs — issue #184 compatibility"**: - Adds `type:module` when missing - Idempotent — safe to call multiple times - Returns `false` when no `package.json` exists - Renames CJS `postcss.config.js` → `postcss.config.cjs` before adding `type:module` - Skips rename for ESM-style `postcss.config.js` (no `module.exports` or `require()`) - Renames `tailwind.config.js` when CJS - Renames `.eslintrc.js` when CJS - Renames CJS config that uses `require()` (not only `module.exports`) - Handles multiple CJS config files simultaneously All 169 tests pass (`pnpm exec vitest run tests/deploy.test.ts`). Fixes #184 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 12:39:31 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#381
No description provided.