Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML minification #6706

Merged
merged 40 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c440edf
TDD pattern development
Mar 30, 2023
c85a4b0
add compact property when the user run pnpm run build
Mar 31, 2023
cbc2b2f
Merge branch 'main' of github.com:withastro/astro into add-Minification
Apr 18, 2023
644dae1
add minification for pro
Apr 18, 2023
1cbc00b
Merge branch 'main' into add-Minification
JerryWu1234 Apr 20, 2023
e843712
fix yaml file collision
Apr 20, 2023
eba4d48
fix yaml collision
Apr 20, 2023
2900aa0
Merge branch 'main' into add-Minification
JerryWu1234 Apr 21, 2023
0c58f5b
Merge branch 'main' into add-Minification
JerryWu1234 Apr 25, 2023
96ae817
fix pageage file
Apr 26, 2023
9236c62
Merge branch 'main' into add-Minification
JerryWu1234 Apr 26, 2023
b871d91
optimize unit test
Apr 26, 2023
35aade7
merge
Apr 26, 2023
50a9ce6
fix revert code
Apr 26, 2023
c78a982
Merge branch 'main' into add-Minification
JerryWu1234 Apr 26, 2023
5e1970b
Merge branch 'main' into add-Minification
JerryWu1234 Apr 28, 2023
a5369ae
merge
May 4, 2023
f6b14fb
fix comment
May 4, 2023
6a5f37a
Merge branch 'main' into add-Minification
JerryWu1234 May 5, 2023
c82e67e
update yaml
May 5, 2023
981426d
merge
May 5, 2023
283cd3e
fix default value
May 5, 2023
49b39a5
add test for dev
May 5, 2023
450e536
Merge branch 'main' into add-Minification
matthewp May 8, 2023
9df321c
Update packages/astro/test/astro-minification-html.test.js
matthewp May 9, 2023
b4b72e5
Update packages/astro/test/astro-minification-html.test.js
matthewp May 9, 2023
d39a84f
Update packages/astro/test/astro-minification-html.test.js
matthewp May 9, 2023
f166e4b
Update packages/astro/test/astro-minification-html.test.js
matthewp May 9, 2023
3805e58
Update packages/astro/test/astro-minification-html.test.js
matthewp May 9, 2023
6903c46
Update the docs to reflect it's opt-in
matthewp May 9, 2023
5b76ca7
Add tests for SSR
matthewp May 9, 2023
c27e6fc
Merge branch 'main' into add-Minification
JerryWu1234 May 10, 2023
aa9c6d9
Document how the tests remove the doctype line
matthewp May 10, 2023
676ce17
Expand on the changeset
matthewp May 10, 2023
9a3ed89
rename for slice -100
May 11, 2023
8bb5a1b
Updates based on PR comments
matthewp May 11, 2023
379e925
optimize description
May 15, 2023
7c2a94d
Merge branch 'main' into add-Minification
JerryWu1234 May 15, 2023
d3a066b
Update packages/astro/src/@types/astro.ts
matthewp May 15, 2023
756b68e
Merge branch 'main' into add-Minification
JerryWu1234 May 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-snails-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

minification the HTML
18 changes: 18 additions & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,24 @@ export interface AstroUserConfig {
*/
site?: string;


/**
* @docs
* @name compressHTML
* @type {boolean}
* @default `true`
matthewp marked this conversation as resolved.
Show resolved Hide resolved
* @description
* Astro removes all whitespace from your final HTML file, including line breaks, by default. This is useful for reducing the size of your final build's HTML bundle.
* To disable this, set the `compressHTML` flag to `false`.
matthewp marked this conversation as resolved.
Show resolved Hide resolved
*
* ```js
* {
* compressHTML: false
* }
* ```
*/
compressHTML?: boolean;

/**
* @docs
* @name base
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function compile({
// use `sourcemap: "both"` so that sourcemap is included in the code
// result passed to esbuild, but also available in the catch handler.
transformResult = await transform(source, {
compact: astroConfig.compressHTML,
filename,
normalizedFilename: normalizeFilename(filename, astroConfig.root),
sourcemap: 'both',
Expand Down
6 changes: 6 additions & 0 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
assets: '_astro',
serverEntry: 'entry.mjs',
},
compressHTML: false,
server: {
host: false,
port: 3000,
Expand Down Expand Up @@ -70,6 +71,7 @@ export const AstroConfigSchema = z.object({
.default(ASTRO_CONFIG_DEFAULTS.cacheDir)
.transform((val) => new URL(val)),
site: z.string().url().optional(),
compressHTML: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.compressHTML),
base: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.base),
trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')])
Expand Down Expand Up @@ -218,6 +220,10 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
.string()
.default(ASTRO_CONFIG_DEFAULTS.srcDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
compressHTML: z
.boolean()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.compressHTML),
publicDir: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.publicDir)
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/runtime/server/render/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export async function renderPage(
result._metadata.headInTree =
result.componentMetadata.get((componentFactory as any).moduleId)?.containsHead ?? false;
const pageProps: Record<string, any> = { ...(props ?? {}), 'server:root': true };

let output: ComponentIterable;
let head = '';
try {
Expand Down
42 changes: 42 additions & 0 deletions packages/astro/test/astro-minification-html.test.js
matthewp marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture, isWindows } from './test-utils.js';

describe('minification html', () => {
matthewp marked this conversation as resolved.
Show resolved Hide resolved
describe('in the dev', () => {
matthewp marked this conversation as resolved.
Show resolved Hide resolved
let fixture;
let devServer;
const regex = /[\r\n]+/gm;
before(async () => {
fixture = await loadFixture({
root: './fixtures/minification-html/',
});
devServer = await fixture.startDevServer();
});

after(async () => {
devServer.stop();
});

it('Verify that the HTML code is compressed in the dev', async () => {
matthewp marked this conversation as resolved.
Show resolved Hide resolved
let res = await fixture.fetch(`/`);
expect(res.status).to.equal(200);
const html = await res.text();
expect(regex.test(html.slice(-100))).to.equal(false);
});

})
describe('build', () => {
matthewp marked this conversation as resolved.
Show resolved Hide resolved
let fixture;
const regex = /[\r\n]+/gm;
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
before(async () => {
fixture = await loadFixture({ root: './fixtures/minification-html/' });
await fixture.build();
});

it('Verify that the HTML code is compressed in the pro', async () => {
matthewp marked this conversation as resolved.
Show resolved Hide resolved
const html = await fixture.readFile('/index.html');
expect(regex.test(html.slice(20))).to.equal(false);
matthewp marked this conversation as resolved.
Show resolved Hide resolved
});
})
});
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,

});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/minification-html/package.json
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:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
---
<div>2</div>
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
---
<div>3</div>
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.