Skip to content

Commit

Permalink
Fix tsconfig alias with import.meta.glob (#9560)
Browse files Browse the repository at this point in the history
Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
bluwy and natemoo-re authored Jan 8, 2024
1 parent 24663c9 commit 8b9c484
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-taxis-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes tsconfig alias with import.meta.glob
8 changes: 8 additions & 0 deletions packages/astro/src/vite-plugin-config-alias/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions packages/astro/test/alias-tsconfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'a';
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>

0 comments on commit 8b9c484

Please sign in to comment.