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:
parent
24663c9695
commit
8b9c4844f7
5 changed files with 31 additions and 0 deletions
5
.changeset/silent-taxis-act.md
Normal file
5
.changeset/silent-taxis-act.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
Fixes tsconfig alias with import.meta.glob
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
1
packages/astro/test/fixtures/alias-tsconfig/src/components/glob/a.js
vendored
Normal file
1
packages/astro/test/fixtures/alias-tsconfig/src/components/glob/a.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
export default 'a';
|
|
@ -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>
|
||||
|
|
Loading…
Add table
Reference in a new issue