-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce consistent import order of CSS (#2065)
Partially fixes #2060
- Loading branch information
Showing
11 changed files
with
134 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Bugfix: improve CSS import order |
This file contains 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 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 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,50 @@ | ||
import { expect } from 'chai'; | ||
import cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('CSS Bundling (ESM import)', () => { | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ projectRoot: './fixtures/astro-css-bundling-import/' }); | ||
await fixture.build(); | ||
}); | ||
|
||
it('CSS output in import order', async () => { | ||
// note: this test is a little confusing, but the main idea is that | ||
// page-2.astro contains all of page-1.astro, plus some unique styles. | ||
// we only test page-2 to ensure the proper order is observed. | ||
const html = await fixture.readFile('/page-2/index.html'); | ||
const $ = cheerio.load(html); | ||
|
||
let css = ''; | ||
|
||
for (const style of $('link[rel=stylesheet]')) { | ||
const href = style.attribs.href.replace(/^\.\./, ''); | ||
if (!href) continue; | ||
css += await fixture.readFile(href); | ||
} | ||
|
||
// test 1: insure green comes after red (site.css) | ||
expect(css.indexOf('p{color:green}')).to.be.greaterThan(css.indexOf('p{color:red}')); | ||
|
||
// test 2: insure green comes after blue (page-1.css) | ||
expect(css.indexOf('p{color:green}')).to.be.greaterThan(css.indexOf('p{color:red}')); | ||
}); | ||
|
||
// TODO: need more investigation to fix this | ||
it.skip('no empty CSS files', async () => { | ||
for (const page of ['/page-1/index.html', '/page-2/index.html']) { | ||
const html = await fixture.readFile(page); | ||
const $ = cheerio.load(html); | ||
|
||
for (const style of $('link[rel=stylesheet]')) { | ||
const href = style.attribs.href.replace(/^\.\./, ''); | ||
if (!href) continue; | ||
const css = await fixture.readFile(href); | ||
|
||
expect(css).to.be.ok; | ||
} | ||
} | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/astro/test/fixtures/astro-css-bundling-import/src/layouts/BaseLayout.astro
This file contains 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,27 @@ | ||
--- | ||
import "../styles/site.css" | ||
const {title} = Astro.props; | ||
--- | ||
|
||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<link rel="icon" type="image/x-icon" href="/favicon.ico" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>{title}</title> | ||
<style> | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<ul> | ||
<li><a href="/page-1">Page 1</a></li> | ||
<li><a href="/page-1">Page 2</a></li> | ||
<!-- <li><a href="/page-2-reduced-layout">Page 2 reduced layout</a></li> --> | ||
</ul> | ||
<slot></slot> | ||
</body> | ||
|
||
</html> |
12 changes: 12 additions & 0 deletions
12
packages/astro/test/fixtures/astro-css-bundling-import/src/layouts/PageLayout.astro
This file contains 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,12 @@ | ||
--- | ||
import BaseLayout from "./BaseLayout.astro" | ||
import "../styles/page-one.css" | ||
const {title} = Astro.props; | ||
--- | ||
|
||
<BaseLayout title={title}> | ||
<main id="page"> | ||
<slot></slot> | ||
</main> | ||
</BaseLayout> |
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/astro-css-bundling-import/src/pages/page-1.astro
This file contains 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,8 @@ | ||
--- | ||
import PageLayout from "../layouts/PageLayout.astro" | ||
--- | ||
|
||
<PageLayout title="Page 1"> | ||
<h1>Page 1</h1> | ||
<p>Nothing to see here. Check <a href="/page-2">Page 2</a></p> | ||
</PageLayout> |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/astro-css-bundling-import/src/pages/page-2.astro
This file contains 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,13 @@ | ||
--- | ||
import PageLayout from "../layouts/PageLayout.astro" | ||
import "../styles/page-two.css" | ||
--- | ||
|
||
<PageLayout title="Page 2"> | ||
<h1>Page 2</h1> | ||
<p>This text should be green, because we want <code>page-2.css</code> to override <code>site.css</code></p> | ||
<p>This works in the dev-server. However in the prod build, the text is blue. Execute <code>npm run build</code> and then execute <code>npx http-server dist/</code>.</p> | ||
<p>We can view the built html at <a href="https://github-qoihup--8080.local.webcontainer.io/page-2/">https://github-qoihup--8080.local.webcontainer.io/page-2/</a>. The color there is blue.</p> | ||
|
||
<p>If it helps debug the issue, rename the <code>page-1.astro</code> file to <code>page-1.astro.bak</code>. Build the prod site and view it. This time the color is green.</p> | ||
</PageLayout> |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/astro-css-bundling-import/src/styles/page-one.css
This file contains 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,3 @@ | ||
p { | ||
color: blue; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/astro-css-bundling-import/src/styles/page-two.css
This file contains 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,3 @@ | ||
p { | ||
color: green; | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/astro/test/fixtures/astro-css-bundling-import/src/styles/site.css
This file contains 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,7 @@ | ||
p { | ||
color: red; | ||
} | ||
|
||
h1 { | ||
outline: 1px solid red; | ||
} |