1 | ---
|
2 | layout: doc_page
|
3 | title: Authoring plugins
|
4 | description: How to build your own Service Worker plugins
|
5 | order: 2
|
6 | sections:
|
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 |
|
19 | Ember Service Worker plugins are just Ember CLI addons to begin with. Begin with
|
20 | initializing a new Ember addon.
|
21 |
|
22 | The 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
|
24 | because the `ember-service-worker` addon only picks up addons that have this
|
25 | keyword. 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 |
|
40 | To 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
|
42 | to have an `index.js` file in it, this will be the entry point file that the
|
43 | generated Service Worker will include.
|
44 |
|
45 | #### Importing other JavaScript modules
|
46 |
|
47 | From within your Service Worker JavaScript files you can freely import code from
|
48 | any other Service Worker plugin, including your own of course. Plugins use
|
49 | native JavaScript modules just like your regular Ember apps and addons.
|
50 |
|
51 | The pattern to include modules goes like this:
|
52 |
|
53 | {% highlight javascript %}
|
54 | import ... from '<plugin-name>/service-worker/<path-to-module>';
|
55 | {% endhighlight %}
|
56 |
|
57 | So if you want to import the `service-worker/utils/calculate-hash.js` module
|
58 | from the `hashing-cacher` plugin the import statement would be:
|
59 |
|
60 | {% highlight javascript %}
|
61 | import calculateHash from 'hashing-cacher/service-worker/utils/calculate-hash';
|
62 | {% endhighlight %}
|
63 |
|
64 | ### Adding Service Worker registration code
|
65 |
|
66 | Adding code to the Service Worker registration script works just like adding
|
67 | code 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 |
|
72 | If you need to modify or transform your service worker (registration) code, you
|
73 | can do that using the `treeForServiceWorker` and
|
74 | `treeForServiceWorkerRegistration` hooks in the `index.js` file that is in the
|
75 | root of the project.
|
76 |
|
77 | The first argument to the hooks is the tree containing the contents of your
|
78 | `service-worker` or `service-worker-registration` directory. The second argument
|
79 | is the tree that contains the fully built app.
|
80 |
|
81 | An example:
|
82 |
|
83 | {% highlight javascript %}
|
84 | var mergeTrees = require('broccoli-merge-trees');
|
85 | var Config = require('./lib/config');
|
86 |
|
87 | module.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 %}
|