Skip to content

Commit 708734d

Browse files
authored
fix(test): resolve ws CJS named export error with cloudflare preset (#855)
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 fe3189b commit 708734d

5 files changed

Lines changed: 32 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/patch-project.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ const cwd = directory ? join(repoRoot, directory) : repoRoot;
2121
// run vp migrate
2222
const cli = process.env.VITE_PLUS_CLI_BIN ?? 'vp';
2323

24+
// Projects that already have vite-plus need it removed before migration so
25+
// vp migrate treats them as fresh and applies tgz overrides. Without this,
26+
// vp migrate detects "already using Vite+" and skips override injection.
27+
const forceFreshMigration = 'forceFreshMigration' in repoConfig && repoConfig.forceFreshMigration;
28+
if (forceFreshMigration) {
29+
const pkgPath = join(cwd, 'package.json');
30+
const pkg = JSON.parse(await readFile(pkgPath, 'utf-8'));
31+
delete pkg.devDependencies?.['vite-plus'];
32+
delete pkg.dependencies?.['vite-plus'];
33+
await writeFile(pkgPath, JSON.stringify(pkg, null, 2) + '\n', 'utf-8');
34+
}
35+
2436
if (project === 'rollipop') {
2537
const oxfmtrc = await readFile(join(repoRoot, '.oxfmtrc.json'), 'utf-8');
2638
await writeFile(

ecosystem-ci/repo.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,11 @@
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",
62+
"forceFreshMigration": true
5763
}
5864
}

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
@@ -37,6 +37,7 @@
3737
".": {
3838
"import": {
3939
"types": "./dist/index.d.ts",
40+
"browser": "./dist/index.js",
4041
"node": "./dist/index-node.js",
4142
"default": "./dist/index.js"
4243
},

0 commit comments

Comments
 (0)