0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

Fix tsconfig alias with import.meta.glob (#9560)

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Bjorn Lu 2024-01-08 11:06:41 +08:00 committed by GitHub
parent 24663c9695
commit 8b9c4844f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes tsconfig alias with import.meta.glob

View file

@ -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;
}

View file

@ -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');
});
});
});

View file

@ -0,0 +1 @@
export default 'a';

View file

@ -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>