ember-service-worker
ember-service-worker copied to clipboard
[FEATURE REQUEST] Import external dependencies in Service Worker
It seems you cannot import anything that is not local to the service-worker
folder, and that is not an ES6 module. The config for rollup
does not include any plugins to allow importing of CJS or AMD modules, and there is no way to change that config from the outside.
Use case: would want to use libs like localForage
or in this case idb-keyval
inside a service worker to queue requests while offline, as using IDB directly is a bit cumbersome and localStorage is not available. But for sure there will be other use cases as well.
FWIW I was able to get this working by using the rollup-plugin-node-resolve
package like this.
Is there any problem with @dadleyy approach? I used his exact code an am finally able to import external dependencies. Would be nice to get his changes into master.
@marten-dockyard @eshtadc Is there a way to import libs like idb-keyval inside the service worker right now? Can you please take a look at @dadleyy's approach and if possible merge to master?
Looks valid and intriguing. Could you pull together a PR that can be tested in some various scenarios?
As a workaround you can manually import your dependency file in ember-cli-build.js
like
app.import('node_modules/some-dependency/dist/umd/index.js', {
outputFile: 'workers/some-dependency.js'
});
and in your service worker file have
importScripts('./workers/some-dependency.js');
Not the best experience, but gets the job done.