Skip to content

Commit 8b9c484

Browse files
bluwynatemoo-re
andauthoredJan 8, 2024
Fix tsconfig alias with import.meta.glob (#9560)
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
1 parent 24663c9 commit 8b9c484

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed
 

‎.changeset/silent-taxis-act.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Fixes tsconfig alias with import.meta.glob

‎packages/astro/src/vite-plugin-config-alias/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ export default function configAliasVitePlugin({
8484
for (const alias of configAlias) {
8585
if (alias.find.test(id)) {
8686
const updatedId = id.replace(alias.find, alias.replacement);
87+
88+
// Vite may pass an id with "*" when resolving glob import paths
89+
// Returning early allows Vite to handle the final resolution
90+
// See https://github.com/withastro/astro/issues/9258#issuecomment-1838806157
91+
if (updatedId.includes('*')) {
92+
return updatedId;
93+
}
94+
8795
const resolved = await this.resolve(updatedId, importer, { skipSelf: true, ...options });
8896
if (resolved) return resolved;
8997
}

‎packages/astro/test/alias-tsconfig.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ describe('Aliases with tsconfig.json', () => {
8686

8787
expect($('#alias').text()).to.equal('foo');
8888
});
89+
90+
it('works for import.meta.glob', async () => {
91+
const html = await fixture.fetch('/').then((res) => res.text());
92+
const $ = cheerio.load(html);
93+
94+
expect($('#glob').text()).to.equal('/src/components/glob/a.js');
95+
});
8996
});
9097

9198
describe('build', () => {
@@ -135,5 +142,12 @@ describe('Aliases with tsconfig.json', () => {
135142

136143
expect($('#alias').text()).to.equal('foo');
137144
});
145+
146+
it('works for import.meta.glob', async () => {
147+
const html = await fixture.readFile('/index.html');
148+
const $ = cheerio.load(html);
149+
150+
expect($('#glob').text()).to.equal('/src/components/glob/a.js');
151+
});
138152
});
139153
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'a';

‎packages/astro/test/fixtures/alias-tsconfig/src/pages/index.astro

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Alias from '@components/Alias.svelte';
66
import { namespace } from '@test/namespace-package'
77
import { foo, index } from 'src/utils/constants';
88
import '@styles/main.css';
9+
10+
const globResult = Object.keys(import.meta.glob('@components/glob/*.js')).join(', ')
911
---
1012
<html lang="en">
1113
<head>
@@ -24,6 +26,7 @@ import '@styles/main.css';
2426
<p id="constants-index">{index}</p>
2527
<p id="style-red">style-red</p>
2628
<p id="style-blue">style-blue</p>
29+
<p id="glob">{globResult}</p>
2730
</main>
2831
</body>
2932
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.