Skip to content

Commit

Permalink
deps(browser): remove modern-node-polyfills package (vitest-dev#3648)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel-G authored and LorenzoBloedow committed Dec 19, 2023
1 parent d02fcb9 commit 101095c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 55 deletions.
1 change: 0 additions & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"dependencies": {
"estree-walker": "^3.0.3",
"magic-string": "^0.30.1",
"modern-node-polyfills": "^1.0.0",
"sirv": "^2.0.3"
},
"devDependencies": {
Expand Down
42 changes: 9 additions & 33 deletions packages/browser/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ import { fileURLToPath } from 'node:url'

import { resolve } from 'node:path'
import { builtinModules } from 'node:module'
import { polyfillPath } from 'modern-node-polyfills'
import sirv from 'sirv'
import type { Plugin } from 'vite'
import { injectVitestModule } from './esmInjector'

const polyfills = [
'util',
]

// don't expose type to not bundle it here
export default (project: any, base = '/'): Plugin[] => {
const pkgRoot = resolve(fileURLToPath(import.meta.url), '../..')
Expand All @@ -36,13 +31,12 @@ export default (project: any, base = '/'): Plugin[] => {
},
},
{
name: 'modern-node-polyfills',
name: 'vitest:browser:tests',
enforce: 'pre',
config() {
return {
optimizeDeps: {
exclude: [
...polyfills,
...builtinModules,
'vitest',
'vitest/utils',
Expand All @@ -63,22 +57,17 @@ export default (project: any, base = '/'): Plugin[] => {
}
},
async resolveId(id) {
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 (!/\?browserv=\w+$/.test(id))
return

if (/^\w:/.test(useId))
useId = useId.replace(/\\/g, '/')
let useId = id.slice(0, id.lastIndexOf('?'))
if (useId.startsWith('/@fs/'))
useId = useId.slice(5)

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

id = normalizeId(id)
return { id: await polyfillPath(id), moduleSideEffects: false }
return useId
},
},
{
Expand All @@ -93,16 +82,3 @@ export default (project: any, base = '/'): Plugin[] => {
},
]
}

function normalizeId(id: string, base?: string): string {
if (base && id.startsWith(base))
id = `/${id.slice(base.length)}`

return id
.replace(/^\/@id\/__x00__/, '\0') // virtual modules start with `\0`
.replace(/^\/@id\//, '')
.replace(/^__vite-browser-external:/, '')
.replace(/^node:/, '')
.replace(/[?&]v=\w+/, '?') // remove ?v= query
.replace(/\?$/, '') // remove end query mark
}
23 changes: 2 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 101095c

Please sign in to comment.