astro-html-minifier-next 2.0.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
  [![Version badge](https://img.shields.io/npm/v/astro-html-minifier-next?style=for-the-badge&label=Version&labelColor=2a2d35&color=f2ebf4)][latest-release]
10
10
  [![NPM downloads badge](https://img.shields.io/npm/d18m/astro-html-minifier-next?style=for-the-badge&label=NPM%20Downloads&labelColor=2a2d35&color=e13fa0&cacheSeconds=86400)][package-on-npm]
11
11
  [![JSR score badge](https://jsr.io/badges/@jonasgeiler/astro-html-minifier-next/score?style=for-the-badge&label=Score&logoColor=fff&labelColor=2a2d35&color=f2ebf4&cacheSeconds=86400)][package-on-jsr]
12
- [![Minified size (gzip) badge](https://img.shields.io/bundlejs/size/astro-html-minifier-next?style=for-the-badge&labelColor=2a2d35&color=e13fa0&cacheSeconds=86400)][package-on-bundlejs]
12
+ [![NPM unpacked size badge](https://img.shields.io/npm/unpacked-size/astro-html-minifier-next?style=for-the-badge&labelColor=2a2d35&color=e13fa0)][package-on-bundlejs]
13
13
 
14
14
  ```typescript
15
15
  import { defineConfig } from "astro/config";
@@ -20,6 +20,7 @@ export default defineConfig({
20
20
  htmlMinifier({
21
21
  /* My recommended html-minifier-next options: */
22
22
  caseSensitive: true,
23
+ collapseAttributeWhitespace: false,
23
24
  collapseBooleanAttributes: true,
24
25
  collapseInlineTagWhitespace: false,
25
26
  collapseWhitespace: true,
@@ -27,11 +28,11 @@ export default defineConfig({
27
28
  continueOnMinifyError: false,
28
29
  continueOnParseError: false,
29
30
  decodeEntities: true,
30
- html5: true,
31
31
  includeAutoGeneratedTags: true,
32
32
  keepClosingSlash: false,
33
33
  minifyCSS: true,
34
34
  minifyJS: true,
35
+ minifySVG: true,
35
36
  minifyURLs: false,
36
37
  noNewlinesBeforeTagClose: false,
37
38
  preserveLineBreaks: false,
@@ -47,7 +48,7 @@ export default defineConfig({
47
48
  removeStyleLinkTypeAttributes: true,
48
49
  removeTagWhitespace: false,
49
50
  sortAttributes: false,
50
- sortClassName: false,
51
+ sortClassNames: false,
51
52
  useShortDoctype: true,
52
53
  }),
53
54
  ],
@@ -106,6 +107,9 @@ You can find a quick reference of all available options in the
106
107
  You can also check out the
107
108
  [JSDoc comments in the source code][html-minifier-next-options-source].
108
109
 
110
+ There is currently one additional option only specific to this Astro
111
+ integration. [See below](#alwayswriteminifiedhtml).
112
+
109
113
  > **Tip**
110
114
  > To ensure consistent and wide browser support throughout your project,
111
115
  > I recommend setting the `targets` property of the `minifyCSS` option to your
@@ -146,9 +150,21 @@ You can also check out the
146
150
  >
147
151
  > More information can be found in the [Lightning CSS documentation][lightningcss-docs].
148
152
 
153
+ #### `alwaysWriteMinifiedHTML`
154
+
155
+ This option is not passed to [html-minifier-next][html-minifier-next]. It
156
+ only controls the behavior of this Astro integration.
157
+
158
+ By default, the integration only overwrites the original HTML assets
159
+ if the minified HTML is smaller.
160
+ However, there are [cases][issue-7] where you might actually prefer the
161
+ larger output due to special compatibility reasons or otherwise.
162
+ Setting this option to `true` causes the integration to always overwrite
163
+ the HTML assets with their minified results, regardless of size.
164
+
149
165
  ## Credits
150
166
 
151
- This integration wouldn't be possible without the awesome work of
167
+ This project wouldn't be possible without the awesome work of
152
168
  [Juriy Zaytsev aka. @kangax][@kangax] ([html-minifier][html-minifier]),
153
169
  [the Terser team][@terser] ([html-minifier-terser][html-minifier-terser]), and
154
170
  of course [Jens Oliver Meiert aka. @j9t][@j9t] ([html-minifier-next][html-minifier-next]).
@@ -156,14 +172,15 @@ of course [Jens Oliver Meiert aka. @j9t][@j9t] ([html-minifier-next][html-minifi
156
172
  [astro]: https://astro.build/
157
173
  [html-minifier-next]: https://www.npmjs.com/package/html-minifier-next
158
174
  [html-minifier-next-options]: https://www.npmjs.com/package/html-minifier-next#options-quick-reference
159
- [html-minifier-next-options-source]: https://github.com/j9t/html-minifier-next/blob/ba95daa75dfec6e3c6bb8bd1b0af3ad84e596e61/src/htmlminifier.js#L1482
175
+ [html-minifier-next-options-source]: https://github.com/j9t/html-minifier-next/blob/e2995fc35a7a3906d3d1960d508d0d24a02128c8/src/htmlminifier.js#L1501
160
176
  [latest-release]: https://github.com/jonasgeiler/astro-html-minifier-next/releases/latest
161
- [package-on-bundlejs]: https://bundlejs.com/?q=astro-html-minifier-next
177
+ [package-on-bundlejs]: https://bundlejs.com/?q=astro-html-minifier-next&config=%7B%22esbuild%22%3A%7B%22external%22%3A%5B%22lightningcss%22%5D%7D%7D
162
178
  [package-on-jsr]: https://jsr.io/@jonasgeiler/astro-html-minifier-next
163
179
  [package-on-npm]: https://www.npmjs.com/package/astro-html-minifier-next
164
180
  [browserslist]: https://browsersl.ist/
165
181
  [lightningcss]: https://lightningcss.dev/
166
182
  [lightningcss-docs]: https://lightningcss.dev/docs.html#with-vite
183
+ [issue-7]: https://github.com/jonasgeiler/astro-html-minifier-next/issues/7
167
184
  [@kangax]: https://github.com/kangax
168
185
  [html-minifier]: https://github.com/kangax/html-minifier
169
186
  [@terser]: https://github.com/terser
package/dist/index.d.ts CHANGED
@@ -1,6 +1,23 @@
1
1
  import type { AstroIntegration } from "astro";
2
- import { type MinifierOptions as HTMLMinifierOptions } from "html-minifier-next";
3
- export type { HTMLMinifierOptions };
2
+ import { type MinifierOptions } from "html-minifier-next";
3
+ /**
4
+ * Options from
5
+ * [html-minifier-next](https://www.npmjs.com/package/html-minifier-next),
6
+ * extended with some options only used by the {@link htmlMinifier}
7
+ * Astro integration.
8
+ */
9
+ export interface HTMLMinifierOptions extends MinifierOptions {
10
+ /**
11
+ * This option is only used by the {@link htmlMinifier} Astro integration.
12
+ *
13
+ * If `true`, the HTML assets will always be overwritten with their
14
+ * minified HTML, even if it would result in a larger file size than
15
+ * the original.
16
+ *
17
+ * @default false
18
+ */
19
+ alwaysWriteMinifiedHTML?: boolean;
20
+ }
4
21
  /**
5
22
  * An Astro integration that minifies HTML assets using
6
23
  * [html-minifier-next](https://www.npmjs.com/package/html-minifier-next).
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAC9C,OAAO,EACN,KAAK,eAAe,IAAI,mBAAmB,EAE3C,MAAM,oBAAoB,CAAC;AAG5B,YAAY,EAAE,mBAAmB,EAAE,CAAC;AAEpC;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,YAAY,CACnC,OAAO,CAAC,EAAE,mBAAmB,GAC3B,gBAAgB,CAyIlB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAC9C,OAAO,EAAE,KAAK,eAAe,EAAwB,MAAM,oBAAoB,CAAC;AAEhF;;;;;GAKG;AACH,MAAM,WAAW,mBAAoB,SAAQ,eAAe;IAC3D;;;;;;;;OAQG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;CAClC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,YAAY,CACnC,OAAO,GAAE,mBAAwB,GAC/B,gBAAgB,CAsJlB"}
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import { availableParallelism as getAvailableParallelism } from "node:os";
3
3
  import { relative as getRelativePath } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { styleText } from "node:util";
6
- import { minify as minifyHTML, } from "html-minifier-next";
6
+ import { minify as minifyHTML } from "html-minifier-next";
7
7
  /**
8
8
  * An Astro integration that minifies HTML assets using
9
9
  * [html-minifier-next](https://www.npmjs.com/package/html-minifier-next).
@@ -12,7 +12,7 @@ import { minify as minifyHTML, } from "html-minifier-next";
12
12
  * [html-minifier-next](https://www.npmjs.com/package/html-minifier-next).
13
13
  * @returns The Astro integration.
14
14
  */
15
- export default function htmlMinifier(options) {
15
+ export default function htmlMinifier(options = {}) {
16
16
  // API Reference: https://docs.astro.build/en/reference/integrations-reference/
17
17
  return {
18
18
  name: "astro-html-minifier-next",
@@ -20,6 +20,8 @@ export default function htmlMinifier(options) {
20
20
  "astro:build:done": async ({ assets, dir: distUrl, logger, }) => {
21
21
  logger.info(styleText(["bgGreen", "black"], " minifying html assets "));
22
22
  const totalTimeStart = performance.now(); // --- TOTAL TIMED BLOCK START ---
23
+ const { alwaysWriteMinifiedHTML = false, ...minifyHTMLOptions // Rest of the options go to html-minifier-next.
24
+ } = options;
23
25
  const tasks = [];
24
26
  let tasksTotal = 0;
25
27
  let tasksDone = 0;
@@ -41,10 +43,12 @@ export default function htmlMinifier(options) {
41
43
  encoding: "utf8",
42
44
  signal,
43
45
  });
44
- const minifiedHTML = await minifyHTML(html, options);
46
+ const minifiedHTML = await minifyHTML(html, minifyHTMLOptions);
45
47
  const savings = Buffer.byteLength(html) - Buffer.byteLength(minifiedHTML);
46
- if (savings > 0) {
47
- // Only write the minified HTML to the file if it's smaller.
48
+ const hasSavings = savings > 0;
49
+ if (hasSavings || alwaysWriteMinifiedHTML) {
50
+ // Only write the minified HTML to the file if it's smaller,
51
+ // or if alwaysWriteMinifiedHTML is enabled.
48
52
  await writeFile(assetPath, minifiedHTML, {
49
53
  encoding: "utf8",
50
54
  signal,
@@ -54,7 +58,7 @@ export default function htmlMinifier(options) {
54
58
  const time = timeEnd - timeStart;
55
59
  // Log a nice summary of the minification savings and the time it
56
60
  // took.
57
- const savingsSign = savings > 0 ? "-" : "+";
61
+ const savingsSign = hasSavings ? "-" : "+";
58
62
  const savingsAbs = Math.abs(savings);
59
63
  const savingsWithUnit = savingsAbs < 1024
60
64
  ? `${savingsAbs}B`
@@ -64,8 +68,13 @@ export default function htmlMinifier(options) {
64
68
  const timeWithUnit = time < 1000
65
69
  ? `${Math.round(time)}ms`
66
70
  : `${(time / 1000).toFixed(2)}s`;
71
+ const savingsNote = hasSavings || alwaysWriteMinifiedHTML
72
+ ? hasSavings
73
+ ? ""
74
+ : ", always write enabled"
75
+ : ", skipped";
67
76
  logger.info(logLineAssetPath +
68
- styleText(savings <= 0 ? "yellow" : "dim", `(${savingsSign}${savingsWithUnit}${savings <= 0 ? ", skipped" : ""}) `) +
77
+ styleText(hasSavings ? "dim" : "yellow", `(${savingsSign}${savingsWithUnit}${savingsNote}) `) +
69
78
  styleText("dim", `(+${timeWithUnit}) (${++tasksDone}/${tasksTotal})`));
70
79
  });
71
80
  tasksTotal++;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAE,oBAAoB,IAAI,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAE,QAAQ,IAAI,eAAe,EAAE,MAAM,WAAW,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAEN,MAAM,IAAI,UAAU,GACpB,MAAM,oBAAoB,CAAC;AAK5B;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,YAAY,CACnC,OAA6B;IAE7B,+EAA+E;IAC/E,OAAO;QACN,IAAI,EAAE,0BAA0B;QAChC,KAAK,EAAE;YACN,kBAAkB,EAAE,KAAK,EAAE,EAC1B,MAAM,EACN,GAAG,EAAE,OAAO,EACZ,MAAM,GACN,EAAiB,EAAE;gBACnB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC,EAAE,yBAAyB,CAAC,CAAC,CAAC;gBAExE,MAAM,cAAc,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC,kCAAkC;gBAE5E,MAAM,KAAK,GAA4B,EAAE,CAAC;gBAC1C,IAAI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C,MAAM,CAAC;oBACrB,CAAC;gBACF,CAAC;gBAED,0CAA0C;gBAC1C,MAAM,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;gBAElC,MAAM,YAAY,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC,gCAAgC;gBACxE,MAAM,SAAS,GAAG,YAAY,GAAG,cAAc,CAAC;gBAEhD,2CAA2C;gBAC3C,MAAM,iBAAiB,GACtB,SAAS,GAAG,IAAI;oBACf,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI;oBAC9B,CAAC,CAAC,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;gBACxC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,kBAAkB,iBAAiB,GAAG,CAAC,CAAC,CAAC;YACzE,CAAC;SACD;KACD,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-html-minifier-next",
3
- "version": "2.0.2",
3
+ "version": "3.0.0",
4
4
  "description": "Minify Astro HTML assets using html-minifier-next!",
5
5
  "homepage": "https://github.com/jonasgeiler/astro-html-minifier-next#readme",
6
6
  "bugs": "https://github.com/jonasgeiler/astro-html-minifier-next/issues",
@@ -18,12 +18,12 @@
18
18
  "astro": "^5.0.0"
19
19
  },
20
20
  "dependencies": {
21
- "html-minifier-next": "^4.3.0"
21
+ "html-minifier-next": "^5.2.0"
22
22
  },
23
23
  "devDependencies": {
24
- "@biomejs/biome": "2.3.2",
25
- "@types/node": "24.9.2",
26
- "astro": "5.16.0",
24
+ "@biomejs/biome": "2.3.15",
25
+ "@types/node": "24.12.0",
26
+ "astro": "5.16.16",
27
27
  "typescript": "5.9.3"
28
28
  },
29
29
  "type": "module",
package/src/index.ts CHANGED
@@ -4,13 +4,26 @@ import { relative as getRelativePath } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { styleText } from "node:util";
6
6
  import type { AstroIntegration } from "astro";
7
- import {
8
- type MinifierOptions as HTMLMinifierOptions,
9
- minify as minifyHTML,
10
- } from "html-minifier-next";
7
+ import { type MinifierOptions, minify as minifyHTML } from "html-minifier-next";
11
8
 
12
- // Re-export the HTMLMinifierOptions type for users who want to use it.
13
- export type { HTMLMinifierOptions };
9
+ /**
10
+ * Options from
11
+ * [html-minifier-next](https://www.npmjs.com/package/html-minifier-next),
12
+ * extended with some options only used by the {@link htmlMinifier}
13
+ * Astro integration.
14
+ */
15
+ export interface HTMLMinifierOptions extends MinifierOptions {
16
+ /**
17
+ * This option is only used by the {@link htmlMinifier} Astro integration.
18
+ *
19
+ * If `true`, the HTML assets will always be overwritten with their
20
+ * minified HTML, even if it would result in a larger file size than
21
+ * the original.
22
+ *
23
+ * @default false
24
+ */
25
+ alwaysWriteMinifiedHTML?: boolean;
26
+ }
14
27
 
15
28
  /**
16
29
  * An Astro integration that minifies HTML assets using
@@ -21,7 +34,7 @@ export type { HTMLMinifierOptions };
21
34
  * @returns The Astro integration.
22
35
  */
23
36
  export default function htmlMinifier(
24
- options?: HTMLMinifierOptions,
37
+ options: HTMLMinifierOptions = {},
25
38
  ): AstroIntegration {
26
39
  // API Reference: https://docs.astro.build/en/reference/integrations-reference/
27
40
  return {
@@ -36,6 +49,11 @@ export default function htmlMinifier(
36
49
 
37
50
  const totalTimeStart = performance.now(); // --- TOTAL TIMED BLOCK START ---
38
51
 
52
+ const {
53
+ alwaysWriteMinifiedHTML = false,
54
+ ...minifyHTMLOptions // Rest of the options go to html-minifier-next.
55
+ } = options;
56
+
39
57
  const tasks: (() => Promise<void>)[] = [];
40
58
  let tasksTotal = 0;
41
59
  let tasksDone = 0;
@@ -61,12 +79,14 @@ export default function htmlMinifier(
61
79
  encoding: "utf8",
62
80
  signal,
63
81
  });
64
- const minifiedHTML = await minifyHTML(html, options);
82
+ const minifiedHTML = await minifyHTML(html, minifyHTMLOptions);
65
83
 
66
84
  const savings =
67
85
  Buffer.byteLength(html) - Buffer.byteLength(minifiedHTML);
68
- if (savings > 0) {
69
- // Only write the minified HTML to the file if it's smaller.
86
+ const hasSavings = savings > 0;
87
+ if (hasSavings || alwaysWriteMinifiedHTML) {
88
+ // Only write the minified HTML to the file if it's smaller,
89
+ // or if alwaysWriteMinifiedHTML is enabled.
70
90
  await writeFile(assetPath, minifiedHTML, {
71
91
  encoding: "utf8",
72
92
  signal,
@@ -78,7 +98,7 @@ export default function htmlMinifier(
78
98
 
79
99
  // Log a nice summary of the minification savings and the time it
80
100
  // took.
81
- const savingsSign = savings > 0 ? "-" : "+";
101
+ const savingsSign = hasSavings ? "-" : "+";
82
102
  const savingsAbs = Math.abs(savings);
83
103
  const savingsWithUnit =
84
104
  savingsAbs < 1024
@@ -90,11 +110,17 @@ export default function htmlMinifier(
90
110
  time < 1000
91
111
  ? `${Math.round(time)}ms`
92
112
  : `${(time / 1000).toFixed(2)}s`;
113
+ const savingsNote =
114
+ hasSavings || alwaysWriteMinifiedHTML
115
+ ? hasSavings
116
+ ? ""
117
+ : ", always write enabled"
118
+ : ", skipped";
93
119
  logger.info(
94
120
  logLineAssetPath +
95
121
  styleText(
96
- savings <= 0 ? "yellow" : "dim",
97
- `(${savingsSign}${savingsWithUnit}${savings <= 0 ? ", skipped" : ""}) `,
122
+ hasSavings ? "dim" : "yellow",
123
+ `(${savingsSign}${savingsWithUnit}${savingsNote}) `,
98
124
  ) +
99
125
  styleText(
100
126
  "dim",