Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Vite's resolve to resolve paths for client:only #5434

Merged
merged 7 commits into from
Nov 18, 2022
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/pink-cheetahs-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Use Vite's resolve to resolve paths for client:only
11 changes: 4 additions & 7 deletions packages/astro/src/core/build/vite-plugin-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,10 @@ export function vitePluginAnalyzer(internals: BuildInternals): VitePlugin {
const cid = c.resolvedPath ? decodeURI(c.resolvedPath) : c.specifier;
internals.discoveredClientOnlyComponents.add(cid);
clientOnlys.push(cid);
// Bare module specifiers need to be resolved so that the CSS
// plugin can walk up the graph to find which page they belong to.
if (c.resolvedPath === c.specifier) {
const resolvedId = await this.resolve(c.specifier, id);
if (resolvedId) {
clientOnlys.push(resolvedId.id);
}

const resolvedId = await this.resolve(c.specifier, id);
if (resolvedId) {
clientOnlys.push(resolvedId.id);
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/astro-client-only.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,14 @@ describe('Client only components subpath', () => {
expect(css).to.match(/yellowgreen/, 'Svelte styles are added');
expect(css).to.match(/Courier New/, 'Global styles are added');
});

it('Adds the CSS to the page for TSX components', async () => {
const html = await fixture.readFile('/tsx-no-extension/index.html');
const $ = cheerioLoad(html);

const href = $('link[rel=stylesheet]').attr('href');
const css = await fixture.readFile(href.replace(/\/blog/, ''));

expect(css).to.match(/purple/, 'Global styles from tsx component are added');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import './global2.css';

export default function() {
return <div>i am react</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: purple;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
import ReactTSXComponent from '../components/TSXComponent';
---
<html>
<head><title>Client only pages</title></head>
<body>
<ReactTSXComponent client:only="react" />
</body>
</html>