add compact property when the user run pnpm run build

This commit is contained in:
wuls 2023-03-31 09:49:27 +08:00
parent c440edf07f
commit c85a4b0e60
3 changed files with 24 additions and 0 deletions

View file

@ -426,6 +426,23 @@ export interface AstroUserConfig {
*/ */
site?: string; 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 * @docs
* @name base * @name base

View file

@ -37,6 +37,7 @@ export async function compile({
// use `sourcemap: "both"` so that sourcemap is included in the code // use `sourcemap: "both"` so that sourcemap is included in the code
// result passed to esbuild, but also available in the catch handler. // result passed to esbuild, but also available in the catch handler.
transformResult = await transform(source, { transformResult = await transform(source, {
compact: astroConfig.compact,
filename, filename,
normalizedFilename: normalizeFilename(filename, astroConfig.root), normalizedFilename: normalizeFilename(filename, astroConfig.root),
sourcemap: 'both', sourcemap: 'both',

View file

@ -22,6 +22,7 @@ const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
assets: '_astro', assets: '_astro',
serverEntry: 'entry.mjs', serverEntry: 'entry.mjs',
}, },
compact: true,
server: { server: {
host: false, host: false,
port: 3000, port: 3000,
@ -62,6 +63,7 @@ export const AstroConfigSchema = z.object({
.default(ASTRO_CONFIG_DEFAULTS.outDir) .default(ASTRO_CONFIG_DEFAULTS.outDir)
.transform((val) => new URL(val)), .transform((val) => new URL(val)),
site: z.string().url().optional(), site: z.string().url().optional(),
compact: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.compact),
base: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.base), base: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.base),
trailingSlash: z trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')]) .union([z.literal('always'), z.literal('never'), z.literal('ignore')])
@ -197,6 +199,10 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
.string() .string()
.default(ASTRO_CONFIG_DEFAULTS.srcDir) .default(ASTRO_CONFIG_DEFAULTS.srcDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)), .transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
compact: z
.boolean()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.compact),
publicDir: z publicDir: z
.string() .string()
.default(ASTRO_CONFIG_DEFAULTS.publicDir) .default(ASTRO_CONFIG_DEFAULTS.publicDir)