diff --git a/.changeset/spotty-apricots-deny.md b/.changeset/spotty-apricots-deny.md new file mode 100644 index 000000000..36d62d973 --- /dev/null +++ b/.changeset/spotty-apricots-deny.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Allow defining aliases with tsconfig diff --git a/packages/astro/src/vite-plugin-config-alias/index.ts b/packages/astro/src/vite-plugin-config-alias/index.ts index b35ca5afe..63360f3b0 100644 --- a/packages/astro/src/vite-plugin-config-alias/index.ts +++ b/packages/astro/src/vite-plugin-config-alias/index.ts @@ -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)); diff --git a/packages/astro/test/alias-tsconfig.test.js b/packages/astro/test/alias-tsconfig.test.js new file mode 100644 index 000000000..1468aabeb --- /dev/null +++ b/packages/astro/test/alias-tsconfig.test.js @@ -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); + }); + }); +}); diff --git a/packages/astro/test/fixtures/alias-tsconfig/astro.config.mjs b/packages/astro/test/fixtures/alias-tsconfig/astro.config.mjs new file mode 100644 index 000000000..77fdcd1b9 --- /dev/null +++ b/packages/astro/test/fixtures/alias-tsconfig/astro.config.mjs @@ -0,0 +1,7 @@ +import { defineConfig } from 'astro/config'; +import svelte from '@astrojs/svelte'; + +// https://astro.build/config +export default defineConfig({ + integrations: [svelte()], +}); diff --git a/packages/astro/test/fixtures/alias-tsconfig/package.json b/packages/astro/test/fixtures/alias-tsconfig/package.json new file mode 100644 index 000000000..0217d35fe --- /dev/null +++ b/packages/astro/test/fixtures/alias-tsconfig/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/aliases-tsconfig", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/svelte": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/alias-tsconfig/src/components/Client.svelte b/packages/astro/test/fixtures/alias-tsconfig/src/components/Client.svelte new file mode 100644 index 000000000..2450d326a --- /dev/null +++ b/packages/astro/test/fixtures/alias-tsconfig/src/components/Client.svelte @@ -0,0 +1,2 @@ + +