1- import { createReadStream , createWriteStream , statSync } from 'fs' ;
2- import { pipeline } from 'stream' ;
3- import glob from 'tiny-glob' ;
4- import { promisify } from 'util' ;
5- import zlib from 'zlib' ;
61import { platforms } from './platforms.js' ;
72
8- const pipe = promisify ( pipeline ) ;
9-
103/** @type {import('.').default } */
114export default function ( options ) {
125 return {
@@ -49,13 +42,13 @@ export default function (options) {
4942 if ( precompress ) {
5043 if ( pages === assets ) {
5144 builder . log . minor ( 'Compressing assets and pages' ) ;
52- await compress ( assets ) ;
45+ await builder . compress ( assets ) ;
5346 } else {
5447 builder . log . minor ( 'Compressing assets' ) ;
55- await compress ( assets ) ;
48+ await builder . compress ( assets ) ;
5649
5750 builder . log . minor ( 'Compressing pages' ) ;
58- await compress ( pages ) ;
51+ await builder . compress ( pages ) ;
5952 }
6053 }
6154
@@ -69,41 +62,3 @@ export default function (options) {
6962 }
7063 } ;
7164}
72-
73- /**
74- * @param {string } directory
75- */
76- async function compress ( directory ) {
77- const files = await glob ( '**/*.{html,js,json,css,svg,xml,wasm}' , {
78- cwd : directory ,
79- dot : true ,
80- absolute : true ,
81- filesOnly : true
82- } ) ;
83-
84- await Promise . all (
85- files . map ( ( file ) => Promise . all ( [ compress_file ( file , 'gz' ) , compress_file ( file , 'br' ) ] ) )
86- ) ;
87- }
88-
89- /**
90- * @param {string } file
91- * @param {'gz' | 'br' } format
92- */
93- async function compress_file ( file , format = 'gz' ) {
94- const compress =
95- format == 'br'
96- ? zlib . createBrotliCompress ( {
97- params : {
98- [ zlib . constants . BROTLI_PARAM_MODE ] : zlib . constants . BROTLI_MODE_TEXT ,
99- [ zlib . constants . BROTLI_PARAM_QUALITY ] : zlib . constants . BROTLI_MAX_QUALITY ,
100- [ zlib . constants . BROTLI_PARAM_SIZE_HINT ] : statSync ( file ) . size
101- }
102- } )
103- : zlib . createGzip ( { level : zlib . constants . Z_BEST_COMPRESSION } ) ;
104-
105- const source = createReadStream ( file ) ;
106- const destination = createWriteStream ( `${ file } .${ format } ` ) ;
107-
108- await pipe ( source , compress , destination ) ;
109- }
0 commit comments