Skip to content

Commit

Permalink
Fix tsconfig alias regression (#6617)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoustaphaDev authored Mar 22, 2023
1 parent b37b865 commit 38e6ec2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-wasps-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix tsconfig alias regression
12 changes: 7 additions & 5 deletions packages/astro/src/vite-plugin-config-alias/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,18 @@ export default function configAliasVitePlugin({
};
}
},
resolveId(id) {
if (id.startsWith('.') || id.startsWith('/')) return;
async resolveId(id, importer, options) {
if (id.startsWith('.') || path.isAbsolute(id)) return;

// Handle baseUrl mapping for non-relative and non-root imports.
// Since TypeScript only applies `baseUrl` autocompletions for files that exist
// in the filesystem only, we can use this heuristic to skip resolve if needed.
const resolved = path.posix.join(resolvedBaseUrl, id);
if (fs.existsSync(resolved)) {
return resolved;
}

return await this.resolve(resolved, importer, {
skipSelf: true,
...options,
});
},
};
}
8 changes: 7 additions & 1 deletion packages/astro/test/alias-tsconfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ describe('Aliases with tsconfig.json', () => {

it('works in css @import', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
console.log(html);
// imported css should be bundled
expect(html).to.include('#style-red');
expect(html).to.include('#style-blue');
});

it('can load load typescript files without .ts or .d.ts extensions', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerio.load(html);

expect($('#mistery').text()).to.equal("I'm a TypeScript file!");
});
});
});
32 changes: 18 additions & 14 deletions packages/astro/test/fixtures/alias-tsconfig/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
---
import Client from '@components/Client.svelte'
import Client from '@components/Client.svelte';
import Foo from 'src/components/Foo.astro';
import StyleComp from 'src/components/Style.astro';
import '@styles/main.css'
import '@styles/main.css';
import { whoImI } from 'src/ts-file';
const mistery = whoImI();
---

<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Aliases using tsconfig</title>
</head>
<body>
<main>
<Client client:load />
<Foo />
<StyleComp />
</main>
</body>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Aliases using tsconfig</title>
</head>
<body>
<main>
<Client client:load />
<Foo />
<StyleComp />
<div id="mistery">{mistery}</div>
</main>
</body>
</html>
3 changes: 3 additions & 0 deletions packages/astro/test/fixtures/alias-tsconfig/src/ts-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function whoImI() {
return "I'm a TypeScript file!";
}

0 comments on commit 38e6ec2

Please sign in to comment.