Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions packages/test/BUNDLING.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ For maintainers developing the vitest/vite migration feature, here are the trans
| `from "vite"` | `from "@voidzero-dev/vite-plus-core"` |
| `from "vite/module-runner"` | `from "@voidzero-dev/vite-plus-core/module-runner"` |

**Note:** pnpm overrides don't affect Node.js module resolution at config load time, so config files must use the `@voidzero-dev/vite-plus-test/browser-playwright` import path.
**Note:** When using pnpm overrides, you have three options for the playwright import:

- `vitest/browser-playwright` - works when `vitest` is overridden to our package (Recommended)
- `@voidzero-dev/vite-plus-test/browser-playwright` - direct import from test package
- `@voidzero-dev/vite-plus/test/plugins/browser-playwright` - direct import from CLI package
Comment thread
Brooooooklyn marked this conversation as resolved.

Importing from `@vitest/browser-playwright` requires an additional override for that specific package.

### package.json Changes

Expand Down Expand Up @@ -151,8 +157,14 @@ overrides:
// Before
import { playwright } from '@vitest/browser-playwright';

// After
// After - Option 1 (Recommended): Via vitest subpath (works when vitest is overridden)
import { playwright } from 'vitest/browser-playwright';

// After - Option 2: Direct import from test package
import { playwright } from '@voidzero-dev/vite-plus-test/browser-playwright';

// After - Option 3: Direct import from CLI package
import { playwright } from '@voidzero-dev/vite-plus/test/plugins/browser-playwright';
Comment thread
Brooooooklyn marked this conversation as resolved.
```

### Plugin Exports for pnpm Overrides
Expand Down Expand Up @@ -315,7 +327,12 @@ This is achieved through:
1. Conditional exports in package.json (`"node": "./dist/index-node.js"`)
2. Browser-safe stubs for `module-runner`
3. Import rewriting to prevent Node.js code from being pulled into browser bundles
4. `vendor-aliases` plugin injection to resolve imports at runtime
4. `vendor-aliases` plugin injection to resolve imports at runtime:
- Handles `@vitest/*` imports → resolves to copied `dist/@vitest/` files
- Handles `vitest/*` subpaths → resolves to dist files (enables `vitest/browser-playwright` usage)
- Handles `@voidzero-dev/vite-plus-test/*` subpaths → maps to equivalent vitest paths
- Handles `@voidzero-dev/vite-plus/test/*` subpaths → maps to equivalent vitest paths (CLI package)
- Intercepts `vitest/browser`, `@voidzero-dev/vite-plus-test/browser`, `@voidzero-dev/vite-plus/test/browser` → returns virtual module ID for BrowserContext plugin

### Key Constants

Expand Down
67 changes: 65 additions & 2 deletions packages/test/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,46 @@ async function patchVitestBrowserPackage() {
if (id === '${CORE_PACKAGE_NAME}' || id === 'vite') {
return { id, external: true };
}
// Handle vitest/browser and package aliases
// Return virtual module ID so BrowserContext plugin can load it
// Supports: vitest/browser, @voidzero-dev/vite-plus-test/browser, @voidzero-dev/vite-plus/test/browser
if (id === 'vitest/browser' || id === '@voidzero-dev/vite-plus-test/browser' || id === '@voidzero-dev/vite-plus/test/browser') {
return '\\0vitest/browser';
}
// Handle vitest/* subpaths (resolve to our dist files)
const vitestSubpathMap = {
'vitest': resolve(packageRoot, 'index.js'),
'vitest/node': resolve(packageRoot, 'node.js'),
'vitest/config': resolve(packageRoot, 'config.js'),
'vitest/internal/browser': resolve(packageRoot, 'browser.js'),
'vitest/runners': resolve(packageRoot, 'runners.js'),
'vitest/suite': resolve(packageRoot, 'suite.js'),
'vitest/environments': resolve(packageRoot, 'environments.js'),
'vitest/coverage': resolve(packageRoot, 'coverage.js'),
'vitest/reporters': resolve(packageRoot, 'reporters.js'),
'vitest/snapshot': resolve(packageRoot, 'snapshot.js'),
'vitest/mocker': resolve(packageRoot, 'mocker.js'),
Comment thread
Brooooooklyn marked this conversation as resolved.
};
Comment thread
Brooooooklyn marked this conversation as resolved.
if (vitestSubpathMap[id]) {
return vitestSubpathMap[id];
}
// Handle @voidzero-dev/vite-plus-test/* subpaths (same as vitest/*)
if (id.startsWith('@voidzero-dev/vite-plus-test/')) {
const subpath = id.slice('@voidzero-dev/vite-plus-test/'.length);
const vitestEquiv = 'vitest/' + subpath;
if (vitestSubpathMap[vitestEquiv]) {
return vitestSubpathMap[vitestEquiv];
}
}
// Handle @voidzero-dev/vite-plus/test/* subpaths (CLI package paths, same as vitest/*)
if (id.startsWith('@voidzero-dev/vite-plus/test/')) {
const subpath = id.slice('@voidzero-dev/vite-plus/test/'.length);
const vitestEquiv = 'vitest/' + subpath;
if (vitestSubpathMap[vitestEquiv]) {
return vitestSubpathMap[vitestEquiv];
}
}
// Handle @vitest/* packages (resolve to our copied files)
const vendorMap = {
${mappingEntries}
};
Expand All @@ -1163,7 +1203,10 @@ async function patchVitestBrowserPackage() {
content = content.replace(pluginArrayPattern, `$1\n ${vendorAliasesPlugin},$2`);
console.log(' Injected vitest:vendor-aliases plugin');
} else {
console.log(' Warning: Could not find browser plugin array to inject vendor-aliases');
throw new Error(
'Failed to inject vendor-aliases plugin in @vitest/browser/index.js: pattern not found. ' +
'This likely means vitest code has changed and the patch needs to be updated.',
);
}

// 2. Patch exclude list to add native deps
Expand All @@ -1185,7 +1228,10 @@ async function patchVitestBrowserPackage() {
content = content.replace(excludePattern, excludeReplacement);
console.log(' Patched exclude list with native deps');
} else {
console.log(' Warning: Could not find exclude array to patch');
throw new Error(
'Failed to patch exclude list in @vitest/browser/index.js: pattern not found. ' +
'This likely means vitest code has changed and the patch needs to be updated.',
);
}

// 3. Remove include patterns that reference bundled deps
Expand All @@ -1204,6 +1250,23 @@ async function patchVitestBrowserPackage() {
}
console.log(' Removed bundled deps from include list');

// 4. Patch BrowserContext to also handle our package aliases as fallback
// This allows direct imports from our package without requiring vitest override
// Supports: @voidzero-dev/vite-plus-test/browser, @voidzero-dev/vite-plus/test/browser
Comment thread
Brooooooklyn marked this conversation as resolved.
Outdated
const browserContextPattern = /if \(id === ID_CONTEXT\) \{/;
if (browserContextPattern.test(content)) {
content = content.replace(
browserContextPattern,
`if (id === ID_CONTEXT || id === "@voidzero-dev/vite-plus-test/browser" || id === "@voidzero-dev/vite-plus/test/browser") {`,
);
console.log(' Patched BrowserContext to handle package aliases');
} else {
throw new Error(
'Failed to patch BrowserContext in @vitest/browser/index.js: pattern not found. ' +
'This likely means vitest code has changed and the patch needs to be updated.',
);
}

await writeFile(browserIndexPath, content, 'utf-8');
console.log(' Successfully patched @vitest/browser/index.js');
}
Expand Down
Loading