@alfred.westerveld/astro-compress-html-fast 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # @alfred.westerveld/astro-compress-html-fast
2
+
3
+ Fast, conservative post-build HTML compression for Astro.
4
+
5
+ This Astro integration runs after `astro build`, walks generated HTML files, and writes smaller output back to the build directory. It does not transform source `.astro` files.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @alfred.westerveld/astro-compress-html-fast
11
+ pnpm add @alfred.westerveld/astro-compress-html-fast
12
+ yarn add @alfred.westerveld/astro-compress-html-fast
13
+ bun add @alfred.westerveld/astro-compress-html-fast
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ts
19
+ import { defineConfig } from "astro/config";
20
+ import compressHtml from "@alfred.westerveld/astro-compress-html-fast";
21
+
22
+ export default defineConfig({
23
+ integrations: [
24
+ compressHtml({
25
+ minifyHtml: true,
26
+ minifyInlineJs: true,
27
+ minifyInlineCss: true,
28
+ }),
29
+ ],
30
+ });
31
+ ```
32
+
33
+ ## Options
34
+
35
+ ```ts
36
+ interface CompressHtmlOptions {
37
+ minifyHtml?: boolean;
38
+ minifyInlineJs?: boolean;
39
+ minifyInlineCss?: boolean;
40
+ failOnError?: boolean;
41
+ verbose?: boolean;
42
+ target?: string;
43
+ include?: string[];
44
+ exclude?: string[];
45
+ }
46
+ ```
47
+
48
+ | Option | Default | Description |
49
+ | --- | --- | --- |
50
+ | `minifyHtml` | `true` | Minify generated HTML with conservative `@swc/html` settings. |
51
+ | `minifyInlineJs` | `false` | Minify eligible inline classic/module scripts with `esbuild`. |
52
+ | `minifyInlineCss` | `false` | Minify inline `<style>` blocks with `esbuild`. |
53
+ | `failOnError` | `false` | Throw on the first failed file instead of warning and keeping the original. |
54
+ | `verbose` | `false` | Print a before/after byte summary even when no files changed. |
55
+ | `target` | `"es2020"` | JavaScript/CSS target passed to `esbuild`. |
56
+ | `include` | `["**/*.html"]` | Glob patterns matched inside Astro's output directory. |
57
+ | `exclude` | `[]` | Glob patterns ignored inside Astro's output directory. |
58
+
59
+ ## Safety Notes
60
+
61
+ The integration protects these regions before whole-document HTML minification:
62
+
63
+ - `<pre>`
64
+ - `<code>`
65
+ - `<textarea>`
66
+ - external scripts
67
+ - JSON-LD scripts
68
+ - import maps
69
+ - unknown or non-JS script types
70
+
71
+ Inline JavaScript and CSS minification are opt-in because those blocks often contain framework data, templates, or code with assumptions outside normal bundled assets.
72
+
73
+ The default HTML pass avoids aggressive rewrites such as optional tag removal, attribute sorting, quote removal, boolean attribute collapsing, and attribute normalization.
74
+
75
+ ## Reporting
76
+
77
+ When files are changed, the integration reports how many HTML files were compressed and the before/after byte count:
78
+
79
+ ```text
80
+ compressed 4/4 HTML files; 18400/22120 bytes (3720 saved, 16.8%)
81
+ ```
82
+
83
+ Use `verbose: true` to always print the summary.
84
+
85
+ ## When Not To Use This
86
+
87
+ Do not use this package when:
88
+
89
+ - another post-build HTML optimizer is already responsible for generated HTML
90
+ - your deployment or tests depend on exact generated HTML bytes
91
+ - pages contain unusual whitespace-sensitive markup outside protected elements
92
+ - gzip or Brotli already gives enough benefit and build-time HTML rewriting is unnecessary
@@ -0,0 +1,16 @@
1
+ import { AstroIntegration } from 'astro';
2
+
3
+ interface CompressHtmlOptions {
4
+ minifyHtml?: boolean;
5
+ minifyInlineJs?: boolean;
6
+ minifyInlineCss?: boolean;
7
+ failOnError?: boolean;
8
+ verbose?: boolean;
9
+ target?: string;
10
+ include?: string[];
11
+ exclude?: string[];
12
+ }
13
+
14
+ declare function compressHtml(userOptions?: CompressHtmlOptions): AstroIntegration;
15
+
16
+ export { type CompressHtmlOptions, compressHtml as default };
package/dist/index.js ADDED
@@ -0,0 +1,294 @@
1
+ // src/index.ts
2
+ import { readFile, writeFile } from "fs/promises";
3
+
4
+ // src/minifyHtml.ts
5
+ import { minify } from "@swc/html";
6
+ import { transform } from "esbuild";
7
+ var rawTextElementPattern = /<(pre|code|textarea)\b[^>]*>[\s\S]*?<\/\1>/gi;
8
+ var scriptPattern = /<script\b([^>]*)>([\s\S]*?)<\/script>/gi;
9
+ var stylePattern = /<style\b([^>]*)>([\s\S]*?)<\/style>/gi;
10
+ async function minifyHtml(html, options) {
11
+ const protectedRegions = [];
12
+ const withScripts = await processScripts(html, options, protectedRegions);
13
+ const withStyles = await processStyles(withScripts, options, protectedRegions);
14
+ const protectedHtml = protectRawTextElements(withStyles, protectedRegions);
15
+ if (!options.minifyHtml) {
16
+ return restoreProtectedRegions(protectedHtml, protectedRegions);
17
+ }
18
+ try {
19
+ const result = await minify(protectedHtml, {
20
+ collapseWhitespaces: "conservative",
21
+ removeComments: false,
22
+ removeEmptyAttributes: false,
23
+ removeRedundantAttributes: "none",
24
+ collapseBooleanAttributes: false,
25
+ normalizeAttributes: false,
26
+ minifyJson: false,
27
+ minifyJs: false,
28
+ minifyCss: false,
29
+ sortSpaceSeparatedAttributeValues: false,
30
+ sortAttributes: false,
31
+ tagOmission: false,
32
+ selfClosingVoidElements: false,
33
+ quotes: true
34
+ });
35
+ if (result.errors && result.errors.length > 0) {
36
+ throw new Error(result.errors.map((error) => error.message).join("; "));
37
+ }
38
+ return restoreProtectedRegions(result.code, protectedRegions);
39
+ } catch (error) {
40
+ if (options.failOnError) {
41
+ throw toError(error, "Could not minify HTML");
42
+ }
43
+ const fallback = conservativeWhitespacePass(protectedHtml);
44
+ return restoreProtectedRegions(fallback, protectedRegions);
45
+ }
46
+ }
47
+ async function processScripts(html, options, protectedRegions) {
48
+ const replacements = Array.from(html.matchAll(scriptPattern), async (match) => {
49
+ const fullMatch = match[0];
50
+ const attributes = match[1] ?? "";
51
+ const content = match[2] ?? "";
52
+ const replacement = await minifyScriptBlock(
53
+ fullMatch,
54
+ attributes,
55
+ content,
56
+ options
57
+ );
58
+ return {
59
+ fullMatch,
60
+ replacement: protect(replacement, protectedRegions)
61
+ };
62
+ });
63
+ const resolved = await Promise.all(replacements);
64
+ return replaceMatches(html, resolved);
65
+ }
66
+ async function processStyles(html, options, protectedRegions) {
67
+ const replacements = Array.from(html.matchAll(stylePattern), async (match) => {
68
+ const fullMatch = match[0];
69
+ const attributes = match[1] ?? "";
70
+ const content = match[2] ?? "";
71
+ const replacement = await minifyStyleBlock(
72
+ fullMatch,
73
+ attributes,
74
+ content,
75
+ options
76
+ );
77
+ return {
78
+ fullMatch,
79
+ replacement: protect(replacement, protectedRegions)
80
+ };
81
+ });
82
+ const resolved = await Promise.all(replacements);
83
+ return replaceMatches(html, resolved);
84
+ }
85
+ async function minifyScriptBlock(original, attributes, content, options) {
86
+ if (!options.minifyInlineJs || !isMinifiableScript(attributes)) {
87
+ return original;
88
+ }
89
+ try {
90
+ const transformOptions = {
91
+ loader: "js",
92
+ minify: true,
93
+ target: options.target
94
+ };
95
+ if (scriptIsModule(attributes)) {
96
+ transformOptions.format = "esm";
97
+ }
98
+ const result = await transform(content, {
99
+ ...transformOptions
100
+ });
101
+ return `<script${attributes}>${result.code.trim()}</script>`;
102
+ } catch (error) {
103
+ if (options.failOnError) {
104
+ throw toError(error, "Could not minify inline script");
105
+ }
106
+ return original;
107
+ }
108
+ }
109
+ async function minifyStyleBlock(original, attributes, content, options) {
110
+ if (!options.minifyInlineCss) {
111
+ return original;
112
+ }
113
+ try {
114
+ const result = await transform(content, {
115
+ loader: "css",
116
+ minify: true,
117
+ target: options.target
118
+ });
119
+ return `<style${attributes}>${result.code.trim()}</style>`;
120
+ } catch (error) {
121
+ if (options.failOnError) {
122
+ throw toError(error, "Could not minify inline style");
123
+ }
124
+ return original;
125
+ }
126
+ }
127
+ function isMinifiableScript(attributes) {
128
+ if (/\ssrc\s*=/i.test(attributes)) {
129
+ return false;
130
+ }
131
+ const type = getAttributeValue(attributes, "type");
132
+ if (!type) {
133
+ return true;
134
+ }
135
+ const normalizedType = type.trim().toLowerCase();
136
+ return normalizedType === "module" || normalizedType === "text/javascript" || normalizedType === "application/javascript";
137
+ }
138
+ function scriptIsModule(attributes) {
139
+ const type = getAttributeValue(attributes, "type");
140
+ return type?.trim().toLowerCase() === "module";
141
+ }
142
+ function getAttributeValue(attributes, name) {
143
+ const pattern = new RegExp(
144
+ `(?:^|\\s)${escapeRegExp(name)}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'=<>\`]+))`,
145
+ "i"
146
+ );
147
+ const match = pattern.exec(attributes);
148
+ return match?.[1] ?? match?.[2] ?? match?.[3];
149
+ }
150
+ function protectRawTextElements(html, protectedRegions) {
151
+ return html.replace(
152
+ rawTextElementPattern,
153
+ (content) => protect(content, protectedRegions)
154
+ );
155
+ }
156
+ function protect(content, protectedRegions) {
157
+ const placeholder = `<!--astro-compress-html-fast:${protectedRegions.length}-->`;
158
+ protectedRegions.push({ placeholder, content });
159
+ return placeholder;
160
+ }
161
+ function restoreProtectedRegions(html, protectedRegions) {
162
+ return protectedRegions.reduce(
163
+ (result, region) => result.split(region.placeholder).join(region.content),
164
+ html
165
+ );
166
+ }
167
+ function conservativeWhitespacePass(html) {
168
+ return html.replace(/>\s+</g, "><").replace(/[ \t\f\r\n]{2,}/g, " ").trim();
169
+ }
170
+ function replaceMatches(html, replacements) {
171
+ return replacements.reduce(
172
+ (result, replacement) => result.replace(replacement.fullMatch, replacement.replacement),
173
+ html
174
+ );
175
+ }
176
+ function escapeRegExp(value) {
177
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
178
+ }
179
+ function toError(error, fallbackMessage) {
180
+ return error instanceof Error ? error : new Error(fallbackMessage);
181
+ }
182
+
183
+ // src/options.ts
184
+ function normalizeOptions(options = {}) {
185
+ return {
186
+ minifyHtml: options.minifyHtml ?? true,
187
+ minifyInlineJs: options.minifyInlineJs ?? false,
188
+ minifyInlineCss: options.minifyInlineCss ?? false,
189
+ failOnError: options.failOnError ?? false,
190
+ verbose: options.verbose ?? false,
191
+ target: options.target ?? "es2020",
192
+ include: options.include ?? ["**/*.html"],
193
+ exclude: options.exclude ?? []
194
+ };
195
+ }
196
+
197
+ // src/report.ts
198
+ function createSummary(results) {
199
+ return results.reduce(
200
+ (summary, result) => ({
201
+ files: summary.files + 1,
202
+ changed: summary.changed + (result.changed ? 1 : 0),
203
+ skipped: summary.skipped + (result.skipped ? 1 : 0),
204
+ originalBytes: summary.originalBytes + result.originalBytes,
205
+ compressedBytes: summary.compressedBytes + result.compressedBytes
206
+ }),
207
+ {
208
+ files: 0,
209
+ changed: 0,
210
+ skipped: 0,
211
+ originalBytes: 0,
212
+ compressedBytes: 0
213
+ }
214
+ );
215
+ }
216
+ function formatByteDelta(summary) {
217
+ const savedBytes = summary.originalBytes - summary.compressedBytes;
218
+ const percentage = summary.originalBytes === 0 ? "0.0" : (savedBytes / summary.originalBytes * 100).toFixed(1);
219
+ return `${summary.compressedBytes}/${summary.originalBytes} bytes (${savedBytes} saved, ${percentage}%)`;
220
+ }
221
+ function reportSummary(reporter, summary, verbose) {
222
+ if (!verbose && summary.changed === 0) {
223
+ return;
224
+ }
225
+ reporter.info(
226
+ `compressed ${summary.changed}/${summary.files} HTML files; ${formatByteDelta(summary)}`
227
+ );
228
+ }
229
+
230
+ // src/walkHtmlFiles.ts
231
+ import { fileURLToPath } from "url";
232
+ import { glob } from "tinyglobby";
233
+ async function walkHtmlFiles(outDir, options) {
234
+ const cwd = fileURLToPath(outDir);
235
+ const files = await glob(options.include, {
236
+ cwd,
237
+ absolute: true,
238
+ onlyFiles: true,
239
+ ignore: options.exclude
240
+ });
241
+ return files.sort();
242
+ }
243
+
244
+ // src/index.ts
245
+ function compressHtml(userOptions = {}) {
246
+ const options = normalizeOptions(userOptions);
247
+ return {
248
+ name: "astro-compress-html-fast",
249
+ hooks: {
250
+ "astro:build:done": async ({ dir, logger }) => {
251
+ const files = await walkHtmlFiles(dir, options);
252
+ const results = [];
253
+ for (const filePath of files) {
254
+ try {
255
+ const original = await readFile(filePath, "utf8");
256
+ const compressed = await minifyHtml(original, options);
257
+ const originalBytes = Buffer.byteLength(original);
258
+ const compressedBytes = Buffer.byteLength(compressed);
259
+ const shouldWrite = compressed.length > 0 && compressedBytes < originalBytes;
260
+ if (shouldWrite) {
261
+ await writeFile(filePath, compressed);
262
+ }
263
+ results.push({
264
+ filePath,
265
+ originalBytes,
266
+ compressedBytes: shouldWrite ? compressedBytes : originalBytes,
267
+ changed: shouldWrite,
268
+ skipped: false
269
+ });
270
+ } catch (error) {
271
+ const message = error instanceof Error ? error.message : "Unknown minification error";
272
+ if (options.failOnError) {
273
+ throw error;
274
+ }
275
+ logger.warn(`Could not compress ${filePath}: ${message}`);
276
+ const original = await readFile(filePath, "utf8");
277
+ const originalBytes = Buffer.byteLength(original);
278
+ results.push({
279
+ filePath,
280
+ originalBytes,
281
+ compressedBytes: originalBytes,
282
+ changed: false,
283
+ skipped: true
284
+ });
285
+ }
286
+ }
287
+ reportSummary(logger, createSummary(results), options.verbose);
288
+ }
289
+ }
290
+ };
291
+ }
292
+ export {
293
+ compressHtml as default
294
+ };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@alfred.westerveld/astro-compress-html-fast",
3
+ "version": "0.1.0",
4
+ "description": "Fast, conservative post-build HTML compression for Astro.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": ""
11
+ },
12
+ "keywords": [
13
+ "astro",
14
+ "integration",
15
+ "html",
16
+ "minify",
17
+ "compress"
18
+ ],
19
+ "sideEffects": false,
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "scripts": {
32
+ "build": "tsup src/index.ts --format esm --dts --clean",
33
+ "typecheck": "tsc --noEmit",
34
+ "test": "vitest run",
35
+ "check": "bun run typecheck && bun run test && bun run build"
36
+ },
37
+ "peerDependencies": {
38
+ "astro": ">=4 <7"
39
+ },
40
+ "dependencies": {
41
+ "@swc/html": "^1.15.41",
42
+ "esbuild": "^0.27.3",
43
+ "tinyglobby": "^0.2.15"
44
+ },
45
+ "devDependencies": {
46
+ "astro": "6.4.4",
47
+ "@types/node": "^24.10.1",
48
+ "tsup": "^8.5.1",
49
+ "typescript": "^5.9.3",
50
+ "vitest": "^4.0.15"
51
+ },
52
+ "engines": {
53
+ "node": ">=18.17.0"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public"
57
+ }
58
+ }