astro-precompressor 1.1.0 → 1.2.1
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 +208 -59
- package/dist/build.d.ts.map +1 -1
- package/dist/build.js +92 -18
- package/dist/build.js.map +1 -1
- package/dist/cache.d.ts +3 -3
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +17 -19
- package/dist/cache.js.map +1 -1
- package/dist/compression.d.ts +2 -2
- package/dist/compression.d.ts.map +1 -1
- package/dist/compression.js +4 -7
- package/dist/compression.js.map +1 -1
- package/dist/gzip.d.ts +2 -2
- package/dist/gzip.d.ts.map +1 -1
- package/dist/gzip.js +2 -2
- package/dist/gzip.js.map +1 -1
- package/dist/integration.d.ts.map +1 -1
- package/dist/integration.js +64 -5
- package/dist/integration.js.map +1 -1
- package/dist/types.d.ts +17 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +3 -2
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +32 -29
- package/dist/utils.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
An Astro integration that precompresses build output using Gzip, Brotli and,
|
|
4
4
|
optionally, Zstandard.
|
|
5
5
|
|
|
6
|
+
[](https://github.com/jpc-ae/astro-precompressor/actions/workflows/test.yml)
|
|
7
|
+
|
|
8
|
+
[](https://www.npmjs.com/package/astro-precompressor)
|
|
9
|
+
[](LICENSE)
|
|
10
|
+
|
|
6
11
|
`astro-precompressor` runs after Astro finishes building your project,
|
|
7
12
|
generating compressed sidecar files alongside eligible output files for your
|
|
8
13
|
server to use instead of on-the-fly compression. It uses incremental caching to
|
|
@@ -10,14 +15,15 @@ avoid unnecessary recompression between builds (the main motivation for building
|
|
|
10
15
|
this integration) and supports configurable algorithms, file filtering, and
|
|
11
16
|
performance.
|
|
12
17
|
|
|
13
|
-
> [!IMPORTANT]
|
|
18
|
+
> [!IMPORTANT]
|
|
19
|
+
> For this integration to be useful, your hosting environment must
|
|
14
20
|
> be set up for serving precompressed sidecars. See
|
|
15
21
|
> [Configuring Your Hosting Environment](#configuring-your-hosting-environment)
|
|
16
22
|
> for suggestions on how to do this.
|
|
17
23
|
|
|
18
24
|
## Quick Start
|
|
19
25
|
|
|
20
|
-
Add the
|
|
26
|
+
Add the integration to your project with the `astro add` command.
|
|
21
27
|
|
|
22
28
|
```bash
|
|
23
29
|
# npm
|
|
@@ -49,7 +55,8 @@ build, you should see compression statistics in your build output.
|
|
|
49
55
|
16:40:30 [astro-precompressor] Done!
|
|
50
56
|
```
|
|
51
57
|
|
|
52
|
-
> [!NOTE]
|
|
58
|
+
> [!NOTE]
|
|
59
|
+
> 'combined processing time' refers to the combined processing time of
|
|
53
60
|
> all compression tasks. Since these tasks run in parallel, the actual time
|
|
54
61
|
> taken for them all to complete will typically be much shorter depending on
|
|
55
62
|
> your concurrency value.
|
|
@@ -62,8 +69,9 @@ filtering. They are also either restricted to node's zlib library or require
|
|
|
62
69
|
command-line scripts.
|
|
63
70
|
|
|
64
71
|
This package aims to be straightforward without sacrificing performance or
|
|
65
|
-
flexibility. It offers optional Zopfli for better gzip compression
|
|
66
|
-
|
|
72
|
+
flexibility. It offers optional Zopfli for better gzip compression as well as a
|
|
73
|
+
fallback engine for Zstandard on older Node.js builds. Support for other
|
|
74
|
+
compression engines can be added on request (please
|
|
67
75
|
[open an issue](https://github.com/jpc-ae/astro-precompressor/issues) for any
|
|
68
76
|
requests). Faster rebuilds using an incremental cache is the priority.
|
|
69
77
|
|
|
@@ -122,7 +130,8 @@ pnpm add -D node-zopfli-es
|
|
|
122
130
|
yarn add -D node-zopfli-es
|
|
123
131
|
```
|
|
124
132
|
|
|
125
|
-
> [!TIP]
|
|
133
|
+
> [!TIP]
|
|
134
|
+
> The default zopfli settings used by `astro-precompressor` have been
|
|
126
135
|
> tuned for speed without sacrificing compression. If smaller gzip files are a
|
|
127
136
|
> priority, installing `node-zopfli-es` alongside `astro-precompressor` is
|
|
128
137
|
> highly recommended. See the Gzip section under
|
|
@@ -146,7 +155,8 @@ export default defineConfig({
|
|
|
146
155
|
})
|
|
147
156
|
```
|
|
148
157
|
|
|
149
|
-
> [!CAUTION]
|
|
158
|
+
> [!CAUTION]
|
|
159
|
+
> Astro runs integrations in the order they are listed. To avoid
|
|
150
160
|
> subsequent integrations from affecting compressed files, and to ensure any
|
|
151
161
|
> files created or modified by other integrations are compressed appropriately,
|
|
152
162
|
> always ensure that this integration sits last in the list.
|
|
@@ -155,14 +165,15 @@ export default defineConfig({
|
|
|
155
165
|
|
|
156
166
|
The integration accepts an optional configuration object.
|
|
157
167
|
|
|
158
|
-
| Option | Default | Description
|
|
159
|
-
| -------------------------- | ------------------------------------------------------- |
|
|
160
|
-
| `algorithms` | Gzip, Brotli enabled; Zstandard disabled | Compression algorithms and their settings
|
|
161
|
-
| `
|
|
162
|
-
| `
|
|
163
|
-
| `
|
|
164
|
-
| `
|
|
165
|
-
| `
|
|
168
|
+
| Option | Default | Description |
|
|
169
|
+
| -------------------------- | ------------------------------------------------------- | --------------------------------------------------------- |
|
|
170
|
+
| `algorithms` | Gzip, Brotli enabled; Zstandard disabled | Compression algorithms and their settings |
|
|
171
|
+
| `performance` | System-derived concurrency; incremental caching enabled | Concurrency, incremental processing, and output behaviour |
|
|
172
|
+
| `cache` | Pruning enabled | Cache maintenance and pruning |
|
|
173
|
+
| `filters` | Built-in filetypes; 150-byte minimum | File eligibility and per-file algorithm selection |
|
|
174
|
+
| `logLevel` | 2 (diagnostics available) | Logging verbosity |
|
|
175
|
+
| `failOnError` | false | Whether compression errors fail the build |
|
|
176
|
+
| `recompressOnConfigChange` | false | Whether configuration changes trigger recompression |
|
|
166
177
|
|
|
167
178
|
Any provided options will be merged with the defaults. For example, `gzip` will
|
|
168
179
|
still be enabled with the configuration below.
|
|
@@ -173,11 +184,14 @@ precompressor({
|
|
|
173
184
|
brotli: false,
|
|
174
185
|
zstd: true,
|
|
175
186
|
},
|
|
187
|
+
cache: {
|
|
188
|
+
prune: true,
|
|
189
|
+
},
|
|
176
190
|
filters: {
|
|
177
191
|
minSize: 1024,
|
|
178
192
|
fileTypes: ['.html', '.css', '.js', '.svg', '.txt'],
|
|
179
|
-
include: ['www/**'],
|
|
180
|
-
exclude: ['**/*.env', '**/.*/**'],
|
|
193
|
+
include: ['www/**', { pattern: '**/*.svg', algorithms: ['brotli'] }],
|
|
194
|
+
exclude: ['**/*.env', '**/.*/**', { pattern: '**/*.css', algorithms: ['zstd'] }],
|
|
181
195
|
},
|
|
182
196
|
logLevel: 0,
|
|
183
197
|
})
|
|
@@ -200,10 +214,11 @@ algorithms: {
|
|
|
200
214
|
}
|
|
201
215
|
```
|
|
202
216
|
|
|
203
|
-
> [!NOTE]
|
|
217
|
+
> [!NOTE]
|
|
218
|
+
> The `constants` object used in the algorithm configurations below
|
|
204
219
|
> comes from the `node:zlib` package.
|
|
205
220
|
|
|
206
|
-
|
|
221
|
+
**Gzip**
|
|
207
222
|
|
|
208
223
|
Gzip supports two compression engines:
|
|
209
224
|
|
|
@@ -236,14 +251,15 @@ gzip: {
|
|
|
236
251
|
}
|
|
237
252
|
```
|
|
238
253
|
|
|
239
|
-
> [!TIP]
|
|
254
|
+
> [!TIP]
|
|
255
|
+
> While higher `numiterations` can produce smaller file sizes, it does so
|
|
240
256
|
> at the expense of speed. In testing representative minified Astro output
|
|
241
257
|
> files, setting it to `5` produced files that were identical in size to `15`
|
|
242
258
|
> (the engine default) while being three times faster. `blocksplitting` has also
|
|
243
|
-
> been turned off for a similar reason. These settings aim to provide
|
|
244
|
-
>
|
|
245
|
-
>
|
|
246
|
-
>
|
|
259
|
+
> been turned off for a similar reason. These settings aim to provide meaningful
|
|
260
|
+
> savings over standard gzip while avoiding the extreme processing time
|
|
261
|
+
> associated with higher Zopfli iteration counts. Feel free to experiment if
|
|
262
|
+
> maximum gzip compression is a priority for you rather than relying on
|
|
247
263
|
> `brotli`.
|
|
248
264
|
|
|
249
265
|
Even with the `node-zopfli-es` package installed, Zopfli can be explicitly
|
|
@@ -264,7 +280,7 @@ gzip: {
|
|
|
264
280
|
}
|
|
265
281
|
```
|
|
266
282
|
|
|
267
|
-
|
|
283
|
+
**Brotli**
|
|
268
284
|
|
|
269
285
|
The default Brotli options prioritize compression ratio.
|
|
270
286
|
|
|
@@ -276,7 +292,7 @@ brotli: {
|
|
|
276
292
|
}
|
|
277
293
|
```
|
|
278
294
|
|
|
279
|
-
|
|
295
|
+
**Zstandard**
|
|
280
296
|
|
|
281
297
|
Since Brotli generally achieves better compression ratios than Zstandard for web
|
|
282
298
|
assets and has been available in browsers for longer, it is the preferred option
|
|
@@ -298,7 +314,8 @@ zstd: {
|
|
|
298
314
|
}
|
|
299
315
|
```
|
|
300
316
|
|
|
301
|
-
> [!CAUTION]
|
|
317
|
+
> [!CAUTION]
|
|
318
|
+
> 19 is the highest standard compression level. Setting this higher
|
|
302
319
|
> (20–22) turns on ultra mode which is significantly more compute-intensive. If
|
|
303
320
|
> you experiment with these higher levels, consider reducing the
|
|
304
321
|
> `performance.concurrency` option to a conservative value, such as 1, and
|
|
@@ -310,9 +327,29 @@ using Astro 5 with an older `node` version, Zstandard output can still be
|
|
|
310
327
|
created by installing the `zstd-napi` package which `astro-precompressor` will
|
|
311
328
|
then automatically use as a fallback.
|
|
312
329
|
|
|
330
|
+
### Cache
|
|
331
|
+
|
|
332
|
+
The `cache` group controls how the cache should be managed when incremental
|
|
333
|
+
compression is enabled. Its default options are shown below.
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
cache: {
|
|
337
|
+
prune: true,
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
| Option | Description |
|
|
342
|
+
| ------- | ------------------------------------------------------ |
|
|
343
|
+
| `prune` | Remove obsolete cached artifacts and manifest entries. |
|
|
344
|
+
|
|
345
|
+
> [!NOTE]
|
|
346
|
+
> Prior to v1.2.0, the `prune` option was controlled by setting
|
|
347
|
+
> `pruneCache` under the `filters` property. It has been moved to its own group
|
|
348
|
+
> for clarity.
|
|
349
|
+
|
|
313
350
|
### File Filtering
|
|
314
351
|
|
|
315
|
-
The `filters`
|
|
352
|
+
The `filters` group controls which build output files should be compressed. Its
|
|
316
353
|
default options are shown below.
|
|
317
354
|
|
|
318
355
|
```ts
|
|
@@ -350,18 +387,16 @@ filters: {
|
|
|
350
387
|
],
|
|
351
388
|
include: undefined,
|
|
352
389
|
exclude: undefined,
|
|
353
|
-
pruneCache: true,
|
|
354
390
|
}
|
|
355
391
|
// ...
|
|
356
392
|
```
|
|
357
393
|
|
|
358
|
-
| Option
|
|
359
|
-
|
|
|
360
|
-
| `minSize`
|
|
361
|
-
| `fileTypes`
|
|
362
|
-
| `include`
|
|
363
|
-
| `exclude`
|
|
364
|
-
| `pruneCache` | Remove obsolete cached artifacts and manifest entries. |
|
|
394
|
+
| Option | Description |
|
|
395
|
+
| ----------- | --------------------------------------------------------- |
|
|
396
|
+
| `minSize` | Minimum source file size in bytes. |
|
|
397
|
+
| `fileTypes` | File extensions eligible for compression. |
|
|
398
|
+
| `include` | Glob patterns specifying files to include. |
|
|
399
|
+
| `exclude` | Glob patterns specifying files or directories to exclude. |
|
|
365
400
|
|
|
366
401
|
If `fileTypes` is specified, the default types are replaced completely. For
|
|
367
402
|
easier configuration, the `WEB_FILE_TYPES`, `DATA_FILE_TYPES` and
|
|
@@ -384,28 +419,129 @@ export default defineConfig({
|
|
|
384
419
|
})
|
|
385
420
|
```
|
|
386
421
|
|
|
422
|
+
Files are evaluated against the global filters first:
|
|
423
|
+
|
|
424
|
+
1. Files with extensions not listed in `fileTypes` are skipped.
|
|
425
|
+
2. Files smaller than `minSize` are skipped.
|
|
426
|
+
3. Files matching any string exclude pattern are skipped.
|
|
427
|
+
4. If include patterns are configured, files that don't match any include
|
|
428
|
+
pattern are skipped.
|
|
429
|
+
|
|
430
|
+
For files that pass these checks, matching include and exclude objects determine
|
|
431
|
+
which globally enabled compression algorithms are used.
|
|
432
|
+
|
|
433
|
+
**Pattern Matching**
|
|
434
|
+
|
|
387
435
|
Include and exclude patterns use
|
|
388
436
|
[picomatch](https://github.com/micromatch/picomatch).
|
|
389
437
|
|
|
390
438
|
Patterns are matched against paths relative to the output directory and use
|
|
391
439
|
forward slashes.
|
|
392
440
|
|
|
393
|
-
|
|
441
|
+
Each entry can be either a string representing a glob pattern or an object
|
|
442
|
+
specifying the algorithms to which a pattern applies.
|
|
394
443
|
|
|
395
444
|
```ts
|
|
396
|
-
|
|
445
|
+
filters: {
|
|
446
|
+
include: [
|
|
447
|
+
'**/*.html',
|
|
448
|
+
{ pattern: '**/*.css', algorithms: ['gzip'] },
|
|
449
|
+
{ pattern: '**/*.svg', algorithms: ['brotli'] },
|
|
450
|
+
],
|
|
451
|
+
exclude: [
|
|
452
|
+
'**/admin/**',
|
|
453
|
+
{ pattern: '**/*.css', algorithms: ['gzip', 'zstd'] },
|
|
454
|
+
],
|
|
455
|
+
}
|
|
397
456
|
```
|
|
398
457
|
|
|
399
|
-
|
|
400
|
-
|
|
458
|
+
A string pattern applies to all globally enabled algorithms. An object pattern
|
|
459
|
+
applies only to the algorithms listed in its `algorithms` array.
|
|
460
|
+
|
|
461
|
+
> [!IMPORTANT]
|
|
462
|
+
> The top-level `algorithms` and `filters.fileTypes` options are
|
|
463
|
+
> authoritative. Glob filters can restrict which globally enabled algorithms are
|
|
464
|
+
> used for a file, but cannot enable an algorithm that is globally disabled.
|
|
465
|
+
> Likewise, include patterns cannot make a filetype eligible if it is excluded
|
|
466
|
+
> by `filters.fileTypes`.
|
|
467
|
+
|
|
468
|
+
**Include Rules**
|
|
469
|
+
|
|
470
|
+
- If no include patterns are configured, all files that pass the other filters
|
|
471
|
+
are eligible for every globally enabled algorithm.
|
|
472
|
+
- If include patterns are configured, a file must match at least one include
|
|
473
|
+
pattern.
|
|
474
|
+
- A string include pattern makes the file eligible for every globally enabled
|
|
475
|
+
algorithm.
|
|
476
|
+
- An object include pattern makes the file eligible for the algorithms listed in
|
|
477
|
+
its `algorithms` array.
|
|
478
|
+
- If multiple include patterns match, their algorithm selections are combined.
|
|
479
|
+
If any matching pattern is a string, all globally enabled algorithms are
|
|
480
|
+
eligible before exclusions are applied.
|
|
481
|
+
|
|
482
|
+
**Exclude Rules**
|
|
483
|
+
|
|
484
|
+
- A file matching any string exclude pattern is excluded entirely.
|
|
485
|
+
- An object exclude pattern removes only the algorithms listed in its
|
|
486
|
+
`algorithms` array.
|
|
487
|
+
- Multiple exclude patterns are applied independently. If any matching rule
|
|
488
|
+
excludes an algorithm, that algorithm is not used.
|
|
489
|
+
- Exclusions take precedence over inclusions.
|
|
490
|
+
|
|
491
|
+
**Combining Pattern Rules**
|
|
492
|
+
|
|
493
|
+
The following table illustrates how matching include and exclude rules are
|
|
494
|
+
combined. It assumes all three algorithms are globally enabled and the file
|
|
495
|
+
passes the `fileTypes` and `minSize` filters.
|
|
496
|
+
|
|
497
|
+
| Matching include rules | Matching exclude rules | Result |
|
|
498
|
+
| ------------------------------------------- | -------------------------- | ----------------------- |
|
|
499
|
+
| No include patterns configured | None | Gzip, Brotli, Zstandard |
|
|
500
|
+
| String pattern | None | Gzip, Brotli, Zstandard |
|
|
501
|
+
| Object specifying ['gzip'] | None | Gzip |
|
|
502
|
+
| Objects specifying ['gzip'] and ['brotli'] | None | Gzip, Brotli |
|
|
503
|
+
| String pattern | Object specifying ['gzip'] | Brotli, Zstandard |
|
|
504
|
+
| Object specifying ['gzip', 'brotli'] | Object specifying ['gzip'] | Brotli |
|
|
505
|
+
| Any matching include pattern | String pattern | File excluded |
|
|
506
|
+
| Include patterns configured, but none match | None | File skipped |
|
|
507
|
+
|
|
508
|
+
> [!NOTE]
|
|
509
|
+
> The resulting algorithms are always limited to those enabled in the
|
|
510
|
+
> top-level `algorithms` configuration. If no algorithms remain after applying
|
|
511
|
+
> the matching rules, the file is skipped.
|
|
512
|
+
|
|
513
|
+
**Empty Algorithm Lists**
|
|
514
|
+
|
|
515
|
+
An object pattern must specify at least one algorithm. If its list is empty and
|
|
516
|
+
`failOnError` is false, the rule is ignored with a warning. When `failOnError`
|
|
517
|
+
is true, the configuration will fail. Invalid glob patterns and unknown
|
|
518
|
+
algorithm names will always fail.
|
|
519
|
+
|
|
520
|
+
**Example**
|
|
401
521
|
|
|
402
|
-
|
|
522
|
+
```ts
|
|
523
|
+
filters: {
|
|
524
|
+
fileTypes: ['.html', '.css', '.svg'],
|
|
525
|
+
include: [
|
|
526
|
+
'**/*.{html|svg}',
|
|
527
|
+
{ pattern: '**/*.css', algorithms: ['gzip'] },
|
|
528
|
+
],
|
|
529
|
+
exclude: [
|
|
530
|
+
'**/drafts/**',
|
|
531
|
+
{ pattern: '**/*.svg', algorithms: ['gzip'] },
|
|
532
|
+
],
|
|
533
|
+
}
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
In this example, HTML files receive every globally enabled algorithm, CSS files
|
|
537
|
+
receive Gzip only, and SVG files receive every globally enabled algorithm except
|
|
538
|
+
Gzip. Files beneath `drafts/` are excluded entirely.
|
|
403
539
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
540
|
+
> [!TIP]
|
|
541
|
+
> Because files only need to match one include pattern, adding `**` or
|
|
542
|
+
> `'**/**'` (which match all file paths) to the include list causes all
|
|
543
|
+
> otherwise eligible files pass the include filter, regardless of any other
|
|
544
|
+
> include patterns specified.
|
|
409
545
|
|
|
410
546
|
### Performance
|
|
411
547
|
|
|
@@ -420,7 +556,8 @@ performance: {
|
|
|
420
556
|
}
|
|
421
557
|
```
|
|
422
558
|
|
|
423
|
-
> [!NOTE]
|
|
559
|
+
> [!NOTE]
|
|
560
|
+
> `availableParallelism` comes from the `node:os` package
|
|
424
561
|
|
|
425
562
|
| Option | Description |
|
|
426
563
|
| ---------------------------- | -------------------------------------------------------------------------------------------- |
|
|
@@ -456,8 +593,14 @@ unchanged files can reuse cached results.
|
|
|
456
593
|
The cache is maintained independently of the build output, allowing compressed
|
|
457
594
|
sidecars to be restored along with their modification timestamps.
|
|
458
595
|
|
|
459
|
-
When `
|
|
460
|
-
|
|
596
|
+
When `cache.prune` is enabled, obsolete cached compression artifacts and
|
|
597
|
+
manifest entries are removed. This keeps the incremental cache from retaining
|
|
598
|
+
entries that are no longer relevant to the current build.
|
|
599
|
+
|
|
600
|
+
Output sidecars are handled separately from cache pruning. If Vite's
|
|
601
|
+
`build.emptyOutDir` option is manually set to `false`, output sidecars will be
|
|
602
|
+
removed if their source files have changed. Otherwise, they will automatically
|
|
603
|
+
be cleaned up by Vite prior to every build.
|
|
461
604
|
|
|
462
605
|
## Output
|
|
463
606
|
|
|
@@ -474,7 +617,7 @@ dist/
|
|
|
474
617
|
│ └─ styles.css
|
|
475
618
|
```
|
|
476
619
|
|
|
477
|
-
The resulting output
|
|
620
|
+
The resulting output will look like:
|
|
478
621
|
|
|
479
622
|
```text
|
|
480
623
|
dist/
|
|
@@ -511,7 +654,8 @@ When `logLevel` is set to `2`, debug messages will only be shown when the
|
|
|
511
654
|
the same output as `logLevel: 1`. The default level is `2` to match Astro's own
|
|
512
655
|
logging conventions.
|
|
513
656
|
|
|
514
|
-
> [!TIP]
|
|
657
|
+
> [!TIP]
|
|
658
|
+
> Set this value to 0 if you prefer concise logs.
|
|
515
659
|
|
|
516
660
|
## Build Lifecycle
|
|
517
661
|
|
|
@@ -523,13 +667,15 @@ The integration uses Astro's build lifecycle hooks:
|
|
|
523
667
|
- `astro:build:done`: Processes the completed build output and generates
|
|
524
668
|
compressed artifacts.
|
|
525
669
|
|
|
526
|
-
> [!WARNING]
|
|
670
|
+
> [!WARNING]
|
|
671
|
+
> If zopfli is explicitly enabled in your configuration without
|
|
527
672
|
> `node-zopfli-es` being installed, your Astro build will fail at the setup
|
|
528
673
|
> stage.
|
|
529
674
|
|
|
530
675
|
## Configuring Your Hosting Environment
|
|
531
676
|
|
|
532
|
-
> [!IMPORTANT]
|
|
677
|
+
> [!IMPORTANT]
|
|
678
|
+
> Generating compressed sidecars does not automatically configure
|
|
533
679
|
> your hosting environment to serve them. Your web server, CDN, or hosting
|
|
534
680
|
> provider must support content negotiation and serve the appropriate sidecar
|
|
535
681
|
> when a client advertises support for the algorithm.
|
|
@@ -537,9 +683,9 @@ The integration uses Astro's build lifecycle hooks:
|
|
|
537
683
|
### Apache
|
|
538
684
|
|
|
539
685
|
Apache's `rewrite_module` (`mod_rewrite.c`) can be used to redirect browsers to
|
|
540
|
-
the sidecars. Here is a sample `.htaccess` configuration
|
|
541
|
-
sites with the default options enabled. It does not
|
|
542
|
-
Zstandard, since that algorithm is disabled by default.
|
|
686
|
+
the sidecars by rewriting requests. Here is a sample `.htaccess` configuration
|
|
687
|
+
that will work for most sites with the default options enabled. It does not
|
|
688
|
+
include a section for Zstandard, since that algorithm is disabled by default.
|
|
543
689
|
|
|
544
690
|
```apacheconf
|
|
545
691
|
# SERVE COMPRESSED FILES
|
|
@@ -584,10 +730,12 @@ RewriteRule \.eot\.(br|gz)$ - [T=application/vnd.ms-fontobject,E=no-brotli:1,E=n
|
|
|
584
730
|
</FilesMatch>
|
|
585
731
|
```
|
|
586
732
|
|
|
587
|
-
> [!TIP]
|
|
733
|
+
> [!TIP]
|
|
734
|
+
> Adjust the filetypes above to match your `astro-precompressor`
|
|
588
735
|
> configuration.
|
|
589
736
|
|
|
590
|
-
> [!WARNING]
|
|
737
|
+
> [!WARNING]
|
|
738
|
+
> The `.htaccess` configuration above is representative and does not
|
|
591
739
|
> guard against a missing `mod_rewrite`. It assumes `mod_headers` and `mod_mime`
|
|
592
740
|
> are available. The encoding conditions reject explicitly disabled encodings
|
|
593
741
|
> (q=0), but do not implement complete quality-value negotiation or wildcard
|
|
@@ -621,7 +769,8 @@ server {
|
|
|
621
769
|
}
|
|
622
770
|
```
|
|
623
771
|
|
|
624
|
-
> [!IMPORTANT]
|
|
772
|
+
> [!IMPORTANT]
|
|
773
|
+
> Zstandard support is not included as standard in many Nginx
|
|
625
774
|
> installations. A separately built or third-party module may be required when
|
|
626
775
|
> using this algorithm in your environment.
|
|
627
776
|
|
package/dist/build.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAyCA,OAAO,KAAK,EAGX,WAAW,EAEX,MAAM,EAEN,eAAe,EACf,MAAM,YAAY,CAAA;AAoPnB,wBAAsB,QAAQ,CAC7B,eAAe,EAAE,MAAM,EACvB,cAAc,EAAE,MAAM,EACtB,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,eAAe,EACxB,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,WAAW,GACtB,OAAO,CAAC,IAAI,CAAC,CA4Rf"}
|
package/dist/build.js
CHANGED
|
@@ -8,19 +8,87 @@ import { runWorkerPool } from './worker-pool.js';
|
|
|
8
8
|
import { loadCache, saveCache, pruneCache, removeDisabledArtifacts, hasCacheHit, removeDisabledOutputSidecars, } from './cache.js';
|
|
9
9
|
import { compressFile } from './compression.js';
|
|
10
10
|
import { Semaphore } from './semaphore.js';
|
|
11
|
-
import { formatBytes, formatDuration, hashFile, log, logError, logHeader, dim, green, bold, toCacheKey, } from './utils.js';
|
|
11
|
+
import { formatBytes, formatDuration, hashFile, log, logError, logHeader, dim, green, bold, toCacheKey, getEnabledAlgorithms, } from './utils.js';
|
|
12
12
|
// -----------------------------------------------------------------------------
|
|
13
13
|
// File discovery
|
|
14
14
|
// -----------------------------------------------------------------------------
|
|
15
|
-
|
|
15
|
+
function resolveGlobFilters(filters) {
|
|
16
|
+
function resolve(filters) {
|
|
17
|
+
return (filters ?? []).map(filter => {
|
|
18
|
+
const pattern = typeof filter === 'string' ? filter : filter.pattern;
|
|
19
|
+
return {
|
|
20
|
+
pattern,
|
|
21
|
+
match: picomatch(pattern, { strictBrackets: true }),
|
|
22
|
+
algorithms: typeof filter === 'string' ? undefined : filter.algorithms,
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
include: resolve(filters.include),
|
|
28
|
+
exclude: resolve(filters.exclude),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function getFileAlgorithms(relativePath, filters, enabledAlgorithms) {
|
|
32
|
+
const selected = new Set();
|
|
33
|
+
// No include rules means all globally enabled algorithms are eligible.
|
|
34
|
+
if (filters.include.length === 0) {
|
|
35
|
+
for (const algorithm of enabledAlgorithms) {
|
|
36
|
+
selected.add(algorithm);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
// Include rules are OR-based. Matching rules contribute algorithms.
|
|
41
|
+
for (const filter of filters.include) {
|
|
42
|
+
if (!filter.match(relativePath)) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (!filter.algorithms) {
|
|
46
|
+
// A plain string selects every globally enabled algorithm.
|
|
47
|
+
for (const algorithm of enabledAlgorithms) {
|
|
48
|
+
selected.add(algorithm);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
for (const algorithm of filter.algorithms) {
|
|
53
|
+
if (enabledAlgorithms.includes(algorithm)) {
|
|
54
|
+
selected.add(algorithm);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Exclude rules are also OR-based: any matching rule removes its
|
|
61
|
+
// algorithms. A plain string excludes the entire file.
|
|
62
|
+
for (const filter of filters.exclude) {
|
|
63
|
+
if (!filter.match(relativePath)) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (!filter.algorithms) {
|
|
67
|
+
selected.clear();
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
for (const algorithm of filter.algorithms) {
|
|
71
|
+
selected.delete(algorithm);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Return in stable supported-algorithm order.
|
|
75
|
+
return SUPPORTED_ALGORITHMS.filter(algorithm => selected.has(algorithm));
|
|
76
|
+
}
|
|
77
|
+
async function findFiles(directory, { fileTypes, exclude }) {
|
|
16
78
|
const files = [];
|
|
17
79
|
const supportedExtensions = new Set(fileTypes.map(extension => extension.toLowerCase()));
|
|
18
|
-
|
|
19
|
-
|
|
80
|
+
// Only plain-string excludes exclude whole files or directories.
|
|
81
|
+
// Algorithm-scoped excludes are handled later per file.
|
|
82
|
+
const globalExcludes = (exclude ?? [])
|
|
83
|
+
.filter((filter) => typeof filter === 'string')
|
|
84
|
+
.map(pattern => picomatch(pattern, { strictBrackets: true }));
|
|
85
|
+
function isGloballyExcluded(relativePath) {
|
|
86
|
+
return globalExcludes.some(match => match(relativePath));
|
|
87
|
+
}
|
|
20
88
|
function isExcludedDirectory(relativePath) {
|
|
21
|
-
return (
|
|
22
|
-
|
|
23
|
-
|
|
89
|
+
return (isGloballyExcluded(relativePath) ||
|
|
90
|
+
isGloballyExcluded(`${relativePath}/`) ||
|
|
91
|
+
isGloballyExcluded(`${relativePath}/__astro_precompressor_probe__`));
|
|
24
92
|
}
|
|
25
93
|
async function walk(currentDirectory) {
|
|
26
94
|
const entries = await readdir(currentDirectory, {
|
|
@@ -47,7 +115,7 @@ async function findFiles(directory, { fileTypes, include, exclude }) {
|
|
|
47
115
|
if (!supportedExtensions.has(extension)) {
|
|
48
116
|
continue;
|
|
49
117
|
}
|
|
50
|
-
if (
|
|
118
|
+
if (isGloballyExcluded(relativePath)) {
|
|
51
119
|
continue;
|
|
52
120
|
}
|
|
53
121
|
files.push(absolutePath);
|
|
@@ -136,6 +204,11 @@ export async function runBuild(outputDirectory, cacheDirectory, configHash, opti
|
|
|
136
204
|
// Discover eligible files
|
|
137
205
|
// -------------------------------------------------------------------------
|
|
138
206
|
const files = await findFiles(outputDirectory, options.filters);
|
|
207
|
+
const resolvedFilters = resolveGlobFilters(options.filters);
|
|
208
|
+
const enabledAlgorithms = getEnabledAlgorithms(options.algorithms);
|
|
209
|
+
if (enabledAlgorithms.length === 0) {
|
|
210
|
+
throw new Error(`[${INTEGRATION_NAME}] No algorithms are enabled.`);
|
|
211
|
+
}
|
|
139
212
|
const eligibleFiles = [];
|
|
140
213
|
const currentKeys = new Set();
|
|
141
214
|
const fileStats = await runWorkerPool(files, 16, async (filePath) => ({
|
|
@@ -147,16 +220,20 @@ export async function runBuild(outputDirectory, cacheDirectory, configHash, opti
|
|
|
147
220
|
continue;
|
|
148
221
|
}
|
|
149
222
|
const key = toCacheKey(outputDirectory, filePath);
|
|
223
|
+
const relativePath = relative(outputDirectory, filePath).split(sep).join('/');
|
|
224
|
+
const algorithms = getFileAlgorithms(relativePath, resolvedFilters, enabledAlgorithms);
|
|
225
|
+
// No selected algorithms means the file is not part of this build.
|
|
226
|
+
if (algorithms.length === 0) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
150
229
|
eligibleFiles.push({
|
|
151
230
|
path: filePath,
|
|
152
231
|
key,
|
|
153
232
|
size: fileStat.size,
|
|
233
|
+
algorithms,
|
|
154
234
|
});
|
|
155
235
|
currentKeys.add(key);
|
|
156
236
|
}
|
|
157
|
-
const enabledAlgorithms = Object.entries(options.algorithms)
|
|
158
|
-
.filter(([, value]) => value)
|
|
159
|
-
.map(([algorithm]) => algorithm);
|
|
160
237
|
if (options.logLevel === 0) {
|
|
161
238
|
logger.info(`Processing ${eligibleFiles.length} eligible files`);
|
|
162
239
|
}
|
|
@@ -178,7 +255,7 @@ export async function runBuild(outputDirectory, cacheDirectory, configHash, opti
|
|
|
178
255
|
// -------------------------------------------------------------------------
|
|
179
256
|
// Prune cache
|
|
180
257
|
// -------------------------------------------------------------------------
|
|
181
|
-
if (options.
|
|
258
|
+
if (options.cache.prune) {
|
|
182
259
|
try {
|
|
183
260
|
const removed = await pruneCache(cache, artifactDirectory, currentKeys);
|
|
184
261
|
if (removed > 0) {
|
|
@@ -217,9 +294,9 @@ export async function runBuild(outputDirectory, cacheDirectory, configHash, opti
|
|
|
217
294
|
log(logger, options, 2, `${file.key}: Calculating source hash`);
|
|
218
295
|
const currentHash = await hashFile(file.path);
|
|
219
296
|
const cachedHash = cache.entries[file.key];
|
|
220
|
-
const cacheHit = await hasCacheHit(artifactDirectory, file.key, currentHash, cachedHash, forceRecompress,
|
|
221
|
-
await removeDisabledOutputSidecars(file.path,
|
|
222
|
-
const result = await compressFile(file.path, artifactDirectory, file.key, currentHash, cachedHash, forceRecompress, options, logger, compressors, semaphore);
|
|
297
|
+
const cacheHit = await hasCacheHit(artifactDirectory, file.key, currentHash, cachedHash, forceRecompress, file.algorithms);
|
|
298
|
+
await removeDisabledOutputSidecars(file.path, file.algorithms, cacheHit);
|
|
299
|
+
const result = await compressFile(file.path, artifactDirectory, file.key, currentHash, cachedHash, forceRecompress, options, logger, compressors, semaphore, file.algorithms);
|
|
223
300
|
logFileResult(logger, options, file.key, file.size, result.compressions);
|
|
224
301
|
// Only update the manifest after all configured
|
|
225
302
|
// compression algorithms have succeeded.
|
|
@@ -257,9 +334,6 @@ export async function runBuild(outputDirectory, cacheDirectory, configHash, opti
|
|
|
257
334
|
}
|
|
258
335
|
}
|
|
259
336
|
}
|
|
260
|
-
if (!enabledAlgorithms.length) {
|
|
261
|
-
throw new Error('There are no eligible algorithms selected.');
|
|
262
|
-
}
|
|
263
337
|
const workerCount = Math.min(eligibleFiles.length, options.performance.concurrency);
|
|
264
338
|
if (options.logLevel > 0) {
|
|
265
339
|
logHeader('generating precompressed files');
|