Skip to content

Commit 8024203

Browse files
committed
fix(test): resolve ws CJS named export error with cloudflare preset
closes #831 When Nuxt's cloudflare preset is active, vitest passes `--conditions browser` to worker processes. This caused `ws` to resolve to its browser stub (`ws/browser.js`) instead of the ESM wrapper (`ws/wrapper.mjs`), because the test package's `node` export condition loaded `index-node.js` which imports `@vitest/browser/index.js` → `ws`. Fix by adding a `browser` export condition before `node` in the test package's main export. When `--conditions browser` is active, `browser` matches first and resolves to `dist/index.js` (which doesn't pull in `@vitest/browser`), avoiding the ws resolution issue. Also fix ecosystem-ci `patch-project.ts` to apply pnpm overrides for projects that already use Vite+ (previously skipped by `vp migrate`), and add the reproduction repo as an e2e test case.
1 parent fdd44d0 commit 8024203

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

.github/workflows/e2e-test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ jobs:
246246
vp run type-check:tsgo
247247
vp run build
248248
vp run test navigation-utils.test.ts real-browser-flicker.test.tsx workflow-parallel-limit.test.tsx
249+
- name: viteplus-ws-repro
250+
node-version: 24
251+
command: |
252+
vp test run
249253
exclude:
250254
# frm-stack uses Docker (testcontainers) which doesn't work the same way on Windows
251255
- os: windows-latest

ecosystem-ci/repo.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,10 @@
5454
"repository": "https://github.com/fengmk2/vite-vue-vercel.git",
5555
"branch": "main",
5656
"hash": "f2bf9fc40880c6a80f5d89bff70641c2eeaf77ef"
57+
},
58+
"viteplus-ws-repro": {
59+
"repository": "https://github.com/Charles5277/viteplus-ws-repro.git",
60+
"branch": "main",
61+
"hash": "451925ad7c07750a23de1d6ed454825d0eb14092"
5762
}
5863
}

packages/test/build.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,18 @@ async function mergePackageJson(pluginExports: Array<{ exportPath: string; shimF
302302
// browser-provider exports. Browser code uses index.js which is safe.
303303
// This separation prevents Node.js-only code (like __vite__injectQuery) from being
304304
// loaded in the browser, which would cause "Identifier already declared" errors.
305+
//
306+
// IMPORTANT: The 'browser' condition must come BEFORE 'node' because vitest passes
307+
// custom --conditions (like 'browser') to worker processes when frameworks like Nuxt
308+
// set edge/cloudflare presets. Without the 'browser' condition here, Node.js would
309+
// match 'node' first, loading index-node.js which imports @vitest/browser/index.js,
310+
// which imports 'ws'. With --conditions browser active, 'ws' resolves to its browser
311+
// stub (ws/browser.js) that doesn't export WebSocketServer, causing a SyntaxError.
312+
// See: https://github.com/voidzero-dev/vite-plus/issues/831
305313
if (destPkg.exports['.'] && destPkg.exports['.'].import) {
306314
destPkg.exports['.'].import = {
307315
types: destPkg.exports['.'].import.types,
316+
browser: destPkg.exports['.'].import.default,
308317
node: './dist/index-node.js',
309318
default: destPkg.exports['.'].import.default,
310319
};

packages/test/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
".": {
2727
"import": {
2828
"types": "./dist/index.d.ts",
29+
"browser": "./dist/index.js",
2930
"node": "./dist/index-node.js",
3031
"default": "./dist/index.js"
3132
},

0 commit comments

Comments
 (0)