changeset 38:a1ca41c70351

Provide a TypeScript file to include external modules, which also provides a way to expose imported modules from the webpack bundle to a web worker. (a pretty horrible hack around angular-cli build limitations).
author Lucas Thompson <dev@lucas.im>
date Thu, 01 Dec 2016 15:44:12 +0000
parents f6e58c2accb0
children e6eb133fa47c
files src/externals.ts
diffstat 1 files changed, 34 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/externals.ts	Thu Dec 01 15:44:12 2016 +0000
@@ -0,0 +1,34 @@
+/**
+ * Created by lucas on 01/12/2016.
+ */
+
+/* This is a really ad-hoc and crappy way of re-exporting modules,
+ * whilst trying to reduce global namespace pollution
+ * The main use case is providing access to npm modules inside a worker via importScripts
+ * over using some compiled version (a la *.min.js, *.umd.js etc)
+ * a better solution would be a custom webpack bundle,
+ * but the current build system in angular-cli doesn't provide a way for custom webpack bundles
+ * ....unless I am missing something
+ *
+ * this does, however, mean that modules will be loaded twice..
+ * once by index.html and once by the worker..
+ * one could potentially run a custom post build script
+ * to remove the script tag in index.html generated by angular-cli for scripts.bundle
+ * .. or find another way of doing this entirely....
+ */
+
+import extractionWorker from './app/services/feature-extraction/FeatureExtractionWorker';
+
+const modules = {
+  'feature-extraction-worker': extractionWorker,
+};
+
+if (typeof (self as any).importScripts === 'function' /* in a worker */) {
+
+  self['require'] = (moduleName) => {
+    if (modules.hasOwnProperty(moduleName))
+      return modules[moduleName];
+    else
+      throw new Error(`Cannot find module '${moduleName}'`);
+  };
+}