1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const Plugin = require('broccoli-plugin');
|
6 |
|
7 | const VERSION_STRATEGIES = {
|
8 | EVERY_BUILD: 'every-build',
|
9 | PROJECT_REVISION: 'project-revision',
|
10 | PROJECT_VERSION: 'project-version'
|
11 | };
|
12 |
|
13 | function baseModule(options) {
|
14 | return `
|
15 | export const PROJECT_VERSION = '${options.projectVersion}';
|
16 | export const PROJECT_REVISION = '${options.projectRevision}';
|
17 | export const VERSION = '${options.buildTime}';
|
18 | `;
|
19 | }
|
20 |
|
21 | const EVERY_BUILD_STRATEGY = `self.CACHE_BUSTER = VERSION;`;
|
22 | const PROJECT_REVISION_STRATEGY = `self.CACHE_BUSTER = PROJECT_REVISION;`;
|
23 | const PROJECT_VERSION_STRATEGY = `self.CACHE_BUSTER = PROJECT_VERSION;`;
|
24 | const INSTALL_EVENT_LISTENER = `
|
25 | self.addEventListener('install', function installEventListenerCallback(event) {
|
26 | return self.skipWaiting();
|
27 | });
|
28 | `;
|
29 | const IMMEDIATE_CLAIM_LISTENER = `
|
30 | self.addEventListener('message', function skipWaitingMessageCallback(event) {
|
31 | if (event.data === 'skipWaiting') {
|
32 | return self.skipWaiting();
|
33 | }
|
34 | });
|
35 | `;
|
36 | const SKIP_WAITING_LISTENER = `
|
37 | self.addEventListener('activate', function installEventListenerCallback(event) {
|
38 | return self.clients.claim();
|
39 | });
|
40 | `;
|
41 |
|
42 | module.exports = class Config extends Plugin {
|
43 | constructor(inputNodes, options) {
|
44 | super(inputNodes, {
|
45 | name: options && options.name,
|
46 | annotation: options && options.annotation
|
47 | });
|
48 |
|
49 | this.options = options;
|
50 | }
|
51 |
|
52 | build() {
|
53 | let options = this.options;
|
54 | options.buildTime = (new Date).getTime() + '|' + Math.random();
|
55 |
|
56 | let module = baseModule(options);
|
57 | let validVersionStrategies = [VERSION_STRATEGIES.EVERY_BUILD, VERSION_STRATEGIES.PROJECT_REVISION, VERSION_STRATEGIES.PROJECT_VERSION];
|
58 | let versionStrategy = validVersionStrategies.indexOf(options.versionStrategy) !== -1 ? options.versionStrategy : VERSION_STRATEGIES.EVERY_BUILD;
|
59 |
|
60 | if (versionStrategy === VERSION_STRATEGIES.EVERY_BUILD) {
|
61 | module += EVERY_BUILD_STRATEGY;
|
62 | }
|
63 |
|
64 | if (versionStrategy === VERSION_STRATEGIES.PROJECT_REVISION) {
|
65 | module += PROJECT_REVISION_STRATEGY;
|
66 | }
|
67 |
|
68 | if (versionStrategy === VERSION_STRATEGIES.PROJECT_VERSION) {
|
69 | module += PROJECT_VERSION_STRATEGY;
|
70 | }
|
71 |
|
72 | const implicitImmediateClaim = !('immediateClaim' in options);
|
73 | const explicitImmediateClaim = !implicitImmediateClaim && options.immediateClaim === true;
|
74 | const skipWaiting = ('skipWaitingOnMessage' in options && options.skipWaitingOnMessage === true);
|
75 |
|
76 | if (implicitImmediateClaim || explicitImmediateClaim) {
|
77 | module += INSTALL_EVENT_LISTENER;
|
78 | }
|
79 | if (!explicitImmediateClaim && skipWaiting) {
|
80 | module += IMMEDIATE_CLAIM_LISTENER;
|
81 | }
|
82 | if (implicitImmediateClaim || explicitImmediateClaim || skipWaiting) {
|
83 | module += SKIP_WAITING_LISTENER;
|
84 | }
|
85 |
|
86 | fs.writeFileSync(path.join(this.outputPath, 'index.js'), module);
|
87 | }
|
88 | };
|