Mercurial > hg > ugly-duckling
annotate src/worker-require.ts @ 321:89208e8af8cc
Load only pYin
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Tue, 16 May 2017 00:28:27 +0100 |
parents | 53ea6406d601 |
children |
rev | line source |
---|---|
dev@236 | 1 /** |
dev@236 | 2 * Created by lucas on 01/12/2016. |
dev@236 | 3 */ |
dev@236 | 4 |
dev@236 | 5 /* This is a really ad-hoc and crappy way of re-exporting modules, |
dev@236 | 6 * whilst trying to reduce global namespace pollution |
dev@236 | 7 * The main use case is providing access to npm modules inside a worker via importScripts |
dev@236 | 8 * over using some compiled version (a la *.min.js, *.umd.js etc) |
dev@236 | 9 * a better solution would be a custom webpack bundle, |
dev@236 | 10 * but the current build system in angular-cli doesn't provide a way for custom webpack bundles |
dev@236 | 11 * ....unless I am missing something |
dev@236 | 12 * |
dev@236 | 13 * this does, however, mean that modules will be loaded twice.. |
dev@236 | 14 * once by index.html and once by the worker.. |
dev@236 | 15 * one could potentially run a custom post build script |
dev@236 | 16 * to remove the script tag in index.html generated by angular-cli for scripts.bundle |
dev@236 | 17 * .. or find another way of doing this entirely.... |
dev@236 | 18 */ |
dev@236 | 19 |
dev@236 | 20 import extractionWorker from './app/services/feature-extraction/FeatureExtractionWorker'; |
dev@236 | 21 |
dev@236 | 22 const modules = { |
dev@236 | 23 'feature-extraction-worker': extractionWorker |
dev@236 | 24 }; |
dev@236 | 25 |
dev@236 | 26 if (typeof (self as any).importScripts === 'function' /* in a worker */) { |
dev@236 | 27 |
dev@236 | 28 self['require'] = (moduleName) => { |
dev@236 | 29 if (modules.hasOwnProperty(moduleName)) { |
dev@236 | 30 return modules[moduleName]; |
dev@236 | 31 } else { |
dev@236 | 32 throw new Error(`Cannot find module '${moduleName}'`); |
dev@236 | 33 } |
dev@236 | 34 }; |
dev@236 | 35 } |