A HTML minifier for Astro
DEPRECATED: You don't need this anymore!
Since Astro 2.5 HTML minification is built in.
Install via your tool of choice:
# Using NPM
npm run astro add astro-html-minifier
# Using Yarn
yarn astro add astro-html-minifier
# Using PNPM
pnpm astro add astro-html-minifier
Then, restart the dev server by typing CTRL-C
and then npm run dev
in the terminal window that was running Astro.
First, install the package with your favorite package manager: pnpm add --dev astro-html-minifier
,
then configure it in your astro.config.*
file in the integrations
property:
import { defineConfig } from "astro/config";
import minifyHtml from "astro-html-minifier";
export default defineConfig({
// ...
integrations: [minifyHtml()],
});
While the default options should serve most users well, you may want to change the options yourself. The options are as follows:
export interface HTMLOptions {
/** Do not minify DOCTYPEs. Minified DOCTYPEs may not be spec compliant. */
doNotMinifyDoctype?: boolean; // true
/** Ensure all unquoted attribute values in the output do not contain any characters prohibited by the WHATWG specification. */
ensureSpecCompliantUnquotedAttributeValues?: boolean; // true
/** Do not omit closing tags when possible. */
keepClosingTags?: boolean; // true
/** Do not omit `<html>` and `<head>` opening tags when they don't have attributes. */
keepHtmlAndHeadOpeningTags?: boolean; // true
/** Keep spaces between attributes when possible to conform to HTML standards. */
keepSpacesBetweenAttributes?: boolean; // true
/** Keep all comments. */
keepComments?: boolean; // false
/**
* If enabled, content in `<script>` tags with a JS or no [MIME type](https://mimesniff.spec.whatwg.org/#javascript-mime-type) will be minified using [minify-js](https://github.com/wilsonzlin/minify-js).
*/
minifyJs?: boolean; // true
/**
* If enabled, CSS in `<style>` tags and `style` attributes will be minified.
*/
minifyCss?: boolean; // true
/** Remove all bangs. */
removeBangs?: boolean; // false
/** Remove all processing_instructions. */
removeProcessingInstructions?: boolean; // false
}
import { defineConfig } from "astro/config";
import minifyHtml from "astro-html-minifier";
export default defineConfig({
// ...
integrations: [minifyHtml({ keepComments: true, minifyJs: false })],
});
MIT.