Skip to content

Commit ddfe20d

Browse files
fix(optimizer): scan is not resolving sub path import if used in a glob import (#22018)
Co-authored-by: Kalven Schraut <kalvens@rtvision.com>
1 parent 43fbbf9 commit ddfe20d

File tree

6 files changed

+56
-5
lines changed

6 files changed

+56
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const modules = import.meta.glob('#src/foo/*.ts')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "scan-subpath-import-glob",
3+
"private": true,
4+
"imports": {
5+
"#src/*": "./src/*"
6+
}
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 'a'

packages/vite/src/node/__tests__/scan.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import path from 'node:path'
22
import { describe, expect, test } from 'vitest'
3-
import { commentRE, importsRE, scriptRE } from '../optimizer/scan'
3+
import {
4+
commentRE,
5+
devToScanEnvironment,
6+
importsRE,
7+
scanImports,
8+
scriptRE,
9+
} from '../optimizer/scan'
410
import { multilineCommentsRE, singlelineCommentsRE } from '../utils'
511
import { createServer, createServerModuleRunner } from '..'
612

@@ -168,3 +174,28 @@ test('scan jsx-runtime', async (ctx) => {
168174
expect((globalThis as any).__test_scan_jsx_runtime).toBe(1)
169175
expect(mod1).toBe(mod2)
170176
})
177+
178+
test('scan import.meta.glob package imports patterns', async (ctx) => {
179+
const server = await createServer({
180+
configFile: false,
181+
logLevel: 'error',
182+
root: path.join(
183+
import.meta.dirname,
184+
'fixtures',
185+
'scan-subpath-import-glob',
186+
),
187+
optimizeDeps: {
188+
entries: ['./entry.ts'],
189+
force: true,
190+
noDiscovery: false,
191+
},
192+
})
193+
ctx.onTestFinished(() => server.close())
194+
195+
const { cancel, result } = scanImports(
196+
devToScanEnvironment(server.environments.client),
197+
)
198+
ctx.onTestFinished(cancel)
199+
200+
await expect(result).resolves.toEqual({ deps: {}, missing: {} })
201+
})

packages/vite/src/node/optimizer/scan.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,28 @@ function rolldownScanPlugin(
348348
async function resolveId(
349349
id: string,
350350
importer?: string,
351+
options?: Parameters<EnvironmentPluginContainer['resolveId']>[2],
351352
): Promise<PartialResolvedId | null> {
352353
return environment.pluginContainer.resolveId(
353354
id,
354355
importer && normalizePath(importer),
355-
{ scan: true },
356+
{ scan: true, ...options },
356357
)
357358
}
358-
const resolve = async (id: string, importer?: string) => {
359-
const key = id + (importer && path.dirname(importer))
359+
const resolve = async (
360+
id: string,
361+
importer?: string,
362+
options?: Parameters<typeof resolveId>[2],
363+
) => {
364+
const key = JSON.stringify([
365+
id,
366+
importer && path.dirname(importer),
367+
options,
368+
])
360369
if (seen.has(key)) {
361370
return seen.get(key)
362371
}
363-
const resolved = await resolveId(id, importer)
372+
const resolved = await resolveId(id, importer, options)
364373
const res = resolved?.id
365374
seen.set(key, res)
366375
return res

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)