UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

88 lines (87 loc) 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fusebox_1 = require("../core/fusebox"); const utils_1 = require("../utils/utils"); class WebWorkerProcess { constructor(props) { this.props = props; //this.props.item.worker = this; this.isRunning = false; const amount = Object.keys(props.ctx.webWorkers).length + 1; this.bundleName = `worker${amount}_${utils_1.fastHash(this.props.item.absPath)}`; } resolveWebWorkerBundlePath() { // making it resolve properly if (this.props.ctx.webIndex && this.props.ctx.webIndex.resolve) { this.props.item.bundlePath = this.props.ctx.webIndex.resolve(this.bundleName) + '.js'; } return this.props.item.bundlePath; } async run() { if (this.isRunning) return; this.isRunning = true; const ctx = this.props.ctx; ctx.log.info('worker', 'Worker process start $path', { path: this.props.item.absPath }); let config = { homeDir: ctx.config.homeDir, target: 'web-worker', // share the same cache object cacheObject: ctx.cache, cache: ctx.config.cache && ctx.config.cache.enabled, entry: this.props.item.absPath, devServer: false, plugins: [ localCtx => { localCtx.ict.on('rebundle_complete', p => { ctx.log.info('worker', 'Worker re-bundled $worker', { worker: this.props.item.absPath, }); return p; }); localCtx.ict.on('before_bundle_write', p => { p.bundle.name = this.bundleName; return p; }); }, ], hmr: false, logging: { level: 'disabled' }, watch: ctx.config.watch.enabled, }; if (ctx.config.webWorkers.config) { // add missing stuff to the configuartion // but don't allow override the compulsory configuration config = Object.assign(Object.assign({}, ctx.config.webWorkers.config), config); // need to add plugins here if exist if (ctx.config.webWorkers.config.plugins) { config.plugins = config.plugins.concat(ctx.config.webWorkers.config.plugins); } // allow override watch property if (ctx.config.webWorkers.config.watch) { config.watch = ctx.config.webWorkers.config.watch; } } const fuse = fusebox_1.fusebox(config); if (ctx.config.production) { await fuse.runProd(ctx.config.production); ctx.log.info('worker', 'Worker bundled (production) $worker', { worker: this.props.item.absPath, }); } else { ctx.log.info('worker', 'Worker bundled (development) $worker', { worker: this.props.item.absPath, }); await fuse.runDev(); } } } exports.WebWorkerProcess = WebWorkerProcess; function registerWebWorkerProcess(props) { const workerProcess = new WebWorkerProcess(props); props.ctx.log.info('worker', 'registered ' + props.item.absPath); props.ctx.webWorkers[props.item.absPath] = workerProcess; return workerProcess; } exports.registerWebWorkerProcess = registerWebWorkerProcess;