/
module.js
executable file
·110 lines (93 loc) · 3.1 KB
/
module.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const path = require('path')
const { writeFileSync, readFileSync } = require('fs')
const hashSum = require('hash-sum')
const defu = require('defu')
const { getRouteParams, joinUrl } = require('../utils')
// =============================================
// oneSignal Module
// =============================================
module.exports = function nuxtOneSignal (oneSignalOptions) {
const hook = () => {
addOneSignal.call(this, oneSignalOptions)
}
if (this.options.mode === 'spa') {
return hook()
}
this.nuxt.hook('build:before', hook)
}
// =============================================
// addOneSignal
// =============================================
function addOneSignal (oneSignalOptions) {
const { publicPath } = getRouteParams(this.options)
// Merge options
const defaults = {
OneSignalSDK: undefined,
cdn: true,
GcmSenderId: '482941778795',
importScripts: [
'/sw.js?' + Date.now()
],
init: {
allowLocalhostAsSecureOrigin: true,
welcomeNotification: {
disable: true
}
}
}
const options = defu(oneSignalOptions, defaults)
if (options.OneSignalSDK === undefined) {
if (options.cdn) {
// Use OneSignalSDK.js from CDN
options.OneSignalSDK = 'https://cdn.onesignal.com/sdks/OneSignalSDK.js'
} else {
// Use OneSignalSDK.js from Dist
const OneSignalSDKJS = readFileSync(path.resolve(__dirname, 'dist/OneSignalSDK.js'))
const OneSignalSDKHash = hashSum(OneSignalSDKJS)
const OneSignalSDKFile = `ons.${OneSignalSDKHash}.js`
options.OneSignalSDK = joinUrl(publicPath, OneSignalSDKFile)
this.options.build.plugins.push({
apply (compiler) {
compiler.hooks.emit.tap('nuxt-pwa-onesignal', (compilation) => {
compilation.assets[OneSignalSDKFile] = {
source: () => OneSignalSDKJS,
size: () => OneSignalSDKJS.length
}
})
}
})
}
}
// Add the oneSignal SDK script to head
if (!this.options.head.script.find(s => s.hid === 'onesignal')) {
this.options.head.script.push({
async: true,
src: options.OneSignalSDK,
hid: 'onesignal'
})
}
// Adjust manifest for oneSignal
if (!this.options.manifest) {
this.options.manifest = {}
}
this.options.manifest.gcm_sender_id = options.GcmSenderId
// Adjust swURL option of Workbox for oneSignal
if (!this.options.workbox) {
this.options.workbox = {}
}
this.options.workbox.swURL = 'OneSignalSDKWorker.js'
// Provide OneSignalSDKWorker.js and OneSignalSDKUpdaterWorker.js
const makeSW = (name, scripts) => {
const workerScript = `importScripts(${scripts.map(i => `'${i}'`).join(', ')})\r\n`
writeFileSync(path.resolve(this.options.srcDir, 'static', name), workerScript, 'utf-8')
}
makeSW('OneSignalSDKWorker.js', [].concat(options.importScripts || []).concat(options.OneSignalSDK))
makeSW('OneSignalSDKUpdaterWorker.js', [options.OneSignalSDK])
// Add OneSignal plugin
this.addPlugin({
src: path.resolve(__dirname, 'templates/plugin.js'),
ssr: false,
fileName: 'onesignal.js',
options
})
}