Fixes using tsconfig to set aliases (#4010)

This commit is contained in:
Matthew Phillips 2022-07-21 13:45:20 -04:00 committed by GitHub
parent 92de6e00a3
commit d503c5bf3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 102 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Allow defining aliases with tsconfig

View file

@ -1,3 +1,4 @@
import type { AstroConfig } from '../@types/astro';
import * as path from 'path';
import * as tsr from 'tsconfig-resolver';
import * as url from 'url';
@ -86,10 +87,7 @@ const getConfigAlias = (cwd: string | undefined): Alias[] | null => {
};
/** Returns a Vite plugin used to alias pathes from tsconfig.json and jsconfig.json. */
export default function configAliasVitePlugin(astroConfig: {
root?: URL;
[key: string]: unknown;
}): vite.PluginOption {
export default function configAliasVitePlugin({ config: astroConfig }: { config: AstroConfig }): vite.PluginOption {
/** Aliases from the tsconfig.json or jsconfig.json configuration. */
const configAlias = getConfigAlias(astroConfig.root && url.fileURLToPath(astroConfig.root));

View file

@ -0,0 +1,38 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { isWindows, loadFixture } from './test-utils.js';
describe('Aliases with tsconfig.json', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/alias-tsconfig/',
});
});
if (isWindows) return;
describe('dev', () => {
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('can load client components', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerio.load(html);
// Should render aliased element
expect($('#client').text()).to.equal('test');
const scripts = $('script').toArray();
expect(scripts.length).to.be.greaterThan(0);
});
});
});

View file

@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import svelte from '@astrojs/svelte';
// https://astro.build/config
export default defineConfig({
integrations: [svelte()],
});

View file

@ -0,0 +1,9 @@
{
"name": "@test/aliases-tsconfig",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/svelte": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,2 @@
<script></script>
<div id="client">test</div>

View file

@ -0,0 +1,15 @@
---
import Client from '@components/Client.svelte'
---
<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 />
</main>
</body>
</html>

View file

@ -0,0 +1,16 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@components/*": [
"src/components/*"
],
"@layouts/*": [
"src/layouts/*"
],
"@assets/*": [
"src/assets/*"
],
}
}
}

View file

@ -1070,6 +1070,14 @@ importers:
'@astrojs/svelte': link:../../../../integrations/svelte
astro: link:../../..
packages/astro/test/fixtures/alias-tsconfig:
specifiers:
'@astrojs/svelte': workspace:*
astro: workspace:*
dependencies:
'@astrojs/svelte': link:../../../../integrations/svelte
astro: link:../../..
packages/astro/test/fixtures/api-routes:
specifiers:
astro: workspace:*