ember-service-worker icon indicating copy to clipboard operation
ember-service-worker copied to clipboard

[FEATURE REQUEST] Import external dependencies in Service Worker

Open simonihmig opened this issue 8 years ago • 5 comments

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.

simonihmig avatar Feb 21 '17 09:02 simonihmig

FWIW I was able to get this working by using the rollup-plugin-node-resolve package like this.

dadleyy avatar Apr 11 '18 14:04 dadleyy

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.

CodingDive avatar Aug 29 '18 14:08 CodingDive

@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?

aliHafizji avatar Nov 29 '18 07:11 aliHafizji

Looks valid and intriguing. Could you pull together a PR that can be tested in some various scenarios?

eshtadc avatar Dec 12 '18 14:12 eshtadc

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.

miguelcobain avatar Nov 29 '19 18:11 miguelcobain