From c85a4b0e60ea136fee7f6831c7ccd1fb4a6128ec Mon Sep 17 00:00:00 2001 From: wuls Date: Fri, 31 Mar 2023 09:49:27 +0800 Subject: [PATCH] add compact property when the user run pnpm run build --- packages/astro/src/@types/astro.ts | 17 +++++++++++++++++ packages/astro/src/core/compile/compile.ts | 1 + packages/astro/src/core/config/schema.ts | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 20aee729d..06b1bdfb8 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -426,6 +426,23 @@ export interface AstroUserConfig { */ site?: string; + + /** + * @docs + * @name compact + * @type {boolean} + * @default `true` + * @description + * Set to `true` to enable compact mode. This will remove all whitespace from your final HTMl file, including newlines. This is useful for reducing the size of your final build html bundle + * + * ```js + * { + * compact: true + * } + * ``` + */ + compact?: boolean; + /** * @docs * @name base diff --git a/packages/astro/src/core/compile/compile.ts b/packages/astro/src/core/compile/compile.ts index a0199a2fb..d5f298cea 100644 --- a/packages/astro/src/core/compile/compile.ts +++ b/packages/astro/src/core/compile/compile.ts @@ -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.compact, filename, normalizedFilename: normalizeFilename(filename, astroConfig.root), sourcemap: 'both', diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 5b374ce33..3b6a92d0c 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -22,6 +22,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = { assets: '_astro', serverEntry: 'entry.mjs', }, + compact: true, server: { host: false, port: 3000, @@ -62,6 +63,7 @@ export const AstroConfigSchema = z.object({ .default(ASTRO_CONFIG_DEFAULTS.outDir) .transform((val) => new URL(val)), site: z.string().url().optional(), + compact: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.compact), base: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.base), trailingSlash: z .union([z.literal('always'), z.literal('never'), z.literal('ignore')]) @@ -197,6 +199,10 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) { .string() .default(ASTRO_CONFIG_DEFAULTS.srcDir) .transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)), + compact: z + .boolean() + .optional() + .default(ASTRO_CONFIG_DEFAULTS.compact), publicDir: z .string() .default(ASTRO_CONFIG_DEFAULTS.publicDir)