diff options
5 files changed, 31 insertions, 0 deletions
diff --git a/.changeset/silent-taxis-act.md b/.changeset/silent-taxis-act.md new file mode 100644 index 000000000..ad5af7d31 --- /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 e083476a1..39ea963e7 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 283a66ea1..2342e94a3 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 000000000..0ed5c3008 --- /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 076d608c6..25faad0ea 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(', ') --- <html lang="en"> <head> @@ -24,6 +26,7 @@ import '@styles/main.css'; <p id="constants-index">{index}</p> <p id="style-red">style-red</p> <p id="style-blue">style-blue</p> + <p id="glob">{globResult}</p> </main> </body> </html> |