0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

Fix tsconfig alias regression (#6617)

This commit is contained in:
Happydev 2023-03-22 06:10:10 +00:00 committed by GitHub
parent b37b865400
commit 38e6ec21e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 20 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix tsconfig alias regression

View file

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

View file

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

View file

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

View file

@ -0,0 +1,3 @@
export function whoImI() {
return "I'm a TypeScript file!";
}