mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Remove baseUrl
requirement
This commit is contained in:
parent
799c8676df
commit
18723ab4b9
20 changed files with 282 additions and 4 deletions
5
.changeset/polite-boxes-tease.md
Normal file
5
.changeset/polite-boxes-tease.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Remove `baseUrl` requirement for tsconfig path aliases. If a `baseUrl` is not set, `paths` entries in your tsconfig file will resolve relative to the tsconfig file. This aligns with [TypeScript’s module resolution strategy](https://www.typescriptlang.org/docs/handbook/modules/reference.html#relationship-to-baseurl).
|
|
@ -7,9 +7,8 @@ Consider the following example configuration:
|
|||
```
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": "src",
|
||||
"paths": {
|
||||
"components:*": ["components/*.astro"]
|
||||
"components:*": ["./src/components/*.astro"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,8 +13,9 @@ const getConfigAlias = (settings: AstroSettings): Alias[] | null => {
|
|||
const { tsConfig, tsConfigPath } = settings;
|
||||
if (!tsConfig || !tsConfigPath || !tsConfig.compilerOptions) return null;
|
||||
|
||||
const { baseUrl, paths } = tsConfig.compilerOptions as CompilerOptions;
|
||||
if (!baseUrl) return null;
|
||||
// TypeScript resolves `paths` relative to the tsconfig file if `baseUrl` is not set
|
||||
// https://www.typescriptlang.org/docs/handbook/modules/reference.html#relationship-to-baseurl
|
||||
const { baseUrl = '.', paths } = tsConfig.compilerOptions as CompilerOptions;
|
||||
|
||||
// resolve the base url from the configuration file directory
|
||||
const resolvedBaseUrl = path.resolve(path.dirname(tsConfigPath), baseUrl);
|
||||
|
|
154
packages/astro/test/alias-tsconfig-no-baseurl.test.js
Normal file
154
packages/astro/test/alias-tsconfig-no-baseurl.test.js
Normal file
|
@ -0,0 +1,154 @@
|
|||
import assert from 'node:assert/strict';
|
||||
import { after, before, describe, it } from 'node:test';
|
||||
import * as cheerio from 'cheerio';
|
||||
import { loadFixture } from './test-utils.js';
|
||||
|
||||
describe('Aliases with tsconfig.json', () => {
|
||||
let fixture;
|
||||
|
||||
/**
|
||||
* @param {string} html
|
||||
* @returns {string[]}
|
||||
*/
|
||||
function getLinks(html) {
|
||||
let $ = cheerio.load(html);
|
||||
let out = [];
|
||||
$('link[rel=stylesheet]').each((_i, el) => {
|
||||
out.push($(el).attr('href'));
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} href
|
||||
* @returns {Promise<{ href: string; css: string; }>}
|
||||
*/
|
||||
async function getLinkContent(href, f = fixture) {
|
||||
const css = await f.readFile(href);
|
||||
return { href, css };
|
||||
}
|
||||
|
||||
before(async () => {
|
||||
fixture = await loadFixture({
|
||||
// test suite was authored when inlineStylesheets defaulted to never
|
||||
build: { inlineStylesheets: 'never' },
|
||||
root: './fixtures/alias-tsconfig-no-baseurl/',
|
||||
});
|
||||
});
|
||||
|
||||
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
|
||||
assert.equal($('#client').text(), 'test');
|
||||
|
||||
const scripts = $('script').toArray();
|
||||
assert.ok(scripts.length > 0);
|
||||
});
|
||||
|
||||
it('can load via baseUrl', async () => {
|
||||
const html = await fixture.fetch('/').then((res) => res.text());
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
assert.equal($('#foo').text(), 'foo');
|
||||
assert.equal($('#constants-foo').text(), 'foo');
|
||||
assert.equal($('#constants-index').text(), 'index');
|
||||
});
|
||||
|
||||
it('can load namespace packages with @* paths', async () => {
|
||||
const html = await fixture.fetch('/').then((res) => res.text());
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
assert.equal($('#namespace').text(), 'namespace');
|
||||
});
|
||||
|
||||
it('works in css @import', async () => {
|
||||
const html = await fixture.fetch('/').then((res) => res.text());
|
||||
// imported css should be bundled
|
||||
assert.ok(html.includes('#style-red'));
|
||||
assert.ok(html.includes('#style-blue'));
|
||||
});
|
||||
|
||||
it('works in components', async () => {
|
||||
const html = await fixture.fetch('/').then((res) => res.text());
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
assert.equal($('#alias').text(), 'foo');
|
||||
});
|
||||
|
||||
it('works for import.meta.glob', async () => {
|
||||
const html = await fixture.fetch('/').then((res) => res.text());
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
assert.equal($('#glob').text(), '/src/components/glob/a.js');
|
||||
});
|
||||
});
|
||||
|
||||
describe('build', () => {
|
||||
before(async () => {
|
||||
await fixture.build();
|
||||
});
|
||||
|
||||
it('can load client components', async () => {
|
||||
const html = await fixture.readFile('/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
// Should render aliased element
|
||||
assert.equal($('#client').text(), 'test');
|
||||
|
||||
const scripts = $('script').toArray();
|
||||
assert.ok(scripts.length > 0);
|
||||
});
|
||||
|
||||
it('can load via baseUrl', async () => {
|
||||
const html = await fixture.readFile('/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
assert.equal($('#foo').text(), 'foo');
|
||||
assert.equal($('#constants-foo').text(), 'foo');
|
||||
assert.equal($('#constants-index').text(), 'index');
|
||||
});
|
||||
|
||||
it('can load namespace packages with @* paths', async () => {
|
||||
const html = await fixture.readFile('/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
assert.equal($('#namespace').text(), 'namespace');
|
||||
});
|
||||
|
||||
it('works in css @import', async () => {
|
||||
const html = await fixture.readFile('/index.html');
|
||||
const content = await Promise.all(getLinks(html).map((href) => getLinkContent(href)));
|
||||
const [{ css }] = content;
|
||||
// imported css should be bundled
|
||||
assert.ok(css.includes('#style-red'));
|
||||
assert.ok(css.includes('#style-blue'));
|
||||
});
|
||||
|
||||
it('works in components', async () => {
|
||||
const html = await fixture.readFile('/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
assert.equal($('#alias').text(), 'foo');
|
||||
});
|
||||
|
||||
it('works for import.meta.glob', async () => {
|
||||
const html = await fixture.readFile('/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
assert.equal($('#glob').text(), '/src/components/glob/a.js');
|
||||
});
|
||||
});
|
||||
});
|
7
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/astro.config.mjs
vendored
Normal file
7
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
import svelte from '@astrojs/svelte';
|
||||
import { defineConfig } from 'astro/config';
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
integrations: [svelte()],
|
||||
});
|
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/deps/namespace-package/index.js
vendored
Normal file
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/deps/namespace-package/index.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
export const namespace = 'namespace';
|
7
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/deps/namespace-package/package.json
vendored
Normal file
7
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/deps/namespace-package/package.json
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"name": "@test/namespace-package-no-baseurl",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": "./index.js"
|
||||
}
|
11
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/package.json
vendored
Normal file
11
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/package.json
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "@test/aliases-tsconfig-no-baseurl",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@astrojs/svelte": "workspace:*",
|
||||
"@test/namespace-package-no-baseurl": "workspace:*",
|
||||
"astro": "workspace:*",
|
||||
"svelte": "^5.2.9"
|
||||
}
|
||||
}
|
4
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Alias.svelte
vendored
Normal file
4
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Alias.svelte
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
<script>
|
||||
import { foo } from 'src/utils/constants';
|
||||
</script>
|
||||
<div id="alias">{foo}</div>
|
2
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Client.svelte
vendored
Normal file
2
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Client.svelte
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
<script></script>
|
||||
<div id="client">test</div>
|
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Foo.astro
vendored
Normal file
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Foo.astro
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
<p id="foo">foo</p>
|
2
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Style.astro
vendored
Normal file
2
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Style.astro
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
<p id="style-blue">i am blue</p>
|
||||
<p id="style-red">i am red</p>
|
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/glob/a.js
vendored
Normal file
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/glob/a.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
export default 'a';
|
32
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/pages/index.astro
vendored
Normal file
32
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,32 @@
|
|||
---
|
||||
import Alias from '@components/Alias.svelte';
|
||||
import Client from '@components/Client.svelte'
|
||||
import '@styles/main.css';
|
||||
import { namespace } from '@test/namespace-package-no-baseurl';
|
||||
import Foo from 'src/components/Foo.astro';
|
||||
import StyleComp from 'src/components/Style.astro';
|
||||
import { foo, index } from 'src/utils/constants';
|
||||
|
||||
const globResult = Object.keys(import.meta.glob('@components/glob/*.js')).join(', ')
|
||||
---
|
||||
<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 />
|
||||
<Alias client:load />
|
||||
<p id="namespace">{namespace}</p>
|
||||
<p id="constants-foo">{foo}</p>
|
||||
<p id="constants-index">{index}</p>
|
||||
<p id="style-red">style-red</p>
|
||||
<p id="style-blue">style-blue</p>
|
||||
<p id="glob">{globResult}</p>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
3
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/styles/extra.css
vendored
Normal file
3
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/styles/extra.css
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
#style-red {
|
||||
color: red;
|
||||
}
|
5
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/styles/main.css
vendored
Normal file
5
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/styles/main.css
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
@import "@styles/extra.css";
|
||||
|
||||
#style-blue {
|
||||
color: blue;
|
||||
}
|
3
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/utils/constants.js
vendored
Normal file
3
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/utils/constants.js
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
export * from '.'
|
||||
|
||||
export const foo = 'foo'
|
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/utils/index.js
vendored
Normal file
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/utils/index.js
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
export const index = 'index'
|
22
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/tsconfig.json
vendored
Normal file
22
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/tsconfig.json
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"paths": {
|
||||
"@components/*": [
|
||||
"./src/components/*"
|
||||
],
|
||||
"@styles/*": [
|
||||
"./src/styles/*"
|
||||
],
|
||||
// this can really trip up namespaced packages
|
||||
"@*": [
|
||||
"./src/*"
|
||||
],
|
||||
// this approximates the functionality you get with a baseUrl set
|
||||
"src/*": [
|
||||
"./src/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"include": [".astro/types.d.ts", "**/*"],
|
||||
"exclude": ["dist"]
|
||||
}
|
|
@ -1801,6 +1801,23 @@ importers:
|
|||
specifier: ^5.2.9
|
||||
version: 5.2.9
|
||||
|
||||
packages/astro/test/fixtures/alias-tsconfig-no-baseurl:
|
||||
dependencies:
|
||||
'@astrojs/svelte':
|
||||
specifier: workspace:*
|
||||
version: link:../../../../integrations/svelte
|
||||
'@test/namespace-package-no-baseurl':
|
||||
specifier: workspace:*
|
||||
version: link:deps/namespace-package
|
||||
astro:
|
||||
specifier: workspace:*
|
||||
version: link:../../..
|
||||
svelte:
|
||||
specifier: ^5.2.9
|
||||
version: 5.2.9
|
||||
|
||||
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/deps/namespace-package: {}
|
||||
|
||||
packages/astro/test/fixtures/alias-tsconfig/deps/namespace-package: {}
|
||||
|
||||
packages/astro/test/fixtures/api-routes:
|
||||
|
|
Loading…
Reference in a new issue