Compare commits

...

24 commits

Author SHA1 Message Date
Matthew Phillips
62568aa397 Merge branch 'main' into add-minification2 2023-05-08 13:21:44 -04:00
wuls
49b39a5c52 add test for dev 2023-05-05 10:33:57 +08:00
wuls
283cd3eb26 fix default value 2023-05-05 09:41:32 +08:00
wuls
981426dc07 merge 2023-05-05 09:21:45 +08:00
wuls
c82e67eeee update yaml 2023-05-05 09:20:50 +08:00
wulinsheng123
6a5f37a57e
Merge branch 'main' into add-Minification 2023-05-05 09:18:57 +08:00
wuls
f6b14fb216 fix comment 2023-05-04 18:57:19 +08:00
wuls
a5369ae504 merge 2023-05-04 18:54:18 +08:00
wulinsheng123
5e1970bf29
Merge branch 'main' into add-Minification 2023-04-29 07:30:14 +08:00
wulinsheng123
c78a9824d8
Merge branch 'main' into add-Minification 2023-04-26 21:04:13 +08:00
wuls
50a9ce6e0f fix revert code 2023-04-26 21:03:36 +08:00
wuls
35aade74f8 merge 2023-04-26 21:02:12 +08:00
wuls
b871d91384 optimize unit test 2023-04-26 21:01:03 +08:00
wulinsheng123
9236c62994
Merge branch 'main' into add-Minification 2023-04-26 16:38:51 +08:00
wuls
96ae817982 fix pageage file 2023-04-26 16:38:03 +08:00
wulinsheng123
0c58f5b1e2
Merge branch 'main' into add-Minification 2023-04-25 10:12:17 +08:00
wulinsheng123
2900aa03e4
Merge branch 'main' into add-Minification 2023-04-21 23:18:34 +08:00
wuls
eba4d488ba fix yaml collision 2023-04-20 10:12:26 +08:00
wuls
e8437125cb fix yaml file collision 2023-04-20 10:11:01 +08:00
wulinsheng123
1cbc00bfca
Merge branch 'main' into add-Minification 2023-04-20 09:38:46 +08:00
wuls
644dae1ee9 add minification for pro 2023-04-18 18:37:38 +08:00
wuls
cbc2b2fd55 Merge branch 'main' of github.com:withastro/astro into add-Minification
# Conflicts:
#	pnpm-lock.yaml
2023-04-18 18:25:07 +08:00
wuls
c85a4b0e60 add compact property when the user run pnpm run build 2023-03-31 09:49:27 +08:00
wuls
c440edf07f TDD pattern development 2023-03-30 10:44:28 +08:00
12 changed files with 120 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
minification the HTML

View file

@ -462,6 +462,24 @@ export interface AstroUserConfig {
*/
site?: string;
/**
* @docs
* @name compressHTML
* @type {boolean}
* @default `true`
* @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`.
*
* ```js
* {
* compressHTML: false
* }
* ```
*/
compressHTML?: boolean;
/**
* @docs
* @name base

View file

@ -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',

View file

@ -23,6 +23,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
assets: '_astro',
serverEntry: 'entry.mjs',
},
compressHTML: false,
server: {
host: false,
port: 3000,
@ -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')])
@ -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)

View file

@ -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 {

View file

@ -0,0 +1,42 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture, isWindows } from './test-utils.js';
describe('minification html', () => {
describe('in the dev', () => {
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 () => {
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', () => {
let fixture;
const regex = /[\r\n]+/gm;
before(async () => {
fixture = await loadFixture({ root: './fixtures/minification-html/' });
await fixture.build();
});
it('Verify that the HTML code is compressed in the pro', async () => {
const html = await fixture.readFile('/index.html');
expect(regex.test(html.slice(20))).to.equal(false);
});
})
});

View file

@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
// https://astro.build/config
export default defineConfig({
compressHTML: true,
});

View file

@ -0,0 +1,8 @@
{
"name": "@test/minification-html",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,3 @@
---
---
<div>2</div>

View file

@ -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>

View file

@ -0,0 +1,3 @@
---
---
<div>3</div>

View file

@ -2780,6 +2780,12 @@ importers:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/minification-html:
dependencies:
astro:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/multiple-renderers:
dependencies:
'@test/astro-renderer-one':