Skip to content

Commit

Permalink
fix(importMetaGlob): handle alias that starts with hash
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 23, 2024
1 parent 4fc9b64 commit 48a3fd0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,12 @@ export async function toAbsoluteGlob(
custom: { 'vite:import-glob': { isSubImportsPattern } },
})) || glob,
)
if (isSubImportsPattern) {
// If matching a subpath import, the returned path is relative and doesn't look absolute.
// If it matches an alias that starts with `#`, the path will be absolute.
if (isSubImportsPattern && !isAbsolute(resolved)) {
return join(root, resolved)
}
if (isAbsolute(resolved)) {
if (isSubImportsPattern || isAbsolute(resolved)) {
return pre + globSafeResolvedPath(resolved, glob)
}

Expand Down
8 changes: 6 additions & 2 deletions playground/glob-import/__tests__/glob-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
27 changes: 16 additions & 11 deletions playground/glob-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ <h2>Escape relative glob</h2>
<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 = {}
Expand Down Expand Up @@ -137,7 +133,6 @@ <h2>In package</h2>
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)
Expand All @@ -152,9 +147,19 @@ <h2>In package</h2>
</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>
Expand Down
1 change: 1 addition & 0 deletions playground/glob-import/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default defineConfig({
alias: {
...escapeAliases,
'@dir': path.resolve(__dirname, './dir/'),
'#alias': path.resolve(__dirname, './imports-path/'),
},
},
build: {
Expand Down

0 comments on commit 48a3fd0

Please sign in to comment.