mercredi 7 septembre 2016

Packaging web worker source for use with Ember.js

I am implementing a feature in our Ember app with web workers, and am having trouble figuring out how to package the worker source.

My first approach was to place worker.js into the assets directory, but in our configuration, assets are served from a different server in production, and we get the error:

Failed to construct Worker: Script at http://ift.tt/2cHHqwt cannot be accessed from origin https://app.foo.com

I tried futzing with document.domain (changing it to foo.com), but the error persists.

I wondered if I could modify ember-cli-build.js (aka Brocfile.js) to create another JS bundle in addition to app.js and vendor.js (say, worker.js), which would be deployed in the same location as those, and could be brought in with a <script> tag in index.html. However, I'm unable to figure out how to do that, either due to the app.js/vendor.js logic being burnt into ember-cli, or my lack of knowledge of Broccoli.

So next I considered creating the worker from a blob. The question is where to store the source for the blob. I considered placing the source for the blob inside a non-standard <script> tag in index.html, from where I could retrieve it and create the blob. I don't want to insert the worker source directly into index.html, though, so I thought to use broccoli-replace, but the worker source is written in ES6 and needs transpilation. I considered using an ember add-on which allows you to insert things into index.html using , but that requires me to specify the text to be inserted, and I don't know where to get it from, and the transpilation problem remains.

I then considered some very klujy solutions such as wrapping the worker source in a function, and then stringifying the function and extracting the body (between {}) at run-time. That works, but the stench is a bit overwhelming, and I cannot use any ES6 constructs such as spread operators or array destructuring that cause Babel to invoke its little run-time utilities like _toConsumableArray.

Any reasonable solutions, or ways to make any of the above approaches work?




Aucun commentaire:

Enregistrer un commentaire