Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

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