UNPKG

@polyfill-io-aot/builder

Version:

This is the builder module for polyfill-io-aot.

67 lines 2.51 kB
"use strict"; const common_1 = require("@polyfill-io-aot/common"); const Bluebird = require("bluebird"); const etag = require("etag"); const fs = require("fs-extra"); const set = require("lodash/set"); const path_1 = require("path"); const zlib_1 = require("zlib"); const Executor_1 = require("../Executor"); const BuildEvent_1 = require("../interfaces/BuildEvent"); const hashRegex = /[\da-z]{32}/i; /** @internal */ class WriteManifestExecutor extends Executor_1.Executor { execute() { this.emit(BuildEvent_1.BuildEvent.WRITE_MANIFEST_BEGIN); this._ora.start('Writing manifest'); const manifest = {}; const filter = /\.js(\.gz|\.br)?$/; Bluebird.resolve(fs.readdir(this.builder.conf.outDir)) .filter((fname) => filter.test(fname)) .reduce(this.reducer.bind(this), manifest) .then(() => this.gzip(manifest)) .then((gzippedManifest) => { return fs.writeFile(path_1.join(this.conf.outDir, 'manifest.json.gz'), gzippedManifest); }) .then(() => { this.emit(BuildEvent_1.BuildEvent.WRITE_MANIFEST_OK); this._ora.succeed('Manifest written'); }, (e) => { this._ora.fail(`Error writing manifest: ${this.formatError(e)}`); this.onError(e); }); } gzip(manifest) { return new Promise((resolve, reject) => { manifest.lastModified = common_1.getLastModified(); zlib_1.gzip(JSON.stringify(manifest), { level: 9 }, (error, result) => { if (error) { reject(error); } else { resolve(result); } }); }); } reducer(acc, fname) { const hash = (fname.match(hashRegex))[0]; const enc = fname.endsWith('br') ? 2 /* BROTLI */ : fname.endsWith('gz') ? 1 /* GZIP */ : 0 /* RAW */; const encName = enc === 0 /* RAW */ ? 'raw' : enc === 2 /* BROTLI */ ? 'br' : 'gz'; return fs.readFile(path_1.join(this.conf.outDir, fname)) .then((buf) => { const tag = etag(buf); const setTag = { encoding: enc, hash }; set(acc, ['etags', tag], setTag); set(acc, ['hashes', hash, encName, 'etag'], tag); return acc; }); } } module.exports = WriteManifestExecutor; //# sourceMappingURL=WriteManifestExecutor.js.map