diff --git a/.changeset/silent-taxis-act.md b/.changeset/silent-taxis-act.md new file mode 100644 index 000000000000..ad5af7d31241 --- /dev/null +++ b/.changeset/silent-taxis-act.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes tsconfig alias with import.meta.glob diff --git a/packages/astro/src/vite-plugin-config-alias/index.ts b/packages/astro/src/vite-plugin-config-alias/index.ts index e083476a13eb..39ea963e75ab 100644 --- a/packages/astro/src/vite-plugin-config-alias/index.ts +++ b/packages/astro/src/vite-plugin-config-alias/index.ts @@ -84,6 +84,14 @@ export default function configAliasVitePlugin({ for (const alias of configAlias) { if (alias.find.test(id)) { const updatedId = id.replace(alias.find, alias.replacement); + + // Vite may pass an id with "*" when resolving glob import paths + // Returning early allows Vite to handle the final resolution + // See https://github.com/withastro/astro/issues/9258#issuecomment-1838806157 + if (updatedId.includes('*')) { + return updatedId; + } + const resolved = await this.resolve(updatedId, importer, { skipSelf: true, ...options }); if (resolved) return resolved; } diff --git a/packages/astro/test/alias-tsconfig.test.js b/packages/astro/test/alias-tsconfig.test.js index 283a66ea1d15..2342e94a3d9d 100644 --- a/packages/astro/test/alias-tsconfig.test.js +++ b/packages/astro/test/alias-tsconfig.test.js @@ -86,6 +86,13 @@ describe('Aliases with tsconfig.json', () => { expect($('#alias').text()).to.equal('foo'); }); + + it('works for import.meta.glob', async () => { + const html = await fixture.fetch('/').then((res) => res.text()); + const $ = cheerio.load(html); + + expect($('#glob').text()).to.equal('/src/components/glob/a.js'); + }); }); describe('build', () => { @@ -135,5 +142,12 @@ describe('Aliases with tsconfig.json', () => { expect($('#alias').text()).to.equal('foo'); }); + + it('works for import.meta.glob', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + + expect($('#glob').text()).to.equal('/src/components/glob/a.js'); + }); }); }); diff --git a/packages/astro/test/fixtures/alias-tsconfig/src/components/glob/a.js b/packages/astro/test/fixtures/alias-tsconfig/src/components/glob/a.js new file mode 100644 index 000000000000..0ed5c30080aa --- /dev/null +++ b/packages/astro/test/fixtures/alias-tsconfig/src/components/glob/a.js @@ -0,0 +1 @@ +export default 'a'; diff --git a/packages/astro/test/fixtures/alias-tsconfig/src/pages/index.astro b/packages/astro/test/fixtures/alias-tsconfig/src/pages/index.astro index 076d608c6aff..25faad0ea74f 100644 --- a/packages/astro/test/fixtures/alias-tsconfig/src/pages/index.astro +++ b/packages/astro/test/fixtures/alias-tsconfig/src/pages/index.astro @@ -6,6 +6,8 @@ import Alias from '@components/Alias.svelte'; import { namespace } from '@test/namespace-package' import { foo, index } from 'src/utils/constants'; import '@styles/main.css'; + +const globResult = Object.keys(import.meta.glob('@components/glob/*.js')).join(', ') ---
@@ -24,6 +26,7 @@ import '@styles/main.css';{index}
style-red
style-blue
+{globResult}