Skip to content

Commit

Permalink
Use accumulated sort order when order production CSS (#5549)
Browse files Browse the repository at this point in the history
* Use accumulated sort order when order production CSS

* Adding a changeset

* Fix lockfile issue
  • Loading branch information
matthewp authored Dec 6, 2022
1 parent 1cfbd59 commit 795f00f
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 2 deletions.
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.

0 comments on commit 795f00f

Please sign in to comment.