Skip to content

Commit

Permalink
fix(browser): failing to load vitest/utils (#3190)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin authored May 3, 2023
1 parent 036de79 commit 78bad4a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/browser/src/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const ENTRY_URL = `${

let config: ResolvedConfig | undefined
let runner: VitestRunner | undefined
const browserHashMap = new Map<string, string>()
const browserHashMap = new Map<string, [test: boolean, timestamp: string]>()

const url = new URL(location.href)
const testId = url.searchParams.get('id') || 'unknown'
Expand Down Expand Up @@ -130,7 +130,7 @@ async function runTests(paths: string[], config: ResolvedConfig) {
})

const now = `${new Date().getTime()}`
files.forEach(i => browserHashMap.set(i, now))
files.forEach(i => browserHashMap.set(i, [true, now]))

for (const file of files)
await startTests([file], runner)
Expand Down
19 changes: 10 additions & 9 deletions packages/browser/src/client/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ResolvedConfig } from '#types'

interface BrowserRunnerOptions {
config: ResolvedConfig
browserHashMap: Map<string, string>
browserHashMap: Map<string, [test: boolean, timstamp: string]>
}

interface CoverageHandler {
Expand All @@ -14,7 +14,7 @@ interface CoverageHandler {
export function createBrowserRunner(original: any, coverageModule: CoverageHandler | null) {
return class BrowserTestRunner extends original {
public config: ResolvedConfig
hashMap = new Map<string, string>()
hashMap = new Map<string, [test: boolean, timstamp: string]>()

constructor(options: BrowserRunnerOptions) {
super(options.config)
Expand Down Expand Up @@ -54,15 +54,16 @@ export function createBrowserRunner(original: any, coverageModule: CoverageHandl
}

async importFile(filepath: string) {
const match = filepath.match(/^(\w:\/)/)
let hash = this.hashMap.get(filepath)
if (!hash) {
let [test, hash] = this.hashMap.get(filepath) ?? [false, '']
if (hash === '') {
hash = Date.now().toString()
this.hashMap.set(filepath, hash)
this.hashMap.set(filepath, [false, hash])
}
const importpath = match
? `/@fs/${filepath.slice(match[1].length)}?v=${hash}`
: `${filepath}?v=${hash}`

// on Windows we need the unit to resolve the test file
const importpath = /^\w:/.test(filepath)
? `/@fs/${filepath}?${test ? 'browserv' : 'v'}=${hash}`
: `${filepath}?${test ? 'browserv' : 'v'}=${hash}`
await import(importpath)
}
}
Expand Down
31 changes: 28 additions & 3 deletions packages/browser/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,38 @@ export default (project: any, base = '/'): Plugin[] => {
config() {
return {
optimizeDeps: {
exclude: [...polyfills, ...builtinModules],
exclude: [
...polyfills,
...builtinModules,
'vitest',
'vitest/utils',
'vitest/browser',
'vitest/runners',
'@vitest/utils',
],
include: [
'@vitest/utils > concordance',
'@vitest/utils > loupe',
'@vitest/utils > pretty-format',
'vitest > chai',
],
},
}
},
async resolveId(id) {
if (!builtinModules.includes(id) && !polyfills.includes(id) && !id.startsWith('node:'))
return
if (!builtinModules.includes(id) && !polyfills.includes(id) && !id.startsWith('node:')) {
if (!/\?browserv=\w+$/.test(id))
return

let useId = id.slice(0, id.lastIndexOf('?'))
if (useId.startsWith('/@fs/'))
useId = useId.slice(5)

if (/^\w:/.test(useId))
useId = useId.replace(/\\/g, '/')

return useId
}

id = normalizeId(id)
return { id: await polyfillPath(id), moduleSideEffects: false }
Expand Down

0 comments on commit 78bad4a

Please sign in to comment.