1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/build/gzip.js
Arunoda Susiripala 29c226771c Add AOT gzip content-encoding support for main build files. (#565)
* Add AOT gzip content-encoding support.
Currently we only do this for
main.js and commons.js only.

* Remove unwanted await.

* Use Promise.all to gzip assets in parallel.
2016-12-29 14:38:19 -08:00

24 lines
598 B
JavaScript

import fs from 'fs'
import path from 'path'
import zlib from 'zlib'
export default async function gzipAssets (dir) {
const nextDir = path.resolve(dir, '.next')
await Promise.all([
gzip(path.resolve(nextDir, 'commons.js')),
gzip(path.resolve(nextDir, 'main.js'))
])
}
export function gzip (filePath) {
const input = fs.createReadStream(filePath)
const output = fs.createWriteStream(`${filePath}.gz`)
return new Promise((resolve, reject) => {
const stream = input.pipe(zlib.createGzip()).pipe(output)
stream.on('error', reject)
stream.on('finish', resolve)
})
}