Skip to content

Commit 45cc342

Browse files
authoredJan 16, 2023
perf: don't resolve import path, if it was already resolved (#2659)
1 parent 6c1a26a commit 45cc342

File tree

6 files changed

+42
-25
lines changed

6 files changed

+42
-25
lines changed
 

‎packages/vite-node/src/client.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,15 @@ export class ViteNodeRunner {
213213
if (importee && id.startsWith(VALID_ID_PREFIX))
214214
importee = undefined
215215
id = normalizeRequestId(id, this.options.base)
216-
if (!this.options.resolveId)
217-
return [id, toFilePath(id, this.root)]
216+
const { path, exists } = toFilePath(id, this.root)
217+
if (!this.options.resolveId || exists)
218+
return [id, path]
218219
const resolved = await this.options.resolveId(id, importee)
219220
const resolvedId = resolved
220221
? normalizeRequestId(resolved.id, this.options.base)
221222
: id
222223
// to be compatible with dependencies that do not resolve id
223-
const fsPath = resolved ? resolvedId : toFilePath(id, this.root)
224+
const fsPath = resolved ? resolvedId : path
224225
return [resolvedId, fsPath]
225226
}
226227

@@ -278,7 +279,6 @@ export class ViteNodeRunner {
278279
if (id in requestStubs)
279280
return requestStubs[id]
280281

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

284284
if (externalize) {

‎packages/vite-node/src/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class ViteNodeServer {
125125
private async _fetchModule(id: string): Promise<FetchResult> {
126126
let result: FetchResult
127127

128-
const filePath = toFilePath(id, this.server.config.root)
128+
const { path: filePath } = toFilePath(id, this.server.config.root)
129129

130130
const module = this.server.moduleGraph.getModuleById(id)
131131
const timestamp = module ? module.lastHMRTimestamp : null

‎packages/vite-node/src/utils.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,32 @@ export function isPrimitive(v: any) {
6161
return v !== Object(v)
6262
}
6363

64-
export function toFilePath(id: string, root: string): string {
65-
let absolute = (() => {
64+
export function toFilePath(id: string, root: string): { path: string; exists: boolean } {
65+
let { absolute, exists } = (() => {
6666
if (id.startsWith('/@fs/'))
67-
return id.slice(4)
67+
return { absolute: id.slice(4), exists: true }
68+
// check if /src/module.js -> <root>/src/module.js
6869
if (!id.startsWith(root) && id.startsWith('/')) {
6970
const resolved = resolve(root, id.slice(1))
70-
// The resolved path can have query values. Remove them before checking
71-
// the file path.
72-
if (existsSync(resolved.replace(/\?.*$/, '')))
73-
return resolved
71+
if (existsSync(cleanUrl(resolved)))
72+
return { absolute: resolved, exists: true }
7473
}
75-
return id
74+
else if (id.startsWith(root) && existsSync(cleanUrl(id))) {
75+
return { absolute: id, exists: true }
76+
}
77+
return { absolute: id, exists: false }
7678
})()
7779

7880
if (absolute.startsWith('//'))
7981
absolute = absolute.slice(1)
8082

8183
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
82-
return isWindows && absolute.startsWith('/')
83-
? slash(fileURLToPath(pathToFileURL(absolute.slice(1)).href))
84-
: absolute
84+
return {
85+
path: isWindows && absolute.startsWith('/')
86+
? slash(fileURLToPath(pathToFileURL(absolute.slice(1)).href))
87+
: absolute,
88+
exists,
89+
}
8590
}
8691

8792
/**

‎test/core/test/file-path.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('toFilePath', () => {
8282
const expected = 'C:/path/to/project/node_modules/pkg/file.js'
8383

8484
const processSpy = vi.spyOn(process, 'cwd').mockReturnValue(root)
85-
const filePath = toFilePath(id, root)
85+
const { path: filePath } = toFilePath(id, root)
8686
processSpy.mockRestore()
8787

8888
expect(slash(filePath)).toEqual(expected)
@@ -94,7 +94,7 @@ describe('toFilePath', () => {
9494
const expected = 'C:/path/to/project/node_modules/pkg/file.js'
9595

9696
const processSpy = vi.spyOn(process, 'cwd').mockReturnValue(root)
97-
const filePath = toFilePath(id, root)
97+
const { path: filePath } = toFilePath(id, root)
9898
processSpy.mockRestore()
9999

100100
expect(slash(filePath)).toEqual(expected)
@@ -110,7 +110,7 @@ describe('toFilePath', () => {
110110

111111
const processSpy = vi.spyOn(process, 'cwd').mockReturnValue(root)
112112
const existsSpy = vi.mocked(existsSync).mockReturnValue(true)
113-
const filePath = toFilePath(id, root)
113+
const { path: filePath } = toFilePath(id, root)
114114
processSpy.mockRestore()
115115
existsSpy.mockRestore()
116116

@@ -124,7 +124,7 @@ describe('toFilePath', () => {
124124

125125
const processSpy = vi.spyOn(process, 'cwd').mockReturnValue(root)
126126
const existsSpy = vi.mocked(existsSync).mockReturnValue(true)
127-
const filePath = toFilePath(id, root)
127+
const { path: filePath } = toFilePath(id, root)
128128
processSpy.mockRestore()
129129
existsSpy.mockRestore()
130130

@@ -138,7 +138,7 @@ describe('toFilePath', () => {
138138

139139
const processSpy = vi.spyOn(process, 'cwd').mockReturnValue(root)
140140
const existsSpy = vi.mocked(existsSync).mockReturnValue(true)
141-
const filePath = toFilePath(id, root)
141+
const { path: filePath } = toFilePath(id, root)
142142
processSpy.mockRestore()
143143
existsSpy.mockRestore()
144144

@@ -152,7 +152,7 @@ describe('toFilePath', () => {
152152

153153
const processSpy = vi.spyOn(process, 'cwd').mockReturnValue(root)
154154
const existsSpy = vi.mocked(existsSync).mockReturnValue(true)
155-
const filePath = toFilePath(id, root)
155+
const { path: filePath } = toFilePath(id, root)
156156
processSpy.mockRestore()
157157
existsSpy.mockRestore()
158158

@@ -166,7 +166,7 @@ describe('toFilePath', () => {
166166

167167
const processSpy = vi.spyOn(process, 'cwd').mockReturnValue(root)
168168
const existsSpy = vi.mocked(existsSync).mockReturnValue(true)
169-
const filePath = toFilePath(id, root)
169+
const { path: filePath } = toFilePath(id, root)
170170
processSpy.mockRestore()
171171
existsSpy.mockRestore()
172172

@@ -179,7 +179,7 @@ describe('toFilePath', () => {
179179

180180
const processSpy = vi.spyOn(process, 'cwd').mockReturnValue(root)
181181
const existsSpy = vi.mocked(existsSync).mockReturnValue(false)
182-
const filePath = toFilePath(id, root)
182+
const { path: filePath } = toFilePath(id, root)
183183
processSpy.mockRestore()
184184
existsSpy.mockRestore()
185185

‎test/core/test/imports.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test('dynamic aliased import works', async () => {
2828
expect(stringTimeoutMod).toBe(variableTimeoutMod)
2929
})
3030

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

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

40+
test('dynamic absolute with extension mport works', async () => {
41+
const stringTimeoutMod = await import('./../src/timeout')
42+
43+
const timeoutPath = '/src/timeout.ts'
44+
const variableTimeoutMod = await import(timeoutPath)
45+
46+
expect(stringTimeoutMod).toBe(variableTimeoutMod)
47+
})
48+
4049
test('data with dynamic import works', async () => {
4150
const dataUri = 'data:text/javascript;charset=utf-8,export default "hi"'
4251
const { default: hi } = await import(dataUri)

‎test/vite-node/test/server.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ describe('server works correctly', async () => {
1010
config: {
1111
root: '/',
1212
},
13+
moduleGraph: {
14+
idToModuleMap: new Map(),
15+
},
1316
} as any, {
1417
transformMode: {
1518
web: [/web/],

0 commit comments

Comments
 (0)
Please sign in to comment.