-
-
Notifications
You must be signed in to change notification settings - Fork 832
Ensure CSS layer order in custom pages #3351
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
Open
HiDeoo
wants to merge
14
commits into
withastro:main
Choose a base branch
from
HiDeoo:hd-feat-vite-plugin-css-layer-order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
676d1e8
chore: add repro
HiDeoo c0ffad1
feat: vite plugin
HiDeoo f8ce61f
test: add layer order test
HiDeoo 9acbc3e
refactor: move import → prepend import
HiDeoo 5e1fa90
Merge branch 'main' into hd-feat-vite-plugin-css-layer-order
HiDeoo 79abdfe
fix: linting errors
HiDeoo f19abd2
refactor: update variable name
HiDeoo 7075b0b
test: add vite plugin to unit tests
HiDeoo 3a8e228
chore: remove demo content
HiDeoo 2919e3a
docs: revert #3199
HiDeoo 90facca
chore: add changeset
HiDeoo caf1d83
chore: changeset component syntax
HiDeoo 5853e33
style: separate imports
HiDeoo 2c358be
test: refactor layer order test
HiDeoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,9 @@ | ||
| --- | ||
| '@astrojs/starlight': minor | ||
| --- | ||
|
|
||
| Ensures that Starlight CSS layer order is predictable in custom pages using the `<StarlightPage>` component. | ||
|
|
||
| Previously, due to how [import order](https://docs.astro.build/en/guides/styling/#import-order) works in Astro, the `<StarlightPage>` component had to be the first import in custom pages to set up [cascade layers](https://starlight.astro.build/guides/css-and-tailwind/#cascade-layers) used internally by Starlight to manage the order of its styles. | ||
|
|
||
| With this change, this restriction no longer applies and Starlight’s styles will be applied correctly regardless of the import order of the `<StarlightPage>` component. | ||
This file contains hidden or 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 hidden or 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
23 changes: 23 additions & 0 deletions
23
packages/starlight/__e2e__/fixtures/basics/src/pages/starlight-page-css-layer-order.astro
This file contains hidden or 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,23 @@ | ||
| --- | ||
| import { LinkButton } from '@astrojs/starlight/components'; | ||
|
|
||
| import AnchorHeading from '@astrojs/starlight/components/AnchorHeading.astro'; | ||
|
|
||
| import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro'; | ||
|
|
||
| /** | ||
| * This page is used to test the CSS layer order in custom pages using the StarlightPage> component | ||
| * where we cannot ensure a correct import order of CSS. | ||
| * Note that the order of imports in this file is important and should not be changed. | ||
| */ | ||
| --- | ||
|
|
||
| <StarlightPage frontmatter={{ title: 'A custom page' }}> | ||
| <AnchorHeading level="2" id="a-sub-heading">A Sub heading</AnchorHeading> | ||
|
|
||
| <p>Custom page content and a <a href="/tabs">link</a> to another page.</p> | ||
|
|
||
| <p> | ||
| <LinkButton href="/tabs">Tabs link button</LinkButton> | ||
| </p> | ||
| </StarlightPage> |
This file contains hidden or 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 hidden or 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 hidden or 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,66 @@ | ||
| import type { ViteUserConfig } from 'astro'; | ||
| import MagicString from 'magic-string'; | ||
|
|
||
| const starlightPageImportSource = '@astrojs/starlight/components/StarlightPage.astro'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m thinking to myself this will break if anyone ever aliases this, but never mind 😁 |
||
|
|
||
| /** | ||
| * Vite plugin that ensures the StarlightPage component is always imported first when imported in | ||
| * an Astro file. | ||
| * | ||
| * This is necessary to ensure a predictable CSS layer order which is defined by the `<Page />` | ||
| * imported by the `<StarlightPage />` component. If a user imports any other component using | ||
| * cascade layers before the `<StarlightPage />` component, it will result in undesired layers | ||
| * being created before we explicitly set the expected layer order. | ||
| */ | ||
| export function vitePluginStarlightCssLayerOrder(): VitePlugin { | ||
| return { | ||
| name: 'vite-plugin-starlight-css-layer-order', | ||
| enforce: 'pre', | ||
| transform(code, id) { | ||
| if ( | ||
| !id.endsWith('.astro') || | ||
| id.endsWith(starlightPageImportSource) || | ||
| code.indexOf('StarlightPage.astro') === -1 | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| let ast: ReturnType<typeof this.parse>; | ||
|
|
||
| try { | ||
| ast = this.parse(code); | ||
| } catch { | ||
| return; | ||
| } | ||
|
|
||
| let hasStarlightPageImport = false; | ||
|
|
||
| for (const node of ast.body) { | ||
| if (node.type !== 'ImportDeclaration') continue; | ||
| if (node.source.value !== starlightPageImportSource) continue; | ||
|
|
||
| const importDefaultSpecifier = node.specifiers.find( | ||
| (specifier) => specifier.type === 'ImportDefaultSpecifier' | ||
| ); | ||
| if (!importDefaultSpecifier) continue; | ||
|
|
||
| hasStarlightPageImport = true; | ||
| break; | ||
| } | ||
|
|
||
| if (!hasStarlightPageImport) return; | ||
|
|
||
| // Format path to unix style path. | ||
| const filename = id.replace(/\\/g, '/'); | ||
| const ms = new MagicString(code, { filename }); | ||
| ms.prepend(`import "${starlightPageImportSource}";\n`); | ||
|
|
||
| return { | ||
| code: ms.toString(), | ||
| map: ms.generateMap({ hires: 'boundary' }), | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| type VitePlugin = NonNullable<ViteUserConfig['plugins']>[number]; | ||
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.