forked from BBVAEngineering/ember-cli-workbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (68 loc) · 2.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
const mergeTrees = require('broccoli-merge-trees');
const BroccoliWorkbox = require('./lib/broccoli-workbox');
module.exports = {
name: require('./package').name,
isDevelopingAddon: () => true,
config(env, baseConfig) {
const baseOptions = baseConfig['ember-cli-workbox'] || {};
const appOptions =
(this.app && this.app.options['ember-cli-workbox']) || {};
const projectName = (baseConfig.APP && baseConfig.APP.name) || 'app';
const appWorkboxOptions = (this.app && this.app.options.workbox) || {};
const workboxOptions = Object.assign(
// Defaults
{
swDest: 'sw.js',
globDirectory: './',
globPatterns: [
'**/*.{json,css,js,png,svg,eot,ttf,woff,jpg,gif,ico,xml,html,txt}',
],
skipWaiting: false,
clientsClaim: false,
cacheId: projectName,
},
// Options from environment config (workbox)
appWorkboxOptions
);
// Do nothing if no ENV. For example, when running an ember generator.
if (!env && !process.env.EMBER_ENV) {
return;
}
const isProdBuild = Boolean(env.match('prod'));
const options = Object.assign(
// Defaults
{
enabled: isProdBuild,
debug: !isProdBuild,
importScriptsGlobPatterns: ['assets/service-workers/*.js'],
},
// Options from initial app config (ember-cli-workbox)
baseOptions,
// Options from environment config (ember-cli-workbox)
appOptions
);
this._options = options;
this.workboxOptions = workboxOptions;
// Append "workbox" config to "ember-cli-workbox" config
if (baseOptions) {
baseOptions.swDest = workboxOptions.swDest;
}
},
postprocessTree(type, tree) {
if (type !== 'all') {
return tree;
}
// This 'all' tree has all the app's files and assets,
// which is exactly what we need for making a service worker using workbox
// Our BroccoliWorkbox plugin will generate the service worker for the given tree
const workboxFunnel = new BroccoliWorkbox([tree], {
options: this._options,
workboxOptions: this.workboxOptions,
});
// Add this BroccoliWorkbox tree to the app's tree
return mergeTrees([tree, workboxFunnel], {
overwrite: true,
});
},
};