fix(importMetaGlob): handle alias that starts with hash (#17743)

This commit is contained in:
Bjorn Lu 2024-07-23 19:00:31 +08:00 committed by GitHub
parent d906d3f8e1
commit b58b423ba8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 24 additions and 17 deletions

View File

@ -563,9 +563,6 @@ export async function toAbsoluteGlob(
custom: { 'vite:import-glob': { isSubImportsPattern } },
})) || glob,
)
if (isSubImportsPattern) {
return join(root, resolved)
}
if (isAbsolute(resolved)) {
return pre + globSafeResolvedPath(resolved, glob)
}

View File

@ -203,7 +203,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
id = resolvedImports
if (resolveOpts.custom?.['vite:import-glob']?.isSubImportsPattern) {
return id
return normalizePath(path.join(root, id))
}
}

View File

@ -240,6 +240,10 @@ test('escapes special chars in globs without mangling user supplied glob suffix'
expect(expectedNames).toEqual(foundAliasNames)
})
test('sub imports', async () => {
expect(await page.textContent('.sub-imports')).toMatch('bar foo')
test('subpath imports', async () => {
expect(await page.textContent('.subpath-imports')).toMatch('bar foo')
})
test('#alias imports', async () => {
expect(await page.textContent('.hash-alias-imports')).toMatch('bar foo')
})

View File

@ -21,20 +21,16 @@
<pre class="escape-relative"></pre>
<h2>Escape alias glob</h2>
<pre class="escape-alias"></pre>
<h2>Sub imports</h2>
<pre class="sub-imports"></pre>
<h2>Subpath imports</h2>
<pre class="subpath-imports"></pre>
<h2>#alias imports</h2>
<pre class="hash-alias-imports"></pre>
<h2>In package</h2>
<pre class="in-package"></pre>
<script type="module" src="./dir/index.js"></script>
<script type="module">
function useImports(modules, selector) {
for (const path in modules) {
modules[path]().then((mod) => {
console.log(path, mod)
})
}
const keys = Object.keys(modules)
Promise.all(keys.map((key) => modules[key]())).then((mods) => {
const res = {}
@ -137,7 +133,6 @@
const globs = import.meta.glob('/escape/**/glob.js', {
eager: true,
})
console.log(globs)
globalThis.globs = globs
const relative = Object.entries(globs)
.filter(([_, mod]) => Object.keys(mod?.relative ?? {}).length === 1)
@ -152,9 +147,19 @@
</script>
<script type="module">
const subImports = import.meta.glob('#imports/*', { eager: true })
const subpathImports = import.meta.glob('#imports/*', { eager: true })
document.querySelector('.subpath-imports').textContent = Object.values(
subpathImports,
)
.map((mod) => mod.default)
.join(' ')
</script>
document.querySelector('.sub-imports').textContent = Object.values(subImports)
<script type="module">
const hashAliasImports = import.meta.glob('#alias/*', { eager: true })
document.querySelector('.hash-alias-imports').textContent = Object.values(
hashAliasImports,
)
.map((mod) => mod.default)
.join(' ')
</script>

View File

@ -19,6 +19,7 @@ export default defineConfig({
alias: {
...escapeAliases,
'@dir': path.resolve(__dirname, './dir/'),
'#alias': path.resolve(__dirname, './imports-path/'),
},
},
build: {