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 accumulated sort order when order production CSS #5549

Merged
merged 3 commits into from
Dec 6, 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/good-terms-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Use accumulated sort order when order production CSS
7 changes: 5 additions & 2 deletions packages/astro/src/core/build/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ export function* walkParentInfos(
id: string,
ctx: { getModuleInfo: GetModuleInfo },
depth = 0,
order = 0,
seen = new Set<string>(),
childId = ''
): Generator<[ModuleInfo, number, number], void, unknown> {
seen.add(id);
const info = ctx.getModuleInfo(id);
if (info) {
let order = childId ? info.importedIds.indexOf(childId) : 0;
if(childId) {
order += info.importedIds.indexOf(childId)
}
yield [info, depth, order];
}
const importers = (info?.importers || []).concat(info?.dynamicImporters || []);
for (const imp of importers) {
if (seen.has(imp)) {
continue;
}
yield* walkParentInfos(imp, ctx, ++depth, seen, id);
yield* walkParentInfos(imp, ctx, ++depth, order, seen, id);
}
}

Expand Down
63 changes: 63 additions & 0 deletions packages/astro/test/css-order-layout.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('CSS ordering - import order with layouts', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/css-order-layout/',
});
});

/**
*
* @param {string} html
* @returns {string[]}
*/
function getLinks(html) {
let $ = cheerio.load(html);
let out = [];
$('link[rel=stylesheet]').each((i, el) => {
out.push($(el).attr('href'));
});
return out;
}

/**
*
* @param {string} href
* @returns {Promise<{ href: string; css: string; }>}
*/
async function getLinkContent(href) {
const css = await fixture.readFile(href);
return { href, css };
}

describe('Production', () => {
before(async () => {
await fixture.build();
});

it('Page level CSS is defined lower in the page', async () => {
let html = await fixture.readFile('/index.html');


const content = await Promise.all(getLinks(html).map((href) => getLinkContent(href)));

let specialButtonCSS = -1;
let globalCSS = -1;
for(let i = 0; i < content.length; i++) {
if(content[i].css.indexOf('.SpecialButton') !== -1) {
specialButtonCSS = i;
} else if(content[i].css.indexOf('green') !== -1) {
globalCSS = i;
}
}

expect(globalCSS).to.equal(0, 'global css sorted on top')
expect(specialButtonCSS).to.equal(1, 'component level css sorted last');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from "astro/config";

export default defineConfig({});
6 changes: 6 additions & 0 deletions packages/astro/test/fixtures/css-order-layout/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@test/css-order-layout",
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
import Button from "./Button.astro";
/*
TODO:
- Improve keyboard navigation / focus handling ref: https://w3c.github.io/aria-practices/examples/disclosure/disclosure-navigation.html
*/
---

<style>
.SpecialButton {
color: blue;
}
</style>

<div>This button should be blue</div>
<Button class="SpecialButton" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
type Props = {
class?: string;
};
const { class: className } = Astro.props as Props;
---

<button class:list={["btn", className]} type="button"> button</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
import "../styles/global.css";
---

<meta charset="UTF-8" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
import MainHead from "../components/MainHead.astro";
import BlueButton from "../components/BlueButton.astro";
---

<html lang="en">
<head>
<MainHead />
</head>
<body>
<BlueButton />
<slot />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
import MainHead from "../components/MainHead.astro";
import BlueButton from "../components/BlueButton.astro";
---

<html lang="en">
<head>
<MainHead />
</head>
<body>
<BlueButton />
<slot />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
import MainHead from "../components/MainHead.astro";
---

<html lang="en">
<head>
<MainHead />
</head>
</html>
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/css-order-layout/src/pages/beta.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import SecondLayout from "../layouts/Second.astro";
---

<SecondLayout>

<div>
Subpage (<a href='/'>Home</a>)
</div>

</SecondLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import MainLayout from "../layouts/Main.astro";
---

<MainLayout>

<div>
Home (<a href='/beta'>Subpage</a>)
</div>

</MainLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.btn {
color: 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.