@polyfill-io-aot/builder
Version:
This is the builder module for polyfill-io-aot.
69 lines • 2.58 kB
JavaScript
"use strict";
const Bluebird = require("bluebird");
const noop = require("lodash/noop");
const BuildEvent_1 = require("../interfaces/BuildEvent");
const PoolExecutor_1 = require("../PoolExecutor");
const symbols_1 = require("../symbols");
const CompressExecutorStatImpl_1 = require("./util/CompressExecutorStatImpl");
//tslint:disable:no-namespace
const STAT = Symbol('Stat');
/** @internal */
class CompressExecutor extends PoolExecutor_1.PoolExecutor {
execute() {
this._ora.start('Compressing polyfill bundles');
this._initPool(require.resolve('../workers/compress'));
this[STAT] = new CompressExecutorStatImpl_1.StatImpl(this.builder[symbols_1.COMBO_HASHES].length);
this.emit(BuildEvent_1.BuildEvent.COMPRESS_ALL_BEGIN, this[STAT]);
Bluebird
.map(this.builder[symbols_1.COMBO_HASHES], (hash) => {
return Bluebird.all([this.makeZopfli(hash), this.makeBrotli(hash)])
.then(noop);
})
.then(() => {
this._ora.succeed('Polyfill bundles compressed');
this.emit(BuildEvent_1.BuildEvent.COMPRESS_ALL_OK, this[STAT]);
}, (e) => {
this._ora.fail(`Failed to compress polyfill bundles: ${this.formatError(e)}`);
this.onError(e);
})
.finally(() => {
this._terminate().catch(noop);
});
}
makeBrotli(hash) {
return this
._wrap(this._pool.exec('brotli', [
this.conf.outDir,
hash,
this.conf.brotli
]))
.then(() => {
this[STAT][CompressExecutorStatImpl_1.BROTLI_COMPLETE]++;
this._ora.text = this[STAT].toString();
this.emit(BuildEvent_1.BuildEvent.COMPRESS_BROTLI_OK, this[STAT]);
})
.catch((e) => {
this.emit(BuildEvent_1.BuildEvent.COMPRESS_BROTLI_ERR, hash, e);
throw e;
});
}
makeZopfli(hash) {
return this
._wrap(this._pool.exec('gzip', [
this.conf.outDir,
hash,
this.conf.zopfli
]))
.then(() => {
this[STAT][CompressExecutorStatImpl_1.ZOPFLI_COMPLETE]++;
this._ora.text = this[STAT].toString();
this.emit(BuildEvent_1.BuildEvent.COMPRESS_ZOPFLI_OK, this[STAT]);
})
.catch((e) => {
this.emit(BuildEvent_1.BuildEvent.COMPRESS_ZOPFLI_ERR, hash, e);
throw e;
});
}
}
module.exports = CompressExecutor;
//# sourceMappingURL=CompressExecutor.js.map