-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* TDD pattern development * add compact property when the user run pnpm run build * add minification for pro * fix yaml file collision * fix yaml collision * fix pageage file * optimize unit test * fix revert code * fix comment * update yaml * fix default value * add test for dev * Update packages/astro/test/astro-minification-html.test.js Co-authored-by: Emanuele Stoppa <[email protected]> * Update packages/astro/test/astro-minification-html.test.js Co-authored-by: Emanuele Stoppa <[email protected]> * Update packages/astro/test/astro-minification-html.test.js Co-authored-by: Emanuele Stoppa <[email protected]> * Update packages/astro/test/astro-minification-html.test.js Co-authored-by: Emanuele Stoppa <[email protected]> * Update packages/astro/test/astro-minification-html.test.js Co-authored-by: Emanuele Stoppa <[email protected]> * Update the docs to reflect it's opt-in * Add tests for SSR * Document how the tests remove the doctype line * Expand on the changeset * rename for slice -100 * Updates based on PR comments * optimize description * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <[email protected]> --------- Co-authored-by: wuls <[email protected]> Co-authored-by: Matthew Phillips <[email protected]> Co-authored-by: Matthew Phillips <[email protected]> Co-authored-by: Emanuele Stoppa <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
5c3c672
commit 763ff2d
Showing
12 changed files
with
169 additions
and
1 deletion.
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,17 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Adds an opt-in way to minify the HTML output. | ||
|
||
Using the `compressHTML` option Astro will remove whitespace from Astro components. This only applies to components written in `.astro` format and happens in the compiler to maximize performance. You can enable with: | ||
|
||
```js | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
compressHTML: true | ||
}); | ||
``` | ||
|
||
Compression occurs both in development mode and in the final build. |
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
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
7 changes: 7 additions & 0 deletions
7
packages/astro/test/fixtures/minification-html/astro.config.mjs
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 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
compressHTML: true, | ||
|
||
}); |
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 @@ | ||
{ | ||
"name": "@test/minification-html", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/minification-html/src/pages/aside.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,3 @@ | ||
--- | ||
--- | ||
<div>2</div> |
21 changes: 21 additions & 0 deletions
21
packages/astro/test/fixtures/minification-html/src/pages/index.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,21 @@ | ||
--- | ||
import Aside from './aside.astro' | ||
import Page from './page.astro' | ||
--- | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<title>minimum html</title> | ||
<style> | ||
.body{ | ||
background: red; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<Aside/> | ||
<main></main> | ||
<Page></Page> | ||
</body> | ||
</html> |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/minification-html/src/pages/page.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,3 @@ | ||
--- | ||
--- | ||
<div>3</div> |
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,79 @@ | ||
import { expect } from 'chai'; | ||
import { loadFixture } from './test-utils.js'; | ||
import testAdapter from './test-adapter.js'; | ||
|
||
const NEW_LINES = /[\r\n]+/gm; | ||
|
||
/** | ||
* The doctype declaration is on a line between the rest of the HTML. | ||
* This function removes the doctype so that we can check if the rest of the HTML is without | ||
* whitespace. | ||
*/ | ||
function removeDoctypeLine(html) { | ||
return html.slice(20); | ||
} | ||
|
||
/** | ||
* In the dev environment, two more script tags will be injected than in the production environment | ||
* so that we can check if the rest of the HTML is without whitespace | ||
*/ | ||
function removeDoctypeLineInDev(html){ | ||
return html.slice(-100) | ||
} | ||
|
||
describe('HTML minification', () => { | ||
describe('in DEV enviroment', () => { | ||
let fixture; | ||
let devServer; | ||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/minification-html/', | ||
}); | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
devServer.stop(); | ||
}); | ||
|
||
it('should emit compressed HTML in the emitted file', async () => { | ||
let res = await fixture.fetch(`/`); | ||
expect(res.status).to.equal(200); | ||
const html = await res.text(); | ||
expect(NEW_LINES.test(removeDoctypeLineInDev(html))).to.equal(false); | ||
}); | ||
}); | ||
|
||
describe('Build SSG', () => { | ||
let fixture; | ||
before(async () => { | ||
fixture = await loadFixture({ root: './fixtures/minification-html/' }); | ||
await fixture.build(); | ||
}); | ||
|
||
it('should emit compressed HTML in the emitted file', async () => { | ||
const html = await fixture.readFile('/index.html'); | ||
expect(NEW_LINES.test(removeDoctypeLine(html))).to.equal(false); | ||
}); | ||
}); | ||
|
||
describe('Build SSR', () => { | ||
let fixture; | ||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/minification-html/', | ||
output: 'server', | ||
adapter: testAdapter() | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('should emit compressed HTML in the emitted file', async () => { | ||
const app = await fixture.loadTestAdapterApp(); | ||
const request = new Request('http://example.com/'); | ||
const response = await app.render(request); | ||
const html = await response.text(); | ||
expect(NEW_LINES.test(removeDoctypeLine(html))).to.equal(false); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.