mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
fix(importMetaGlob): handle alias that starts with hash (#17743)
This commit is contained in:
parent
d906d3f8e1
commit
b58b423ba8
@ -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)
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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')
|
||||
})
|
||||
|
@ -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>
|
||||
|
@ -19,6 +19,7 @@ export default defineConfig({
|
||||
alias: {
|
||||
...escapeAliases,
|
||||
'@dir': path.resolve(__dirname, './dir/'),
|
||||
'#alias': path.resolve(__dirname, './imports-path/'),
|
||||
},
|
||||
},
|
||||
build: {
|
||||
|
Loading…
Reference in New Issue
Block a user