@polyfill-io-aot/builder
Version:
This is the builder module for polyfill-io-aot.
172 lines • 6.54 kB
JavaScript
;
var _a, _b;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const common_1 = require("@polyfill-io-aot/common");
const EventEmitter = require("events");
const lazy_get_decorator_1 = require("lazy-get-decorator");
const merge = require("lodash/merge");
const ImportedOra = require("ora");
const os_1 = require("os");
const path_1 = require("path");
const zlib_1 = require("zlib");
const BuildEvent_1 = require("./interfaces/BuildEvent");
const symbols_1 = require("./symbols");
const END_TIME = Symbol('End time');
const START_TIME = Symbol('Start time');
// They messed up the typings
const ora = ImportedOra;
/** Code polyfill builder class */
class PolyfillBuilder extends EventEmitter {
/**
* Constructor
* @param conf Builder configuration
*/
constructor(conf) {
super();
const defaults = {
brotli: {
[zlib_1.constants.BROTLI_PARAM_QUALITY]: zlib_1.constants.BROTLI_MAX_QUALITY
},
dirs: [],
excludes: [],
flags: [],
outDir: common_1.DEFAULT_OUT_DIR,
packageManager: 'npm',
polyfills: ['default'],
//tslint:disable-next-line:no-magic-numbers
processes: process.env.CI ? 2 : Math.max(1, os_1.cpus().length - 1),
uaGenerators: {},
unknown: 'polyfill',
zopfli: {
blocksplitting: true,
numiterations: 15
}
};
this.conf = merge(defaults, conf || {});
this.initUaGenerators();
Object.freeze(this.conf);
this.initLogs();
}
/** Completion promise returned by {@link #start()} */
get promise() {
return new Promise((resolve$, reject) => {
this.once(BuildEvent_1.BuildEvent.END, resolve$);
this.once(BuildEvent_1.BuildEvent.ERROR, reject);
});
}
/** Number of seconds the builder spent building */
get runtimeSeconds() {
if (!this[START_TIME]) {
return '0.00';
}
return (((this[END_TIME] || new Date()).getTime() - this[START_TIME].getTime()) / 1000)
//tslint:disable-next-line:no-magic-numbers
.toFixed(2);
}
/** @internal */
get [_a = symbols_1.POLYFILL_LIBRARY_PKG]() {
return path_1.dirname(require.resolve('polyfill-library/package.json'));
}
/** @internal */
get [_b = symbols_1.POLYFILLS_ROOT]() {
return path_1.join(this[symbols_1.POLYFILL_LIBRARY_PKG], 'polyfills');
}
/**
* Start the build promise
* @returns A promise that completes on the END event and rejects on the ERROR event
*/
start() {
if (this[START_TIME]) {
const err = new Error('Build already started');
this.emit(BuildEvent_1.BuildEvent.ERROR, err);
return Promise.reject(err);
}
else {
const out = this.promise;
this.initChain();
setImmediate(() => {
this.emit(BuildEvent_1.BuildEvent.START);
});
return out;
}
}
initChain() {
const chain = {
[BuildEvent_1.BuildEvent.START]: './executors/FormatSourceDirsExecutor',
[BuildEvent_1.BuildEvent.FORMAT_DIRS_OK]: './executors/ValidateSourceDirsExecutor',
[BuildEvent_1.BuildEvent.VALIDATE_DIRS_OK]: './executors/CopySourceDirsExecutor',
[BuildEvent_1.BuildEvent.COPY_EXTRA_DIRS_OK]: './executors/CopyExtraFilesExecutor',
[BuildEvent_1.BuildEvent.COPY_EXTRA_FILES_OK]: './executors/CompilePolyfillsExecutor',
[BuildEvent_1.BuildEvent.COMPILE_POLYFILLS_OK]: './executors/GenerateUserAgentsExecutor',
[BuildEvent_1.BuildEvent.GENERATE_UAS_ALL_OK]: './executors/GeneratePolyfillCombinations',
[BuildEvent_1.BuildEvent.GENERATE_COMBO_ALL_OK]: './executors/WritePolyfillCombinationsExecutor',
[BuildEvent_1.BuildEvent.GENERATE_BUNDLES_OK]: './executors/UglifyExecutor',
[BuildEvent_1.BuildEvent.UGLIFY_ALL_OK]: './executors/CompressExecutor',
[BuildEvent_1.BuildEvent.COMPRESS_ALL_OK]: './executors/WriteManifestExecutor'
};
this.once(BuildEvent_1.BuildEvent.WRITE_MANIFEST_OK, () => {
setImmediate(() => {
this.emit(BuildEvent_1.BuildEvent.END);
});
});
for (const k of Object.keys(chain)) {
this.once(k, () => {
setImmediate(() => {
const xc = require(chain[k]);
new xc(this).start();
});
});
}
}
initLogs() {
this
.once(BuildEvent_1.BuildEvent.START, () => {
this[START_TIME] = new Date();
})
.once(BuildEvent_1.BuildEvent.ERROR, (e) => {
this[END_TIME] = new Date();
const msg = e.stack || e.message || e;
ora().fail(`Build errored after ${this.runtimeSeconds} seconds with ${msg}`);
})
.once(BuildEvent_1.BuildEvent.END, () => {
this[END_TIME] = new Date();
ora().succeed(`Build finished in ${this.runtimeSeconds} seconds`);
});
}
initUaGenerators() {
const mappings = [
['android', 'android'],
['blackberry', 'bb'],
['chrome', 'chrome'],
['firefox', 'firefox'],
['firefoxMobile', 'firefox'],
['ie', 'ie'],
['ieMobile', 'ie_mob'],
['iosSafari', 'ios_saf'],
['opera', 'opera'],
['operaMobile', 'op_mob'],
['safari', 'safari'],
['samsung', 'samsung']
];
for (const mapping of mappings) {
const prop = mapping[0];
if (!this.conf.uaGenerators[prop]) {
const src = mapping[1];
//tslint:disable-next-line:no-var-requires
this.conf.uaGenerators[prop] = require(`./ua-generators/${src}`);
}
}
}
}
tslib_1.__decorate([
lazy_get_decorator_1.LazyGetter()
], PolyfillBuilder.prototype, "promise", null);
tslib_1.__decorate([
lazy_get_decorator_1.LazyGetter(true)
], PolyfillBuilder.prototype, _a, null);
tslib_1.__decorate([
lazy_get_decorator_1.LazyGetter(true)
], PolyfillBuilder.prototype, _b, null);
exports.PolyfillBuilder = PolyfillBuilder;
//# sourceMappingURL=PolyfillBuilder.js.map