2016-12-29 22:38:19 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import zlib from 'zlib'
|
2016-12-31 12:46:23 +00:00
|
|
|
import glob from 'glob-promise'
|
2016-12-29 22:38:19 +00:00
|
|
|
|
2017-02-14 23:14:30 +00:00
|
|
|
export default async function gzipAssets (dir, buildFolder = '.next') {
|
|
|
|
const nextDir = path.resolve(dir, buildFolder)
|
2016-12-29 22:38:19 +00:00
|
|
|
|
2016-12-31 12:46:23 +00:00
|
|
|
const coreAssets = [
|
|
|
|
path.join(nextDir, 'commons.js'),
|
|
|
|
path.join(nextDir, 'main.js')
|
|
|
|
]
|
|
|
|
const pages = await glob('bundles/pages/**/*.json', { cwd: nextDir })
|
|
|
|
|
|
|
|
const allAssets = [
|
|
|
|
...coreAssets,
|
|
|
|
...pages.map(page => path.join(nextDir, page))
|
|
|
|
]
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// gzip only 10 assets in parallel at a time.
|
|
|
|
const currentChunk = allAssets.splice(0, 10)
|
|
|
|
if (currentChunk.length === 0) break
|
|
|
|
|
|
|
|
await Promise.all(currentChunk.map(gzip))
|
|
|
|
}
|
2016-12-29 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|