astro-precompressor 0.0.0-stage → 1.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/LICENCE.md +20 -0
- package/README.md +632 -2
- package/dist/build.d.ts +3 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.js +291 -0
- package/dist/build.js.map +1 -0
- package/dist/cache.d.ts +11 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +112 -0
- package/dist/cache.js.map +1 -0
- package/dist/compression.d.ts +6 -0
- package/dist/compression.d.ts.map +1 -0
- package/dist/compression.js +121 -0
- package/dist/compression.js.map +1 -0
- package/dist/constants.d.ts +34 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +145 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/integration.d.ts +4 -0
- package/dist/integration.d.ts.map +1 -0
- package/dist/integration.js +160 -0
- package/dist/integration.js.map +1 -0
- package/dist/semaphore.d.ts +8 -0
- package/dist/semaphore.d.ts.map +1 -0
- package/dist/semaphore.js +43 -0
- package/dist/semaphore.js.map +1 -0
- package/dist/types.d.ts +83 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +19 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +178 -0
- package/dist/utils.js.map +1 -0
- package/dist/worker-pool.d.ts +2 -0
- package/dist/worker-pool.d.ts.map +1 -0
- package/dist/worker-pool.js +34 -0
- package/dist/worker-pool.js.map +1 -0
- package/dist/zopfli.d.ts +9 -0
- package/dist/zopfli.d.ts.map +1 -0
- package/dist/zopfli.js +39 -0
- package/dist/zopfli.js.map +1 -0
- package/package.json +79 -3
package/LICENCE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jason P. Cochrane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
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, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,633 @@
|
|
|
1
|
-
#
|
|
1
|
+
# astro-precompressor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An Astro integration that precompresses build output using Gzip, Brotli and,
|
|
4
|
+
optionally, Zstandard.
|
|
5
|
+
|
|
6
|
+
`astro-precompressor` runs after Astro finishes building your project,
|
|
7
|
+
generating compressed sidecar files alongside eligible output files for your
|
|
8
|
+
server to use instead of on-the-fly compression. It uses incremental caching to
|
|
9
|
+
avoid unnecessary recompression between builds (the main motivation for building
|
|
10
|
+
this integration) and supports configurable algorithms, file filtering, and
|
|
11
|
+
performance.
|
|
12
|
+
|
|
13
|
+
> [!IMPORTANT] For this integration to be useful, your hosting environment must
|
|
14
|
+
> be set up for serving precompressed sidecars. See
|
|
15
|
+
> [Configuring Your Hosting Environment](#configuring-your-hosting-environment)
|
|
16
|
+
> for details.
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
Add the extension to your project with the `astro add` command.
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# npm
|
|
24
|
+
npx astro add astro-precompressor
|
|
25
|
+
|
|
26
|
+
# pnpm
|
|
27
|
+
pnpm astro add astro-precompressor
|
|
28
|
+
|
|
29
|
+
# yarn
|
|
30
|
+
yarn astro add astro-precompressor
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
This will install the module into your project, import the integration in your
|
|
34
|
+
Astro config, and add it to the end of your integrations array. The next time
|
|
35
|
+
you run your build, you should see the compression details at the end of your
|
|
36
|
+
build output.
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
generating precompressed files
|
|
40
|
+
16:40:29 ▶ index.html.gz 185.99 KiB → 65.74 KiB (-64.7%) in 431ms
|
|
41
|
+
16:40:29 ▶ index.html.br 185.99 KiB → 36.95 KiB (-80.1%) in 681ms
|
|
42
|
+
...
|
|
43
|
+
16:40:30 ✓ Completed in 2.31s
|
|
44
|
+
|
|
45
|
+
16:40:30 [astro-precompressor] Processed 71 source files using 47.47s of combined processing time.
|
|
46
|
+
16:40:30 [astro-precompressor] Reused: 0 | Updated: 71 | Generated: 142 (0 discarded)
|
|
47
|
+
16:40:30 [astro-precompressor] gzip: 71 generated | 1.74 MiB (-64.2%) | 21.46s combined processing time
|
|
48
|
+
16:40:30 [astro-precompressor] brotli: 71 generated | 1.07 MiB (-78.1%) | 26.01s combined processing time
|
|
49
|
+
16:40:30 [astro-precompressor] Errors: 0
|
|
50
|
+
16:40:30 [astro-precompressor] Done!
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
> [!NOTE] 'combined processing time' refers to the combined processing time of
|
|
54
|
+
> all compression tasks. Since these tasks run in parallel, the actual time
|
|
55
|
+
> taken for them all to complete will typically be much shorter depending on
|
|
56
|
+
> your concurrency value.
|
|
57
|
+
|
|
58
|
+
## Motivation
|
|
59
|
+
|
|
60
|
+
While various compression options exist for Astro builds, they either do not
|
|
61
|
+
offer caching or are too complicated to configure for common options like file
|
|
62
|
+
filtering. They are also either restricted to node's zlib library or require
|
|
63
|
+
command-line scripts.
|
|
64
|
+
|
|
65
|
+
This package aims to provide a straightforward configuration without sacrificing
|
|
66
|
+
performance or flexibility. The priority is faster builds and, more importantly,
|
|
67
|
+
faster rebuilds. It also offers optional Zopfli for better gzip compression, and
|
|
68
|
+
is open to adding other engines on request (please
|
|
69
|
+
[open an issue](https://github.com/jpc-ae/astro-precompressor/issues) for any
|
|
70
|
+
requests).
|
|
71
|
+
|
|
72
|
+
## Features
|
|
73
|
+
|
|
74
|
+
- **Sane defaults:** Gzip and Brotli tuned for high compression, with
|
|
75
|
+
incremental caching to avoid redundant processing; Zstandard off but available
|
|
76
|
+
if needed.
|
|
77
|
+
- **Highly configurable:** Fine-tune algorithm-specific compression settings,
|
|
78
|
+
performance options, file filtering and cleanup.
|
|
79
|
+
- **Optional Zopfli:** Install the `node-zopfli-es` package into your project
|
|
80
|
+
for smaller gzip output.
|
|
81
|
+
- **Incremental builds:** Reuse cached compression artifacts when source files
|
|
82
|
+
haven't changed, preserving cached artifact modification timestamps.
|
|
83
|
+
- **Detailed filtering:** Specify file extensions, a minimum size, and include
|
|
84
|
+
or exclude paths using glob patterns.
|
|
85
|
+
- **Compression size checks:** Discard compressed outputs that are not smaller
|
|
86
|
+
than their originals.
|
|
87
|
+
- **Detailed logging:** Choose between a basic summary, per-file statistics
|
|
88
|
+
similar to Astro's build logs, and detailed diagnostic output.
|
|
89
|
+
|
|
90
|
+
## Installation
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
# npm
|
|
94
|
+
npm install -D astro-precompressor
|
|
95
|
+
|
|
96
|
+
# pnpm
|
|
97
|
+
pnpm add -D astro-precompressor
|
|
98
|
+
|
|
99
|
+
# yarn
|
|
100
|
+
yarn add -D astro-precompressor
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Optional Zopfli
|
|
104
|
+
|
|
105
|
+
To enable the Zopfli gzip compression engine, install node-zopfli-es as a
|
|
106
|
+
development dependency in your Astro project. It will be detected automatically
|
|
107
|
+
when the integration initializes.
|
|
108
|
+
|
|
109
|
+
If installed, Zopfli is automatically used for gzip compression when no custom
|
|
110
|
+
gzip options are provided. If Zopfli is explicitly enabled, the package must be
|
|
111
|
+
installed in your Astro project or the build will fail. See
|
|
112
|
+
[Compression Algorithms](#compression-algorithms) for more details.
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
# npm
|
|
116
|
+
npm install -D node-zopfli-es
|
|
117
|
+
|
|
118
|
+
# pnpm
|
|
119
|
+
pnpm add -D node-zopfli-es
|
|
120
|
+
|
|
121
|
+
# yarn
|
|
122
|
+
yarn add -D node-zopfli-es
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
> [!TIP] The default settings used by `astro-precompressor` have been tuned for
|
|
126
|
+
> speed without sacrificing compression. Unless gzip output is not needed or
|
|
127
|
+
> there is a compelling reason to avoid the extra dependency, installing
|
|
128
|
+
> `node-zopfli-es` alongside `astro-precompressor` is highly recommended. See
|
|
129
|
+
> the Gzip section under [Algorithms](#compression-algorithms) for more details.
|
|
130
|
+
|
|
131
|
+
## Usage
|
|
132
|
+
|
|
133
|
+
Import the integration into your Astro configuration and add it as the last item
|
|
134
|
+
of the integrations array.
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
// astro.config.ts
|
|
138
|
+
import { defineConfig } from 'astro/config'
|
|
139
|
+
import precompressor from 'astro-precompressor'
|
|
140
|
+
|
|
141
|
+
export default defineConfig({
|
|
142
|
+
integrations: [
|
|
143
|
+
//...
|
|
144
|
+
precompressor(),
|
|
145
|
+
],
|
|
146
|
+
})
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
> [!CAUTION] Astro runs integrations in the order they are listed. To avoid
|
|
150
|
+
> subsequent integrations from affecting compressed files, and to ensure any
|
|
151
|
+
> files created or modified by other integrations are compressed appropriately,
|
|
152
|
+
> always ensure that this integration sits last in the list.
|
|
153
|
+
|
|
154
|
+
## Configuration
|
|
155
|
+
|
|
156
|
+
The integration accepts an optional configuration object.
|
|
157
|
+
|
|
158
|
+
| Option | Default | Description |
|
|
159
|
+
| -------------------------- | ----------------------- | --------------------------------------------------------- |
|
|
160
|
+
| `algorithms` | Gzip and Brotli enabled | Compression algorithms and settings |
|
|
161
|
+
| `filters` | Built-in file types | Files eligible for compression |
|
|
162
|
+
| `performance` | Automatic concurrency | Processing and caching behaviour |
|
|
163
|
+
| `logLevel` | 2 | Logging verbosity |
|
|
164
|
+
| `failOnError` | disabled | Whether compression failures stop the build |
|
|
165
|
+
| `recompressOnConfigChange` | disabled | Whether changes to this configuration force recompression |
|
|
166
|
+
|
|
167
|
+
Any provided options will be merged with the defaults. For example, `gzip` will
|
|
168
|
+
still be enabled with the configuration below.
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
precompressor({
|
|
172
|
+
algorithms: {
|
|
173
|
+
brotli: false,
|
|
174
|
+
zstd: true,
|
|
175
|
+
},
|
|
176
|
+
filters: {
|
|
177
|
+
minSize: 1024,
|
|
178
|
+
fileTypes: ['.html', '.css', '.js', '.svg', '.txt'],
|
|
179
|
+
include: ['www/**'],
|
|
180
|
+
exclude: ['**/*.env', '**/.*/**'],
|
|
181
|
+
},
|
|
182
|
+
logLevel: 0,
|
|
183
|
+
})
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
The following sections describe the details of each option.
|
|
187
|
+
|
|
188
|
+
### Compression Algorithms
|
|
189
|
+
|
|
190
|
+
The `algorithms` option controls which compression algorithms are enabled and
|
|
191
|
+
their individual settings. Each algorithm accepts either a boolean (`true` or
|
|
192
|
+
`false`) or an options object that will be passed through directly to the
|
|
193
|
+
compression engine. Below are the defaults.
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
algorithms: {
|
|
197
|
+
gzip: true,
|
|
198
|
+
brotli: true,
|
|
199
|
+
zstd: false,
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
> [!NOTE] The `constants` object used in the algorithm configurations below
|
|
204
|
+
> comes from the `node:zlib` package.
|
|
205
|
+
|
|
206
|
+
#### Gzip
|
|
207
|
+
|
|
208
|
+
Gzip supports two compression engines:
|
|
209
|
+
|
|
210
|
+
- **zlib:** The standard Node.js compression implementation.
|
|
211
|
+
- **Zopfli:** An optional, more computationally intensive implementation that
|
|
212
|
+
generally produces smaller gzip files. It depends on the `node-zopfli-es`
|
|
213
|
+
package.
|
|
214
|
+
|
|
215
|
+
Without `node-zopfli-es` installed, the following zlib options are used. They
|
|
216
|
+
ensure maximum compression with this engine.
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
gzip: {
|
|
220
|
+
level: constants.Z_BEST_COMPRESSION,
|
|
221
|
+
memLevel: constants.Z_MAX_MEMLEVEL,
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
When `node-zopfli-es` is installed in your Astro project, it is automatically
|
|
226
|
+
detected and used for gzip compression when no custom gzip options are provided.
|
|
227
|
+
Alternatively, Zopfli can be explicitly enabled by providing a Zopfli options
|
|
228
|
+
object, in which case the build will fail if the package is missing.
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
gzip: {
|
|
232
|
+
zopfli: {
|
|
233
|
+
numiterations: 5,
|
|
234
|
+
blocksplitting: false,
|
|
235
|
+
},
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
> [!TIP] While higher `numiterations` can produce smaller file sizes, it does so
|
|
240
|
+
> at the expense of speed. 15 (the normal default) is common, but did not
|
|
241
|
+
> meaningfully affect the final compression ratios in testing while taking 3
|
|
242
|
+
> times longer. The tests revealed that, for representative minified Astro
|
|
243
|
+
> output files, `numiterations: 5` produced files that were identical in size to
|
|
244
|
+
> `numiterations: 15`, always smaller than `zlib` while only being slightly
|
|
245
|
+
> slower than `zlib`. `blocksplitting` has also been turned off for a similar
|
|
246
|
+
> reason. Feel free to experiment if maximum gzip compression is a priority for
|
|
247
|
+
> you rather than relying on `brotli`.
|
|
248
|
+
|
|
249
|
+
Even with the package installed, Zopfli can be explicitly disabled by providing
|
|
250
|
+
custom `zlib` compression options or setting `algorithms.gzip.zopfli` to
|
|
251
|
+
`false`.
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
gzip: {
|
|
255
|
+
zopfli: false,
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
or
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
gzip: {
|
|
263
|
+
level: 1,
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
#### Brotli
|
|
268
|
+
|
|
269
|
+
The default Brotli options prioritize compression ratio.
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
brotli: {
|
|
273
|
+
params: {
|
|
274
|
+
[constants.BROTLI_PARAM_QUALITY]: constants.BROTLI_MAX_QUALITY,
|
|
275
|
+
},
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
#### Zstandard
|
|
280
|
+
|
|
281
|
+
Since Brotli generally achieves better compression ratios than Zstandard for web
|
|
282
|
+
assets (at the expense of speed) and has been available in browsers for longer,
|
|
283
|
+
it is the preferred option for precompression. Zstandard is instead an
|
|
284
|
+
attractive middleware option on server rendered pages where compression speed
|
|
285
|
+
and resource usage often matter more.
|
|
286
|
+
|
|
287
|
+
This integration is exclusively for precompressing static assets. Zstandard is
|
|
288
|
+
therefore disabled by default. It can, however, be enabled by setting its
|
|
289
|
+
property to true or providing a configuration object. The default settings for
|
|
290
|
+
`zstd` are below, prioritizing high compression ratios without overly taxing
|
|
291
|
+
your system.
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
zstd: {
|
|
295
|
+
params: {
|
|
296
|
+
[constants.ZSTD_c_compressionLevel]: 19,
|
|
297
|
+
},
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
> [!CAUTION] 19 is the highest standard compression level. Setting this higher
|
|
302
|
+
> (20-22) turns on ultra mode which is significantly more compute-intensive. If
|
|
303
|
+
> you experiment with these higher levels, consider reducing the
|
|
304
|
+
> `performance.concurrency` option to a conservative value, such as 1, and
|
|
305
|
+
> monitor memory usage. A machine with sufficient CPU and memory resources, such
|
|
306
|
+
> as a server environment, is recommended.
|
|
307
|
+
|
|
308
|
+
### File Filtering
|
|
309
|
+
|
|
310
|
+
The `filters` option controls which build output files should be compressed. Its
|
|
311
|
+
default options are shown below.
|
|
312
|
+
|
|
313
|
+
```ts
|
|
314
|
+
WEB_FILE_TYPES = [
|
|
315
|
+
'.html',
|
|
316
|
+
'.htm',
|
|
317
|
+
'.css',
|
|
318
|
+
'.js',
|
|
319
|
+
'.mjs',
|
|
320
|
+
'.cjs',
|
|
321
|
+
]
|
|
322
|
+
|
|
323
|
+
DATA_FILE_TYPES = [
|
|
324
|
+
'.json',
|
|
325
|
+
'.xml',
|
|
326
|
+
'.rss',
|
|
327
|
+
'.atom',
|
|
328
|
+
]
|
|
329
|
+
|
|
330
|
+
FONT_FILE_TYPES = [
|
|
331
|
+
'.ttf',
|
|
332
|
+
'.otf',
|
|
333
|
+
'.eot',
|
|
334
|
+
]
|
|
335
|
+
|
|
336
|
+
filters: {
|
|
337
|
+
minSize: 150,
|
|
338
|
+
fileTypes: [
|
|
339
|
+
...WEB_FILE_TYPES,
|
|
340
|
+
...DATA_FILE_TYPES,
|
|
341
|
+
...FONT_FILE_TYPES,
|
|
342
|
+
'.txt',
|
|
343
|
+
'.svg',
|
|
344
|
+
],
|
|
345
|
+
include: undefined,
|
|
346
|
+
exclude: undefined,
|
|
347
|
+
pruneCache: true,
|
|
348
|
+
}
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
| Option | Description |
|
|
352
|
+
| ------------ | --------------------------------------------------------- |
|
|
353
|
+
| `minSize` | Minimum source file size in bytes. |
|
|
354
|
+
| `fileTypes` | File extensions eligible for compression. |
|
|
355
|
+
| `include` | Glob patterns specifying files to include. |
|
|
356
|
+
| `exclude` | Glob patterns specifying files or directories to exclude. |
|
|
357
|
+
| `pruneCache` | Remove obsolete cached artifacts and manifest entries. |
|
|
358
|
+
|
|
359
|
+
If `fileTypes` is specified, the default types are replaced completely. For
|
|
360
|
+
easier configuration, the `WEB_FILE_TYPES`, `DATA_FILE_TYPES` and
|
|
361
|
+
`FONT_FILE_TYPES` constants can be imported as below.
|
|
362
|
+
|
|
363
|
+
```ts
|
|
364
|
+
// astro.config.ts
|
|
365
|
+
import { defineConfig } from 'astro/config'
|
|
366
|
+
import precompressor from 'astro-precompressor'
|
|
367
|
+
import { WEB_FILE_TYPES, DATA_FILE_TYPES } from 'astro-precompressor'
|
|
368
|
+
|
|
369
|
+
export default defineConfig({
|
|
370
|
+
integrations: [
|
|
371
|
+
precompressor({
|
|
372
|
+
filters: {
|
|
373
|
+
fileTypes: [...WEB_FILE_TYPES, ...DATA_FILE_TYPES, '.txt'],
|
|
374
|
+
},
|
|
375
|
+
}),
|
|
376
|
+
],
|
|
377
|
+
})
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
Include and exclude patterns use
|
|
381
|
+
[picomatch](https://github.com/micromatch/picomatch).
|
|
382
|
+
|
|
383
|
+
Patterns are matched against paths relative to the output directory and use
|
|
384
|
+
forward slashes.
|
|
385
|
+
|
|
386
|
+
For example:
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
exclude: ['drafts/**', '**/ignored/**']
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
The first pattern excludes files beneath the root-level `drafts` directory. The
|
|
393
|
+
second excludes any directory named `ignored`.
|
|
394
|
+
|
|
395
|
+
Files are matched in the following order:
|
|
396
|
+
|
|
397
|
+
1. If include patterns are configured, files that don't match any of them are
|
|
398
|
+
skipped.
|
|
399
|
+
2. Files matching an exclude pattern are skipped.
|
|
400
|
+
3. Files with extensions not listed in fileTypes are skipped.
|
|
401
|
+
4. Files smaller than minSize are skipped.
|
|
402
|
+
|
|
403
|
+
### Performance
|
|
404
|
+
|
|
405
|
+
The `performance` option controls concurrent processing and whether compression
|
|
406
|
+
results should be retained in the build output. The defaults are below.
|
|
407
|
+
|
|
408
|
+
```ts
|
|
409
|
+
performance: {
|
|
410
|
+
concurrency: availableParallelism(),
|
|
411
|
+
incremental: true,
|
|
412
|
+
discardCompressionIfLarger: true,
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
> [!NOTE] `availableParallelism` comes from the `node:os` package
|
|
417
|
+
|
|
418
|
+
| Option | Description |
|
|
419
|
+
| ---------------------------- | -------------------------------------------------------------------------------------------- |
|
|
420
|
+
| `concurrency` | Maximum number of compression operations running concurrently. |
|
|
421
|
+
| `incremental` | Whether to retain cached copies of compressed files for reuse when source file hashes match. |
|
|
422
|
+
| `discardCompressionIfLarger` | Whether to discard compressed output when the original is smaller. |
|
|
423
|
+
|
|
424
|
+
The default value for `concurrency` is set automatically based on the number of
|
|
425
|
+
logical processors that Node.js reports as available. This is usually optimal,
|
|
426
|
+
but lower values may use less memory at the expense of speed.
|
|
427
|
+
|
|
428
|
+
When `incremental` is enabled, compression artifacts are retained in the cache
|
|
429
|
+
along with a hash of the source file. While this may marginally slow down
|
|
430
|
+
initial builds, subsequent builds will be much faster as long as there are a
|
|
431
|
+
large number of unchanged source files. Especially for large sites and
|
|
432
|
+
incremental builds, this can shorten build times tremendously. See
|
|
433
|
+
[Incremental Caching](#incremental-caching) below for more details.
|
|
434
|
+
|
|
435
|
+
When `discardCompressionIfLarger` is enabled, each compression algorithm is
|
|
436
|
+
evaluated independently. Oversized compression artifacts are retained in the
|
|
437
|
+
cache to avoid recompressing the same files on subsequent builds, but are not
|
|
438
|
+
copied into the build output.
|
|
439
|
+
|
|
440
|
+
## Incremental Caching
|
|
441
|
+
|
|
442
|
+
The integration maintains a cache inside Astro's configured cache directory.
|
|
443
|
+
|
|
444
|
+
Cached compression artifacts are associated with their source files using
|
|
445
|
+
content hashes. When a source file hasn't changed, its cached artifacts can be
|
|
446
|
+
reused instead of running the compression engine again.
|
|
447
|
+
|
|
448
|
+
The cache is maintained independently of the build output, allowing compressed
|
|
449
|
+
sidecars to be restored along with their modification timestamps.
|
|
450
|
+
|
|
451
|
+
When `filters.pruneCache` is enabled, obsolete artifacts associated with files
|
|
452
|
+
that no longer exist in the output directory are removed.
|
|
453
|
+
|
|
454
|
+
## Output
|
|
455
|
+
|
|
456
|
+
For each eligible source file, the integration generates one sidecar per enabled
|
|
457
|
+
compression algorithm.
|
|
458
|
+
|
|
459
|
+
For example, given:
|
|
460
|
+
|
|
461
|
+
```text
|
|
462
|
+
dist/
|
|
463
|
+
├─ index.html
|
|
464
|
+
├─ assets/
|
|
465
|
+
│ ├─ scripts.js
|
|
466
|
+
│ └─ styles.css
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
The resulting output may look like:
|
|
470
|
+
|
|
471
|
+
```text
|
|
472
|
+
dist/
|
|
473
|
+
├─ index.html
|
|
474
|
+
├─ index.html.gz
|
|
475
|
+
├─ index.html.br
|
|
476
|
+
├─ assets/
|
|
477
|
+
│ ├─ scripts.js
|
|
478
|
+
│ ├─ scripts.js.gz
|
|
479
|
+
│ ├─ scripts.js.br
|
|
480
|
+
│ ├─ styles.css
|
|
481
|
+
│ ├─ styles.css.gz
|
|
482
|
+
│ └─ styles.css.br
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
The original files are never modified or replaced.
|
|
486
|
+
|
|
487
|
+
## Logging
|
|
488
|
+
|
|
489
|
+
Three levels of logging detail are available:
|
|
490
|
+
|
|
491
|
+
1. **Summary (`logLevel: 0`):** A concise build summary along with any errors.
|
|
492
|
+
2. **Detailed (`logLevel: 1`):** Per-file processing information, cache status,
|
|
493
|
+
compression statistics, and a more detailed summary.
|
|
494
|
+
3. **Debug (`logLevel: 2`):** Additional diagnostic messages explaining the
|
|
495
|
+
individual processing steps.
|
|
496
|
+
|
|
497
|
+
Even when `logLevel` is set to `2`, debug messages will only be shown when the
|
|
498
|
+
`--verbose` flag is added to the `astro build` command. The default level is `2`
|
|
499
|
+
to match Astro's own logging conventions.
|
|
500
|
+
|
|
501
|
+
> [!TIP] Set this value to 0 if you prefer concise logs.
|
|
502
|
+
|
|
503
|
+
```ts
|
|
504
|
+
logLevel: 0
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
## Build Lifecycle
|
|
508
|
+
|
|
509
|
+
The integration uses Astro's build lifecycle hooks:
|
|
510
|
+
|
|
511
|
+
- `astro:config:setup`: Initializes the integration, resolves configuration, and
|
|
512
|
+
prepares the cache. It also waits for `node-zopfli-es` to load if available or
|
|
513
|
+
requested (see [Optional Zopfli](#optional-zopfli)).
|
|
514
|
+
- `astro:build:done`: Processes the completed build output and generates
|
|
515
|
+
compressed artifacts.
|
|
516
|
+
|
|
517
|
+
> [!WARNING] If zopfli is explicitly enabled in your configuration without
|
|
518
|
+
> `node-zopfli-es` being installed, your Astro build will fail at the setup
|
|
519
|
+
> stage.
|
|
520
|
+
|
|
521
|
+
## Configuring Your Hosting Environment
|
|
522
|
+
|
|
523
|
+
> [!IMPORTANT] Generating compressed sidecars does not automatically configure
|
|
524
|
+
> your hosting environment to serve them. Your web server, CDN, or hosting
|
|
525
|
+
> provider must support content negotiation and serve the appropriate sidecar
|
|
526
|
+
> when a client advertises support for the algorithm.
|
|
527
|
+
|
|
528
|
+
### Apache
|
|
529
|
+
|
|
530
|
+
Apache's `rewrite_module` (`mod_rewrite.c`) can be used to redirect browsers to
|
|
531
|
+
the sidecars. Here is a sample `.htaccess` configuration that will work for most
|
|
532
|
+
sites with the default options enabled. It does not include a section for
|
|
533
|
+
Zstandard, since that algorithm is disabled by default.
|
|
534
|
+
|
|
535
|
+
```apacheconf
|
|
536
|
+
# SERVE COMPRESSED FILES
|
|
537
|
+
|
|
538
|
+
## brotli
|
|
539
|
+
RewriteCond %{HTTP:Accept-encoding} \bbr\b(?!\s*;\s*q=0(?:\.0{1,3})?(?=\s*(?:,|$))) [NC]
|
|
540
|
+
RewriteCond %{REQUEST_URI} \.(html?|css|(c|m)?js|svg|json|xml|rss|atom|txt|ttf|otf|eot)$ [NC]
|
|
541
|
+
RewriteCond %{REQUEST_FILENAME}.br -s [NC]
|
|
542
|
+
RewriteRule ^(.+) $1.br
|
|
543
|
+
<FilesMatch "\.(html?|css|(c|m)?js|svg|json|xml|rss|atom|txt|ttf|otf|eot)\.br$">
|
|
544
|
+
### Prevent Apache from interpreting .br as a language extension
|
|
545
|
+
RemoveLanguage .br
|
|
546
|
+
Header merge Content-Encoding br
|
|
547
|
+
</FilesMatch>
|
|
548
|
+
|
|
549
|
+
## gzip
|
|
550
|
+
RewriteCond %{HTTP:Accept-encoding} \bgzip\b(?!\s*;\s*q=0(?:\.0{1,3})?(?=\s*(?:,|$))) [NC]
|
|
551
|
+
RewriteCond %{REQUEST_URI} \.(html?|css|(c|m)?js|svg|json|xml|rss|atom|txt|ttf|otf|eot)$ [NC]
|
|
552
|
+
RewriteCond %{REQUEST_FILENAME}.gz -s [NC]
|
|
553
|
+
RewriteRule ^(.+) $1.gz
|
|
554
|
+
<FilesMatch "\.(html?|css|(c|m)?js|svg|json|xml|rss|atom|txt|ttf|otf|eot)\.gz$">
|
|
555
|
+
Header merge Content-Encoding gzip
|
|
556
|
+
</FilesMatch>
|
|
557
|
+
|
|
558
|
+
## mime types
|
|
559
|
+
RewriteRule \.html?\.(br|gz)$ - [T=text/html;charset=UTF-8,E=no-brotli:1,E=no-gzip:1]
|
|
560
|
+
RewriteRule \.css\.(br|gz)$ - [T=text/css;charset=UTF-8,E=no-brotli:1,E=no-gzip:1]
|
|
561
|
+
RewriteRule \.(c|m)?js\.(br|gz)$ - [T=text/javascript;charset=UTF-8,E=no-brotli:1,E=no-gzip:1]
|
|
562
|
+
RewriteRule \.svg\.(br|gz)$ - [T=image/svg+xml;charset=UTF-8,E=no-brotli:1,E=no-gzip:1]
|
|
563
|
+
RewriteRule \.json\.(br|gz)$ - [T=application/json;charset=UTF-8,E=no-brotli:1,E=no-gzip:1]
|
|
564
|
+
RewriteRule \.xml\.(br|gz)$ - [T=application/xml;charset=UTF-8,E=no-brotli:1,E=no-gzip:1]
|
|
565
|
+
RewriteRule \.rss\.(br|gz)$ - [T=application/rss+xml;charset=UTF-8,E=no-brotli:1,E=no-gzip:1]
|
|
566
|
+
RewriteRule \.atom\.(br|gz)$ - [T=application/atom+xml;charset=UTF-8,E=no-brotli:1,E=no-gzip:1]
|
|
567
|
+
RewriteRule \.txt\.(br|gz)$ - [T=text/plain;charset=UTF-8,E=no-brotli:1,E=no-gzip:1]
|
|
568
|
+
RewriteRule \.ttf\.(br|gz)$ - [T=font/ttf,E=no-brotli:1,E=no-gzip:1]
|
|
569
|
+
RewriteRule \.otf\.(br|gz)$ - [T=font/otf,E=no-brotli:1,E=no-gzip:1]
|
|
570
|
+
RewriteRule \.eot\.(br|gz)$ - [T=application/vnd.ms-fontobject,E=no-brotli:1,E=no-gzip:1]
|
|
571
|
+
|
|
572
|
+
## cache compressed files separately
|
|
573
|
+
<FilesMatch "\.(html?|css|(c|m)?js|svg|json|xml|rss|atom|txt|ttf|otf|eot)(\.(br|gz))?$">
|
|
574
|
+
Header merge Vary Accept-Encoding
|
|
575
|
+
</FilesMatch>
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
> [!TIP] Adjust the file types above to match your `astro-precompressor`
|
|
579
|
+
> configuration.
|
|
580
|
+
|
|
581
|
+
> [!WARNING] The `.htaccess` configuration above is representative and does not
|
|
582
|
+
> guard against a missing `mod_rewrite`. It assumes `mod_headers` and `mod_mime`
|
|
583
|
+
> are available. The encoding conditions reject explicitly disabled encodings
|
|
584
|
+
> (q=0), but do not implement complete quality-value negotiation or wildcard
|
|
585
|
+
> handling. Always test before using it in a production environment.
|
|
586
|
+
|
|
587
|
+
### Nginx
|
|
588
|
+
|
|
589
|
+
Serving precompressed sidecars requires `ngx_http_gzip_static_module` for gzip,
|
|
590
|
+
`ngx_http_brotli_static_module` for Brotli and `ngx_http_zstd_static_module` for
|
|
591
|
+
Zstandard. Ensure the required algorithm modules are loaded, and then enable
|
|
592
|
+
them in a server location block.
|
|
593
|
+
|
|
594
|
+
```nginx
|
|
595
|
+
server {
|
|
596
|
+
listen 80;
|
|
597
|
+
server_name example.com;
|
|
598
|
+
root /var/www/html;
|
|
599
|
+
|
|
600
|
+
location / {
|
|
601
|
+
brotli_static on;
|
|
602
|
+
zstd_static on;
|
|
603
|
+
gzip_static on;
|
|
604
|
+
|
|
605
|
+
# cache precompressed files separately
|
|
606
|
+
gzip_vary on;
|
|
607
|
+
|
|
608
|
+
# fallback
|
|
609
|
+
try_files $uri $uri/ =404;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
> [!IMPORTANT] Zstandard support is not included as standard in many Nginx
|
|
615
|
+
> installations. A separately built or third-party module may be required when
|
|
616
|
+
> using this algorithm in your environment.
|
|
617
|
+
|
|
618
|
+
### Caddy
|
|
619
|
+
|
|
620
|
+
Simply add the `precompressed` directive inside your `file_server` block.
|
|
621
|
+
|
|
622
|
+
```caddyfile
|
|
623
|
+
example.com {
|
|
624
|
+
root * /var/www/html
|
|
625
|
+
file_server {
|
|
626
|
+
precompressed br zstd gzip
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
## License
|
|
632
|
+
|
|
633
|
+
See the repository [licence](./LICENCE.md) file.
|
package/dist/build.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Logger, ResolvedOptions, ZopfliModule } from './types.js';
|
|
2
|
+
export declare function runBuild(outputDirectory: string, cacheDirectory: string, configHash: string, options: ResolvedOptions, logger: Logger, zopfli: ZopfliModule | undefined): Promise<void>;
|
|
3
|
+
//# sourceMappingURL=build.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAiCA,OAAO,KAAK,EAIX,MAAM,EACN,eAAe,EACf,YAAY,EACZ,MAAM,YAAY,CAAA;AA4JnB,wBAAsB,QAAQ,CAC7B,eAAe,EAAE,MAAM,EACvB,cAAc,EAAE,MAAM,EACtB,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,eAAe,EACxB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,YAAY,GAAG,SAAS,GAC9B,OAAO,CAAC,IAAI,CAAC,CA4Pf"}
|