UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

76 lines (75 loc) 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const resolver_1 = require("../resolver/resolver"); const WebWorkerProcess_1 = require("./WebWorkerProcess"); function resolveWorkerPath(module, target) { const ctx = module.props.ctx; const config = ctx.config; const resolved = resolver_1.resolveModule({ filePath: module.props.absPath, homeDir: config.homeDir, alias: config.alias, javascriptFirst: module.isJavascriptModule(), typescriptPaths: module.pkg.isDefaultPackage && ctx.tsConfig.typescriptPaths, buildTarget: config.target, modules: config.modules, importType: resolver_1.ImportType.REQUIRE, target: target, }); if (resolved.package) { ctx.log.error('WebWorker: It looks like $target in $module points to an external package. Automatic workers can only work inside your project', { target, module }); return; } if (!resolved.absPath) { ctx.log.error('WebWorker: Failed to resolve $target in $module', { target, module }); return; } return resolved; } function attachWebWorkers(ctx) { if (!ctx.config.webWorkers.enabled) { return; } function ensureWorkerExists(wk, module) { if (!ctx.webWorkers[wk.absPath]) { return WebWorkerProcess_1.registerWebWorkerProcess({ item: wk, module, ctx: ctx }); } else { return ctx.webWorkers[wk.absPath]; } } ctx.ict.on('assemble_module_init', props => { const { module } = props; if (module.isCached && props.module.fastAnalysis && props.module.fastAnalysis.workers) { module.fastAnalysis.workers.forEach(wk => { if (wk.absPath) { const webWorkerProcess = ensureWorkerExists(wk, module); webWorkerProcess.resolveWebWorkerBundlePath(); } }); } return props; }); ctx.ict.on('assemble_fast_analysis', props => { const { module } = props; if (props.module.fastAnalysis && props.module.fastAnalysis.workers) { module.fastAnalysis.workers.forEach(wk => { const resolved = resolveWorkerPath(module, wk.path); // this will be saved to cache wk.absPath = resolved.absPath; const webWorkerProcess = ensureWorkerExists(wk, module); wk.bundlePath = webWorkerProcess.resolveWebWorkerBundlePath(); }); } return props; }); ctx.ict.on('complete', props => { if (ctx.webWorkers) { for (const absPath in ctx.webWorkers) { ctx.webWorkers[absPath].run(); } } return props; }); } exports.attachWebWorkers = attachWebWorkers;