UNPKG

3.27 kBMarkdownView Raw
1---
2layout: doc_page
3title: Authoring plugins
4description: How to build your own Service Worker plugins
5order: 2
6sections:
7 - anchor: initializing-a-plugin
8 title: Initializing a plugin
9 - anchor: adding-service-worker-code
10 title: Adding Service Worker code
11 - anchor: adding-service-worker-registration-code
12 title: Adding Service Worker registration code
13 - anchor: programmaticaly-customizing-the-output
14 title: Programmaticaly customizing the output
15---
16
17### Initializing a plugin
18
19Ember Service Worker plugins are just Ember CLI addons to begin with. Begin with
20initializing a new Ember addon.
21
22The first step to turn the regular Ember CLI addon into a plugin is to open up
23`package.json` and add `"ember-service-worker-plugin"` as keyword to it. This is
24because the `ember-service-worker` addon only picks up addons that have this
25keyword. An abridged version of `package.json` should look like this:
26
27{% highlight json %}
28{
29 "name": "my-ember-service-worker-plugin",
30 "version": "0.0.0",
31 "keywords": [
32 "ember-addon",
33 "ember-service-worker-plugin"
34 ]
35}
36{% endhighlight %}
37
38### Adding Service Worker code
39
40To add code to the generated Service Worker, you need to create a
41`service-worker` folder in the root of your project. You will also at least need
42to have an `index.js` file in it, this will be the entry point file that the
43generated Service Worker will include.
44
45#### Importing other JavaScript modules
46
47From within your Service Worker JavaScript files you can freely import code from
48any other Service Worker plugin, including your own of course. Plugins use
49native JavaScript modules just like your regular Ember apps and addons.
50
51The pattern to include modules goes like this:
52
53{% highlight javascript %}
54import ... from '<plugin-name>/service-worker/<path-to-module>';
55{% endhighlight %}
56
57So if you want to import the `service-worker/utils/calculate-hash.js` module
58from the `hashing-cacher` plugin the import statement would be:
59
60{% highlight javascript %}
61import calculateHash from 'hashing-cacher/service-worker/utils/calculate-hash';
62{% endhighlight %}
63
64### Adding Service Worker registration code
65
66Adding code to the Service Worker registration script works just like adding
67code to the Service Worker script, except that the code needs to be in the
68`service-worker-registration` folder instead.
69
70### Programmaticaly customizing the output
71
72If you need to modify or transform your service worker (registration) code, you
73can do that using the `treeForServiceWorker` and
74`treeForServiceWorkerRegistration` hooks in the `index.js` file that is in the
75root of the project.
76
77The first argument to the hooks is the tree containing the contents of your
78`service-worker` or `service-worker-registration` directory. The second argument
79is the tree that contains the fully built app.
80
81An example:
82
83{% highlight javascript %}
84var mergeTrees = require('broccoli-merge-trees');
85var Config = require('./lib/config');
86
87module.exports = {
88 name: 'my-ember-service-worker-plugin',
89
90 treeForServiceWorker(serviceWorkerTree, appTree) {
91 var options = this.app.options['my-service-worker-plugin'];
92 var configTree = new Config([appTree], options);
93
94 return mergeTrees([serviceWorkerTree, configTree]);
95 }
96};
97{% endhighlight %}