astro-html-minifier-next 2.0.2 → 2.1.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
@@ -106,6 +106,9 @@ You can find a quick reference of all available options in the
106
106
  You can also check out the
107
107
  [JSDoc comments in the source code][html-minifier-next-options-source].
108
108
 
109
+ There is currently one additional option only specific to this Astro
110
+ integration. [See below](#alwayswriteminifiedhtml).
111
+
109
112
  > **Tip**
110
113
  > To ensure consistent and wide browser support throughout your project,
111
114
  > I recommend setting the `targets` property of the `minifyCSS` option to your
@@ -146,9 +149,21 @@ You can also check out the
146
149
  >
147
150
  > More information can be found in the [Lightning CSS documentation][lightningcss-docs].
148
151
 
152
+ #### `alwaysWriteMinifiedHTML`
153
+
154
+ This option is not passed to [html-minifier-next][html-minifier-next]. It
155
+ only controls the behavior of this Astro integration.
156
+
157
+ By default, the integration only overwrites the original HTML assets
158
+ if the minified HTML is smaller.
159
+ However, there are [cases][issue-7] where you might actually prefer the
160
+ larger output due to special compatibility reasons or otherwise.
161
+ Setting this option to `true` causes the integration to always overwrite
162
+ the HTML assets with their minified results, regardless of size.
163
+
149
164
  ## Credits
150
165
 
151
- This integration wouldn't be possible without the awesome work of
166
+ This project wouldn't be possible without the awesome work of
152
167
  [Juriy Zaytsev aka. @kangax][@kangax] ([html-minifier][html-minifier]),
153
168
  [the Terser team][@terser] ([html-minifier-terser][html-minifier-terser]), and
154
169
  of course [Jens Oliver Meiert aka. @j9t][@j9t] ([html-minifier-next][html-minifier-next]).
@@ -164,6 +179,7 @@ of course [Jens Oliver Meiert aka. @j9t][@j9t] ([html-minifier-next][html-minifi
164
179
  [browserslist]: https://browsersl.ist/
165
180
  [lightningcss]: https://lightningcss.dev/
166
181
  [lightningcss-docs]: https://lightningcss.dev/docs.html#with-vite
182
+ [issue-7]: https://github.com/jonasgeiler/astro-html-minifier-next/issues/7
167
183
  [@kangax]: https://github.com/kangax
168
184
  [html-minifier]: https://github.com/kangax/html-minifier
169
185
  [@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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"astro-html-minifier-next",
3
- "version": "2.0.2",
3
+ "version": "2.1.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",
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",