Skip to content

Commit

Permalink
Fix CSS chunking between multiple framework components
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Mar 17, 2023
1 parent f5fddaf commit 1f749c9
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/violet-islands-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix CSS chunking between multiple framework components
2 changes: 2 additions & 0 deletions packages/astro/src/core/build/plugins/plugin-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[]
name: 'astro:rollup-plugin-build-css',

outputOptions(outputOptions) {
if (options.target === 'client') return

const assetFileNames = outputOptions.assetFileNames;
const namingIncludesHash = assetFileNames?.toString().includes('[hash]');
const createNameForParentPages = namingIncludesHash
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/css-order-import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ describe('CSS ordering - import order', () => {
expect(idx1).to.be.greaterThan(idx2);
expect(idx2).to.be.greaterThan(idx3);
});

it('correctly chunks css import from framework components', async () => {
let html = await fixture.readFile('/index.html');

const content = await Promise.all(getLinks(html).map((href) => getLinkContent(href)));
const [, { css }] = content;
expect(css).to.not.include(
'.client-1{background:red!important}',
'CSS from Client2.jsx leaked into index.astro when chunking'
);
});
});

describe('Dynamic import', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import react from '@astrojs/react'

// https://astro.build/config
export default defineConfig({
integrations: [react()]
});
5 changes: 4 additions & 1 deletion packages/astro/test/fixtures/css-order-import/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "@test/css-order-import",
"dependencies": {
"astro": "workspace:*"
"@astrojs/react": "workspace:*",
"astro": "workspace:*",
"react": "^18.1.0",
"react-dom": "^18.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "../styles/Client1.css"

export default function Client() {
return <div className="client-1">Client 1</div>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "../styles/Client2.css"

export default function Client() {
return <div className="client-2">Client 2</div>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import One from '../components/One.astro';
import Two from '../components/Two.astro';
import Client2 from '../components/Client2.jsx';
---
<html>
<head>
Expand All @@ -15,5 +16,6 @@ import Two from '../components/Two.astro';
</head>
<body>
<h1>Test</h1>
<Client2 client:only="react" />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import '../styles/base.css';
import Client1 from '../components/Client1.jsx';
---
<html>
<head>
Expand All @@ -12,5 +13,6 @@ import '../styles/base.css';
</head>
<body>
<h1>Test</h1>
<Client1 client:only="react" />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.client-1 {
background: green;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
This cheeky css tries to affect index.astro, the good thing is that the Client2.jsx component is
only loaded in component.astro. So index.astro's Client1.jsx component should not be affected by this.
*/
.client-1 {
background: red !important;
}
.client-2 {
background: green;
}
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.

0 comments on commit 1f749c9

Please sign in to comment.