Compare commits
24 commits
main
...
add-minifi
Author | SHA1 | Date | |
---|---|---|---|
|
62568aa397 | ||
|
49b39a5c52 | ||
|
283cd3eb26 | ||
|
981426dc07 | ||
|
c82e67eeee | ||
|
6a5f37a57e | ||
|
f6b14fb216 | ||
|
a5369ae504 | ||
|
5e1970bf29 | ||
|
c78a9824d8 | ||
|
50a9ce6e0f | ||
|
35aade74f8 | ||
|
b871d91384 | ||
|
9236c62994 | ||
|
96ae817982 | ||
|
0c58f5b1e2 | ||
|
2900aa03e4 | ||
|
eba4d488ba | ||
|
e8437125cb | ||
|
1cbc00bfca | ||
|
644dae1ee9 | ||
|
cbc2b2fd55 | ||
|
c85a4b0e60 | ||
|
c440edf07f |
12 changed files with 120 additions and 1 deletions
5
.changeset/tiny-snails-dance.md
Normal file
5
.changeset/tiny-snails-dance.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': minor
|
||||
---
|
||||
|
||||
minification the HTML
|
|
@ -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
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
42
packages/astro/test/astro-minification-html.test.js
Normal file
42
packages/astro/test/astro-minification-html.test.js
Normal 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);
|
||||
});
|
||||
})
|
||||
});
|
7
packages/astro/test/fixtures/minification-html/astro.config.mjs
vendored
Normal file
7
packages/astro/test/fixtures/minification-html/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { defineConfig } from 'astro/config';
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
compressHTML: true,
|
||||
|
||||
});
|
8
packages/astro/test/fixtures/minification-html/package.json
vendored
Normal file
8
packages/astro/test/fixtures/minification-html/package.json
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "@test/minification-html",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
3
packages/astro/test/fixtures/minification-html/src/pages/aside.astro
vendored
Normal file
3
packages/astro/test/fixtures/minification-html/src/pages/aside.astro
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
---
|
||||
<div>2</div>
|
21
packages/astro/test/fixtures/minification-html/src/pages/index.astro
vendored
Normal file
21
packages/astro/test/fixtures/minification-html/src/pages/index.astro
vendored
Normal 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>
|
3
packages/astro/test/fixtures/minification-html/src/pages/page.astro
vendored
Normal file
3
packages/astro/test/fixtures/minification-html/src/pages/page.astro
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
---
|
||||
<div>3</div>
|
|
@ -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':
|
||||
|
|
Loading…
Reference in a new issue