Skip to content

Commit b58b423

Browse files
authored
fix(importMetaGlob): handle alias that starts with hash (#17743)
1 parent d906d3f commit b58b423

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

packages/vite/src/node/plugins/importMetaGlob.ts

-3
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,6 @@ export async function toAbsoluteGlob(
563563
custom: { 'vite:import-glob': { isSubImportsPattern } },
564564
})) || glob,
565565
)
566-
if (isSubImportsPattern) {
567-
return join(root, resolved)
568-
}
569566
if (isAbsolute(resolved)) {
570567
return pre + globSafeResolvedPath(resolved, glob)
571568
}

packages/vite/src/node/plugins/resolve.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
203203
id = resolvedImports
204204

205205
if (resolveOpts.custom?.['vite:import-glob']?.isSubImportsPattern) {
206-
return id
206+
return normalizePath(path.join(root, id))
207207
}
208208
}
209209

playground/glob-import/__tests__/glob-import.spec.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ test('escapes special chars in globs without mangling user supplied glob suffix'
240240
expect(expectedNames).toEqual(foundAliasNames)
241241
})
242242

243-
test('sub imports', async () => {
244-
expect(await page.textContent('.sub-imports')).toMatch('bar foo')
243+
test('subpath imports', async () => {
244+
expect(await page.textContent('.subpath-imports')).toMatch('bar foo')
245+
})
246+
247+
test('#alias imports', async () => {
248+
expect(await page.textContent('.hash-alias-imports')).toMatch('bar foo')
245249
})

playground/glob-import/index.html

+16-11
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,16 @@ <h2>Escape relative glob</h2>
2121
<pre class="escape-relative"></pre>
2222
<h2>Escape alias glob</h2>
2323
<pre class="escape-alias"></pre>
24-
<h2>Sub imports</h2>
25-
<pre class="sub-imports"></pre>
24+
<h2>Subpath imports</h2>
25+
<pre class="subpath-imports"></pre>
26+
<h2>#alias imports</h2>
27+
<pre class="hash-alias-imports"></pre>
2628
<h2>In package</h2>
2729
<pre class="in-package"></pre>
2830

2931
<script type="module" src="./dir/index.js"></script>
3032
<script type="module">
3133
function useImports(modules, selector) {
32-
for (const path in modules) {
33-
modules[path]().then((mod) => {
34-
console.log(path, mod)
35-
})
36-
}
37-
3834
const keys = Object.keys(modules)
3935
Promise.all(keys.map((key) => modules[key]())).then((mods) => {
4036
const res = {}
@@ -137,7 +133,6 @@ <h2>In package</h2>
137133
const globs = import.meta.glob('/escape/**/glob.js', {
138134
eager: true,
139135
})
140-
console.log(globs)
141136
globalThis.globs = globs
142137
const relative = Object.entries(globs)
143138
.filter(([_, mod]) => Object.keys(mod?.relative ?? {}).length === 1)
@@ -152,9 +147,19 @@ <h2>In package</h2>
152147
</script>
153148

154149
<script type="module">
155-
const subImports = import.meta.glob('#imports/*', { eager: true })
150+
const subpathImports = import.meta.glob('#imports/*', { eager: true })
151+
document.querySelector('.subpath-imports').textContent = Object.values(
152+
subpathImports,
153+
)
154+
.map((mod) => mod.default)
155+
.join(' ')
156+
</script>
156157

157-
document.querySelector('.sub-imports').textContent = Object.values(subImports)
158+
<script type="module">
159+
const hashAliasImports = import.meta.glob('#alias/*', { eager: true })
160+
document.querySelector('.hash-alias-imports').textContent = Object.values(
161+
hashAliasImports,
162+
)
158163
.map((mod) => mod.default)
159164
.join(' ')
160165
</script>

playground/glob-import/vite.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default defineConfig({
1919
alias: {
2020
...escapeAliases,
2121
'@dir': path.resolve(__dirname, './dir/'),
22+
'#alias': path.resolve(__dirname, './imports-path/'),
2223
},
2324
},
2425
build: {

0 commit comments

Comments
 (0)