Skip to content

Commit

Permalink
fix: correctly handle multiple wildcard path aliases (#13059)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Jan 24, 2025
1 parent 46ec06e commit e36837f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-years-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug that caused tsconfig path aliases to break if there was more than one wildcard pattern
5 changes: 2 additions & 3 deletions packages/astro/src/vite-plugin-config-alias/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ const getConfigAlias = (settings: AstroSettings): Alias[] | null => {
.join('')}$`,
);

/** Internal index used to calculate the matching id in a replacement. */
let matchId = 0;

for (const value of values) {
/** Internal index used to calculate the matching id in a replacement. */
let matchId = 0;
/** String used to replace a matched path. */
const replacement = [...normalizePath(path.resolve(resolvedBaseUrl, value))]
.map((segment) => (segment === '*' ? `$${++matchId}` : segment === '$' ? '$$' : segment))
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/alias-tsconfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ describe('Aliases with tsconfig.json', () => {
assert.equal($('#alias').text(), 'foo');
});

it('handles multiple replacements in one alias', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

assert.equal($('#bar').text(), 'bar');
assert.equal($('#baz').text(), 'baz');
});

it('works for import.meta.glob', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p id="bar">bar</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p id="baz">baz</p>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Client from '@components/Client.svelte'
import '@styles/main.css';
import { namespace } from '@test/namespace-package'
import Foo from 'src/components/Foo.astro';
import Bar from "@short/Bar"
import Baz from "@short/Baz"
import StyleComp from 'src/components/Style.astro';
import { foo, index } from 'src/utils/constants';
Expand All @@ -19,6 +21,8 @@ const globResult = Object.keys(import.meta.glob('@components/glob/*.js')).join('
<main>
<Client client:load />
<Foo />
<Bar />
<Baz />
<StyleComp />
<Alias client:load />
<p id="namespace">{namespace}</p>
Expand Down
18 changes: 14 additions & 4 deletions packages/astro/test/fixtures/alias-tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
"src/components/*"
],
"@styles/*": [
"src/styles/*"
"src/styles/*",
"src/more-styles/*"
],
"@short/*": [
"src/components/*.astro",
"src/components/*/index.astro"
],
// this can really trip up namespaced packages
"@*": [
"src/*"
]
}
},
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist"]
}
"include": [
".astro/types.d.ts",
"**/*"
],
"exclude": [
"dist"
]
}

0 comments on commit e36837f

Please sign in to comment.