Skip to content

Commit

Permalink
fix(vite-node): always treat node_modules as modulesDirectory (#3830)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 28, 2023
1 parent 17988e5 commit 51ab8d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/vite-node/src/server.ts
@@ -1,6 +1,6 @@
import { performance } from 'node:perf_hooks'
import { existsSync } from 'node:fs'
import { join, relative, resolve } from 'pathe'
import { join, normalize, relative, resolve } from 'pathe'
import type { TransformResult, ViteDevServer } from 'vite'
import createDebug from 'debug'
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
Expand Down Expand Up @@ -72,6 +72,18 @@ export class ViteNodeServer {
const customModuleDirectories = envValue?.split(',')
if (customModuleDirectories)
options.deps.moduleDirectories.push(...customModuleDirectories)

options.deps.moduleDirectories = options.deps.moduleDirectories.map((dir) => {
if (!dir.startsWith('/'))
dir = `/${dir}`
if (!dir.endsWith('/'))
dir += '/'
return normalize(dir)
})

// always add node_modules as a module directory
if (!options.deps.moduleDirectories.includes('/node_modules/'))
options.deps.moduleDirectories.push('/node_modules/')
}

shouldExternalize(id: string) {
Expand Down
4 changes: 3 additions & 1 deletion packages/vitest/src/node/config.ts
Expand Up @@ -140,14 +140,16 @@ export function resolveConfig(
resolved.deps.inline.push(...extraInlineDeps)
}
}
resolved.deps.moduleDirectories ??= ['/node_modules/']
resolved.deps.moduleDirectories ??= []
resolved.deps.moduleDirectories = resolved.deps.moduleDirectories.map((dir) => {
if (!dir.startsWith('/'))
dir = `/${dir}`
if (!dir.endsWith('/'))
dir += '/'
return normalize(dir)
})
if (!resolved.deps.moduleDirectories.includes('/node_modules/'))
resolved.deps.moduleDirectories.push('/node_modules/')

if (resolved.runner) {
resolved.runner = resolveModule(resolved.runner, { paths: [resolved.root] })
Expand Down

0 comments on commit 51ab8d6

Please sign in to comment.