Fix client:only when used with JSX (#4592)
This commit is contained in:
parent
f018e365cf
commit
8476f2a293
6 changed files with 46 additions and 3 deletions
|
@ -1,7 +1,7 @@
|
|||
import type { OutputChunk, RenderedChunk } from 'rollup';
|
||||
import type { PageBuildData, ViteID } from './types';
|
||||
|
||||
import { prependForwardSlash } from '../path.js';
|
||||
import { prependForwardSlash, removeFileExtension } from '../path.js';
|
||||
import { viteID } from '../util.js';
|
||||
|
||||
export interface BuildInternals {
|
||||
|
@ -136,8 +136,15 @@ export function* getPageDatasByClientOnlyID(
|
|||
): Generator<PageBuildData, void, unknown> {
|
||||
const pagesByClientOnly = internals.pagesByClientOnly;
|
||||
if (pagesByClientOnly.size) {
|
||||
const pathname = `/@fs${prependForwardSlash(viteid)}`;
|
||||
const pageBuildDatas = pagesByClientOnly.get(pathname);
|
||||
let pathname = `/@fs${prependForwardSlash(viteid)}`;
|
||||
let pageBuildDatas = pagesByClientOnly.get(viteid);
|
||||
// BUG! The compiler partially resolves .jsx to remove the file extension so we have to check again.
|
||||
// We should probably get rid of all `@fs` usage and always fully resolve via Vite,
|
||||
// but this would be a bigger change.
|
||||
if(!pageBuildDatas) {
|
||||
pathname = `/@fs${prependForwardSlash(removeFileExtension(viteid))}`
|
||||
pageBuildDatas = pagesByClientOnly.get(pathname);
|
||||
}
|
||||
if (pageBuildDatas) {
|
||||
for (const pageData of pageBuildDatas) {
|
||||
yield pageData;
|
||||
|
|
|
@ -46,3 +46,8 @@ function isString(path: unknown): path is string {
|
|||
export function joinPaths(...paths: (string | undefined)[]) {
|
||||
return paths.filter(isString).map(trimSlashes).join('/');
|
||||
}
|
||||
|
||||
export function removeFileExtension(path: string) {
|
||||
let idx = path.lastIndexOf('.');
|
||||
return idx === -1 ? path : path.slice(0, idx);
|
||||
}
|
||||
|
|
|
@ -33,6 +33,12 @@ describe('Client only components', () => {
|
|||
expect(css).to.match(/yellowgreen/, 'Svelte styles are added');
|
||||
expect(css).to.match(/Courier New/, 'Global styles are added');
|
||||
});
|
||||
|
||||
it('Includes CSS from components that use CSS modules', async () => {
|
||||
const html = await fixture.readFile('/css-modules/index.html');
|
||||
const $ = cheerioLoad(html);
|
||||
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Client only components subpath', () => {
|
||||
|
|
11
packages/astro/test/fixtures/astro-client-only/src/components/UsingCSSModules.jsx
vendored
Normal file
11
packages/astro/test/fixtures/astro-client-only/src/components/UsingCSSModules.jsx
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
import Styles from './styles.module.scss';
|
||||
|
||||
const ClientApp = () => {
|
||||
return (
|
||||
<div>
|
||||
<h2 className={Styles.red}>This text should be red</h2>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ClientApp;
|
3
packages/astro/test/fixtures/astro-client-only/src/components/styles.module.scss
vendored
Normal file
3
packages/astro/test/fixtures/astro-client-only/src/components/styles.module.scss
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
.red {
|
||||
color: red;
|
||||
}
|
11
packages/astro/test/fixtures/astro-client-only/src/pages/css-modules.astro
vendored
Normal file
11
packages/astro/test/fixtures/astro-client-only/src/pages/css-modules.astro
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
import UsingCSSModules from '../components/UsingCSSModules.jsx';
|
||||
---
|
||||
<html>
|
||||
<head>
|
||||
<title>Using CSS modules</title>
|
||||
</head>
|
||||
<body>
|
||||
<UsingCSSModules client:only="react" />
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue