-
-
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.
Fix CSS chunking between multiple framework components (#6582)
* Fix CSS chunking between multiple framework components * Better CSS dedupe handling * Add more tests * Update docs
- Loading branch information
Showing
16 changed files
with
115 additions
and
16 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 | ||
--- | ||
|
||
Fix CSS chunking and deduping between multiple Astro files and framework components |
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 |
---|---|---|
|
@@ -55,7 +55,20 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] | |
{ | ||
name: 'astro:rollup-plugin-build-css', | ||
|
||
transform(_, id) { | ||
// In the SSR build, styles that are bundled are tracked in `internals.cssChunkModuleIds`. | ||
// In the client build, if we're also bundling the same style, return an empty string to | ||
// deduplicate the final CSS output. | ||
if (options.target === 'client' && internals.cssChunkModuleIds.has(id)) { | ||
return ''; | ||
} | ||
}, | ||
|
||
outputOptions(outputOptions) { | ||
// Skip in client builds as its module graph doesn't have reference to Astro pages | ||
// to be able to chunk based on it's related top-level pages. | ||
if (options.target === 'client') return; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bluwy
Author
Member
|
||
|
||
const assetFileNames = outputOptions.assetFileNames; | ||
const namingIncludesHash = assetFileNames?.toString().includes('[hash]'); | ||
const createNameForParentPages = namingIncludesHash | ||
|
@@ -133,17 +146,6 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] | |
internals.cssChunkModuleIds.add(id); | ||
} | ||
} | ||
// In the client build, we bail if the chunk is a duplicated CSS chunk tracked from | ||
// above. We remove all the importedCss to prevent emitting the CSS asset. | ||
if (options.target === 'client') { | ||
if (Object.keys(c.modules).every((id) => internals.cssChunkModuleIds.has(id))) { | ||
for (const importedCssImport of meta.importedCss) { | ||
delete bundle[importedCssImport]; | ||
meta.importedCss.delete(importedCssImport); | ||
} | ||
return; | ||
} | ||
} | ||
|
||
// For the client build, client:only styles need to be mapped | ||
// over to their page. For this chunk, determine if it's a child of a | ||
|
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
7 changes: 7 additions & 0 deletions
7
packages/astro/test/fixtures/css-order-import/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 { defineConfig } from 'astro/config'; | ||
import react from '@astrojs/react' | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
integrations: [react()] | ||
}); |
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 |
---|---|---|
@@ -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" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/css-order-import/src/components/Client1.jsx
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/Client1.css" | ||
|
||
export default function Client() { | ||
return <div className="client-1">Client 1</div>; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/css-order-import/src/components/Client2.jsx
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/Client2.css" | ||
|
||
export default function Client() { | ||
return <div className="client-2">Client 2</div>; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/css-order-import/src/components/Dedupe.jsx
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/AstroJsx.css'; | ||
|
||
export default function Dedupe() { | ||
return <div className="astro-jsx">Dedupe Astro JSX</div>; | ||
} |
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
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/css-order-import/src/pages/dedupe.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,13 @@ | ||
--- | ||
import '../styles/AstroJsx.css'; | ||
import Dedupe from '../components/Dedupe.jsx'; | ||
--- | ||
<html> | ||
<head> | ||
<title>Test</title> | ||
</head> | ||
<body> | ||
<h1>Test</h1> | ||
<Dedupe client:only="react" /> | ||
</body> | ||
</html> |
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
4 changes: 4 additions & 0 deletions
4
packages/astro/test/fixtures/css-order-import/src/styles/AstroJsx.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,4 @@ | ||
/* this css is imported in both .astro and .jsx component, make sure CSS is deduped */ | ||
.astro-jsx { | ||
color: red; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/css-order-import/src/styles/Client1.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 @@ | ||
.client-1 { | ||
background: green; | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/astro/test/fixtures/css-order-import/src/styles/Client2.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,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; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
@bluwy
This ln is breaking Vue component styles
When client:only="vue" the styles for the child components will fail to be included, but not for all, still trying to figure out the exact conditions.