diff --git a/.changeset/happy-dragons-invite.md b/.changeset/happy-dragons-invite.md new file mode 100644 index 000000000..510f18e91 --- /dev/null +++ b/.changeset/happy-dragons-invite.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Take dynamic import into account in CSS ordering diff --git a/packages/astro/src/core/build/graph.ts b/packages/astro/src/core/build/graph.ts index 5edb07a71..5b4b2107c 100644 --- a/packages/astro/src/core/build/graph.ts +++ b/packages/astro/src/core/build/graph.ts @@ -16,8 +16,17 @@ export function* walkParentInfos( const info = ctx.getModuleInfo(id); if (info) { 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]; } if (until?.(id)) return; diff --git a/packages/astro/test/css-order-import.test.js b/packages/astro/test/css-order-import.test.js index 09f54a0e6..b93822a69 100644 --- a/packages/astro/test/css-order-import.test.js +++ b/packages/astro/test/css-order-import.test.js @@ -39,8 +39,8 @@ describe('CSS ordering - import order', () => { * @param {string} href * @returns {Promise<{ href: string; css: string; }>} */ - async function getLinkContent(href) { - const css = await fixture.readFile(href); + async function getLinkContent(href, f = fixture) { + const css = await f.readFile(href); return { href, css }; } @@ -107,4 +107,23 @@ describe('CSS ordering - import order', () => { 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'); + }); + }); }); diff --git a/packages/astro/test/fixtures/css-order-dynamic-import/package.json b/packages/astro/test/fixtures/css-order-dynamic-import/package.json new file mode 100644 index 000000000..2901a838f --- /dev/null +++ b/packages/astro/test/fixtures/css-order-dynamic-import/package.json @@ -0,0 +1,6 @@ +{ + "name": "@test/css-order-import", + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/css-order-dynamic-import/src/components/One.astro b/packages/astro/test/fixtures/css-order-dynamic-import/src/components/One.astro new file mode 100644 index 000000000..54b1039b1 --- /dev/null +++ b/packages/astro/test/fixtures/css-order-dynamic-import/src/components/One.astro @@ -0,0 +1,4 @@ +--- +import '../styles/One.css'; +--- + diff --git a/packages/astro/test/fixtures/css-order-dynamic-import/src/components/Three.astro b/packages/astro/test/fixtures/css-order-dynamic-import/src/components/Three.astro new file mode 100644 index 000000000..8918fdc78 --- /dev/null +++ b/packages/astro/test/fixtures/css-order-dynamic-import/src/components/Three.astro @@ -0,0 +1,6 @@ +--- +--- + +