withastro / roadmap

Ideas, suggestions, and formal RFC proposals for the Astro project.
320 stars 30 forks source link

HTML Minification #537

Closed natemoo-re closed 1 year ago

natemoo-re commented 1 year ago

Body

Summary

When building for production, we should safely collapse whitespace when generating the HTML

Background & Motivation

Astro users are often confused why we don't minify HTML by default.

The long answer is:

The shorter answer is:

Goals

Non-Goals

Example

Probably just a boolean config option. Name TBD.

paperdave commented 1 year ago

The compiler already has a flag that minifies markup from Astro components at compile time, which as far as I'm aware is unused in the framework.

I had a different discussion that briefly outlines how to get this method working, it's a couple of lines of code: https://github.com/withastro/roadmap/discussions/511. Not sure how much it applies given the "long answer."

JerryWu1234 commented 1 year ago

Do we need to support it as a new flag? Such as pnpm run build --compact. And by default is true?

JerryWu1234 commented 1 year ago

@matthewp cc

JerryWu1234 commented 1 year ago

image @paperdave, the bundle was partially compressed. Please check again.

this link https://github.com/wulinsheng123/astro/blob/add-Minification/packages/astro/test/fixtures/minification-html/package.json

matthewp commented 1 year ago

I think we want a config option, yes. Probably build.compressHTML: true?

JerryWu1234 commented 1 year ago

I think we want a config option, yes. Probably build.compressHTML: true?

Ok, no problem. I will continue when that bug of the compiler has been fixed.

matthewp commented 1 year ago

@natemoo-re Where should the option for this go? I previously suggested build.compressHTML but how I realize this is both dev and build. Should this be top-level compressHTML?

millette commented 1 year ago

@matthewp Should the html actually be compressed when developing?

JerryWu1234 commented 1 year ago

@natemoo-re Where should the option for this go? I previously suggested build.compressHTML but how I realize this is both dev and build. Should this be top-level compressHTML?

I propose it only supports pro. so supporting build.compressHTML. because the users don't need to see HTML that was compressed in most of the scenes in the dev.

delucis commented 1 year ago

In general, having it behave differently in dev vs production can trip developers up — if minification is the source of a bug, you don't see it until you get to a production or staging environment. So I'd suggest consistent dev & prod behaviour (which a user can then change by using a ternary in the config or passing a flag).

JerryWu1234 commented 1 year ago

In general, having it behave differently in dev vs production can trip developers up — if minification is the source of a bug, you don't see it until you get to a production or staging environment. So I'd suggest consistent dev & prod behaviour (which a user can then change by using a ternary in the config or passing a flag).

OK, I agree. I will follow what you said while I develop.

matthewp commented 1 year ago

This was released in 2.5.