Skip to content

fix(mocker): lazily import createFilter to respect optional vite peer dep#9919

Open
Hassad674 wants to merge 1 commit intovitest-dev:mainfrom
Hassad674:fix/mocker-lazy-vite-import
Open

fix(mocker): lazily import createFilter to respect optional vite peer dep#9919
Hassad674 wants to merge 1 commit intovitest-dev:mainfrom
Hassad674:fix/mocker-lazy-vite-import

Conversation

@Hassad674
Copy link
Copy Markdown

Fixes #9915

The static import { createFilter } from 'vite' in hoistMocksPlugin.ts causes @vitest/mocker/node to fail with ERR_MODULE_NOT_FOUND when vite is not installed, even though vite is declared as an optional peer dependency.

The fix moves the createFilter import into a dynamic await import('vite') inside the buildStart hook. Since buildStart always runs before transform in both Vite and Rollup, the filter is guaranteed to be initialized before any file is processed. When no custom filter is provided and vite isn't available, the plugin gracefully accepts all files.

All other vite imports in the mocker package are already import type (erased at compile time) — this was the only runtime value import.

… peer dep

Replace the static `import { createFilter } from 'vite'` with a
dynamic import inside the buildStart hook so that importing
`@vitest/mocker/node` no longer fails when vite is not installed.

Fixes vitest-dev#9915.
@netlify
Copy link
Copy Markdown

netlify bot commented Mar 19, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit cb16e1f
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69bc8d6dafbada0008220b1d
😎 Deploy Preview https://deploy-preview-9919--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@vitest/mocker 4.1.0: vite declared as optional peer dependency but imported unconditionally in dist/node.js

1 participant