Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: don't resolve import path, if it was already resolved #2659

Merged
merged 9 commits into from Jan 16, 2023
1 change: 0 additions & 1 deletion packages/vite-node/src/client.ts
Expand Up @@ -278,7 +278,6 @@ export class ViteNodeRunner {
if (id in requestStubs)
return requestStubs[id]

// eslint-disable-next-line prefer-const
let { code: transformed, externalize } = await this.options.fetchModule(id)

if (externalize) {
Expand Down
16 changes: 14 additions & 2 deletions packages/vite-node/src/server.ts
@@ -1,10 +1,10 @@
import { performance } from 'node:perf_hooks'
import { resolve } from 'pathe'
import { isAbsolute, join, resolve } from 'pathe'
import type { TransformResult, ViteDevServer } from 'vite'
import createDebug from 'debug'
import type { DebuggerOptions, FetchResult, RawSourceMap, ViteNodeResolveId, ViteNodeServerOptions } from './types'
import { shouldExternalize } from './externalize'
import { normalizeModuleId, toArray, toFilePath } from './utils'
import { isWindows, normalizeModuleId, slash, toArray, toFilePath } from './utils'
import { Debugger } from './debug'
import { withInlineSourcemap } from './source-map'

Expand Down Expand Up @@ -65,6 +65,18 @@ export class ViteNodeServer {
}

async resolveId(id: string, importer?: string): Promise<ViteNodeResolveId | null> {
if (id.startsWith('/@fs/')) {
const fsPath = slash(id.slice(4))
return { id: isWindows ? fsPath.slice(1) : fsPath }
}
if (isAbsolute(id)) {
const moduleMap = this.server.moduleGraph.idToModuleMap
if (moduleMap.has(id))
return { id }
const fsPath = join(this.server.config.root, id)
if (moduleMap.has(fsPath))
return { id: fsPath }
}
if (importer && !importer.startsWith(this.server.config.root))
importer = resolve(this.server.config.root, importer)
const mode = (importer && this.getTransformMode(importer)) || 'ssr'
Expand Down
11 changes: 10 additions & 1 deletion test/core/test/imports.test.ts
Expand Up @@ -28,7 +28,7 @@ test('dynamic aliased import works', async () => {
expect(stringTimeoutMod).toBe(variableTimeoutMod)
})

test('dynamic absolute import works', async () => {
test('dynamic absolute from root import works', async () => {
const stringTimeoutMod = await import('./../src/timeout')

const timeoutPath = '/src/timeout'
Expand All @@ -37,6 +37,15 @@ test('dynamic absolute import works', async () => {
expect(stringTimeoutMod).toBe(variableTimeoutMod)
})

test('dynamic absolute with extension mport works', async () => {
const stringTimeoutMod = await import('./../src/timeout')

const timeoutPath = '/src/timeout.ts'
const variableTimeoutMod = await import(timeoutPath)

expect(stringTimeoutMod).toBe(variableTimeoutMod)
})

test('data with dynamic import works', async () => {
const dataUri = 'data:text/javascript;charset=utf-8,export default "hi"'
const { default: hi } = await import(dataUri)
Expand Down
3 changes: 3 additions & 0 deletions test/vite-node/test/server.test.ts
Expand Up @@ -10,6 +10,9 @@ describe('server works correctly', async () => {
config: {
root: '/',
},
moduleGraph: {
idToModuleMap: new Map(),
},
} as any, {
transformMode: {
web: [/web/],
Expand Down