Take dynamic imports into account for CSS ordering (#6176)

* Take dynamic imports into account for CSS ordering

* Adding a changeset

* updated lockfile
This commit is contained in:
Matthew Phillips 2023-02-08 10:15:36 -05:00 committed by GitHub
parent 334da38c56
commit 8bbdcf17dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 101 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Take dynamic import into account in CSS ordering

View file

@ -16,8 +16,17 @@ export function* walkParentInfos(
const info = ctx.getModuleInfo(id); const info = ctx.getModuleInfo(id);
if (info) { if (info) {
if (childId) { if (childId) {
order += info.importedIds.indexOf(childId); const idx = info.importedIds.indexOf(childId);
if(idx === -1) {
// Dynamic imports come after all normal imports. So first add the number of normal imports.
order += info.importedIds.length;
// Then add on the dynamic ones.
order += info.dynamicallyImportedIds.indexOf(childId);
} else {
order += idx;
} }
}
yield [info, depth, order]; yield [info, depth, order];
} }
if (until?.(id)) return; if (until?.(id)) return;

View file

@ -39,8 +39,8 @@ describe('CSS ordering - import order', () => {
* @param {string} href * @param {string} href
* @returns {Promise<{ href: string; css: string; }>} * @returns {Promise<{ href: string; css: string; }>}
*/ */
async function getLinkContent(href) { async function getLinkContent(href, f = fixture) {
const css = await fixture.readFile(href); const css = await f.readFile(href);
return { href, css }; return { href, css };
} }
@ -107,4 +107,23 @@ describe('CSS ordering - import order', () => {
expect(idx2).to.be.greaterThan(idx3); expect(idx2).to.be.greaterThan(idx3);
}); });
}); });
describe('Dynamic import', () => {
// eslint-disable-next-line @typescript-eslint/no-shadow
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/css-order-dynamic-import/',
});
await fixture.build();
});
it('dynamic imports taken into account', async () => {
let html = await fixture.readFile('/one/index.html');
const content = await Promise.all(getLinks(html).map((href) => getLinkContent(href, fixture)));
let [link1, link2] = content;
expect(link1.css).to.contain('aliceblue');
expect(link2.css).to.contain('yellow');
});
});
}); });

View file

@ -0,0 +1,6 @@
{
"name": "@test/css-order-import",
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,4 @@
---
import '../styles/One.css';
---
<link>

View file

@ -0,0 +1,6 @@
---
---
<style>
body { background: yellow;}
</style>
<div>testing</div>

View file

@ -0,0 +1,5 @@
<style>
body {
background: aliceblue;
}
</style>

View file

@ -0,0 +1,13 @@
---
import '../components/One.astro';
import '../components/Two.astro';
await import('../components/Three.astro');
---
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Test</h1>
</body>
</html>

View file

@ -0,0 +1,19 @@
---
import One from '../components/One.astro';
import Two from '../components/Two.astro';
---
<html>
<head>
<title>Test</title>
<One />
<Two />
<style>
body {
background: whitesmoke;
}
</style>
</head>
<body>
<h1>Test</h1>
</body>
</html>

View file

@ -0,0 +1,3 @@
body {
background: burlywood;
}

View file

@ -0,0 +1,3 @@
body {
background: green;
}

View file

@ -1708,6 +1708,12 @@ importers:
dependencies: dependencies:
astro: link:../../.. astro: link:../../..
packages/astro/test/fixtures/css-order-dynamic-import:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/css-order-import: packages/astro/test/fixtures/css-order-import:
specifiers: specifiers:
astro: workspace:* astro: workspace:*