-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
282 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 7 additions & 0 deletions
7
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import svelte from '@astrojs/svelte'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [svelte()], | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/deps/namespace-package/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const namespace = 'namespace'; |
7 changes: 7 additions & 0 deletions
7
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/deps/namespace-package/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "@test/namespace-package-no-baseurl", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"exports": "./index.js" | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 4 additions & 0 deletions
4
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Alias.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<script> | ||
import { foo } from 'src/utils/constants'; | ||
</script> | ||
<div id="alias">{foo}</div> |
2 changes: 2 additions & 0 deletions
2
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Client.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<script></script> | ||
<div id="client">test</div> |
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Foo.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p id="foo">foo</p> |
2 changes: 2 additions & 0 deletions
2
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/Style.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<p id="style-blue">i am blue</p> | ||
<p id="style-red">i am red</p> |
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/components/glob/a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'a'; |
32 changes: 32 additions & 0 deletions
32
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/styles/extra.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#style-red { | ||
color: red; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/styles/main.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@import "@styles/extra.css"; | ||
|
||
#style-blue { | ||
color: blue; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/utils/constants.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from '.' | ||
|
||
export const foo = 'foo' |
1 change: 1 addition & 0 deletions
1
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/src/utils/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const index = 'index' |
22 changes: 22 additions & 0 deletions
22
packages/astro/test/fixtures/alias-tsconfig-no-baseurl/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.