Skip to content
Open
Changes from all 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
11 changes: 8 additions & 3 deletions packages/mocker/src/node/hoistMocksPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { SourceMap } from 'magic-string'
import type { Plugin, Rollup } from 'vite'
import type { HoistMocksOptions } from './hoistMocks'
import { createFilter } from 'vite'
import { cleanUrl } from '../utils'
import { hoistMocks } from './hoistMocks'

Expand All @@ -15,7 +14,7 @@ export interface HoistMocksPluginOptions extends Omit<HoistMocksOptions, 'regexp
}

export function hoistMocksPlugin(options: HoistMocksPluginOptions = {}): Plugin {
const filter = options.filter || createFilter(options.include, options.exclude)
let filter: ((id: string) => boolean) | undefined = options.filter

const {
hoistableMockMethodNames = ['mock', 'unmock'],
Expand All @@ -37,8 +36,14 @@ export function hoistMocksPlugin(options: HoistMocksPluginOptions = {}): Plugin
return {
name: 'vitest:mocks',
enforce: 'post',
async buildStart() {
if (!filter) {
const { createFilter } = await import('vite')
filter = createFilter(options.include, options.exclude)
}
},
transform(code, id) {
if (!filter(id)) {
if (filter && !filter(id)) {
return
}
const s = hoistMocks(code, id, this.parse, {
Expand Down
Loading