Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-style-path-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix `<style>` compilation failure when importing Astro components via tsconfig path aliases
7 changes: 7 additions & 0 deletions packages/astro/src/vite-plugin-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ export function getFileInfo(id: string, config: AstroConfig) {
*
* - /@fs/home/user/project/src/pages/index.astro
* - /src/pages/index.astro
* - ./src/pages/index.astro
*
* as absolute file paths with forward slashes.
*/
export function normalizeFilename(filename: string, root: URL) {
if (filename.startsWith('/@fs')) {
filename = filename.slice('/@fs'.length);
} else if (filename.startsWith('.')) {
// Handle relative paths (e.g. ./src/components/Foo.astro) by resolving against root.
// This can occur on certain environments (e.g. Windows + newer Node.js) when a component
// is imported via a TypeScript path alias and Vite produces a relative virtual module ID.
const url = new URL(filename, root);
filename = viteID(url);
} else if (filename.startsWith('/') && !commonAncestorPath(filename, fileURLToPath(root))) {
const url = new URL('.' + filename, root);
filename = viteID(url);
Expand Down
34 changes: 34 additions & 0 deletions packages/astro/test/alias-path-alias-style.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

// Regression test for https://github.com/withastro/astro/issues/15963
// <style> tags in components imported via tsconfig path aliases should compile correctly.
describe('Style compilation with tsconfig path aliases', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/alias-path-alias-style/',
build: { inlineStylesheets: 'never' },
});
await fixture.build();
});

it('builds successfully and includes scoped styles from aliased component', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

// The styled component should be rendered
assert.ok($('.styled').length > 0, 'Styled component should be present in output');

// With inlineStylesheets: 'never', styles are emitted as external CSS files
const links = $('link[rel=stylesheet]').map((_i, el) => $(el).attr('href')).get();
assert.ok(links.length > 0, 'Should have at least one linked stylesheet');

const cssContents = await Promise.all(links.map((href) => fixture.readFile(href)));
const allCss = cssContents.join('\n');
assert.ok(allCss.includes('.styled'), 'Scoped .styled CSS should be present in emitted stylesheet');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'astro/config';

export default defineConfig({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/alias-path-alias-style",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="styled">Styled Component</div>

<style>
.styled {
color: blue;
font-weight: bold;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
import StyledComponent from '@/components/StyledComponent.astro';
---
<html>
<head><title>Path Alias Style Test</title></head>
<body>
<h1>Path Alias Style Test</h1>
<StyledComponent />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
},
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist"]
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading