|
| 1 | +const path = require('path') |
| 2 | +const { writeFileSync, readFileSync } = require('fs') |
| 3 | +const hashSum = require('hash-sum') |
| 4 | +const debug = require('debug')('nuxt:pwa') |
| 5 | + |
| 6 | +const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://') |
| 7 | +const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0 |
| 8 | + |
| 9 | +// ============================================= |
| 10 | +// oneSignal Module |
| 11 | +// ============================================= |
| 12 | + |
| 13 | +module.exports = function nuxtOneSignal (moduleOptions) { |
| 14 | + const hook = () => { |
| 15 | + debug('Adding OneSignal') |
| 16 | + addOneSignal.call(this, moduleOptions) |
| 17 | + } |
| 18 | + |
| 19 | + if (this.options.mode === 'spa') { |
| 20 | + return hook() |
| 21 | + } |
| 22 | + |
| 23 | + this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook) |
| 24 | +} |
| 25 | + |
| 26 | +// ============================================= |
| 27 | +// addOneSignal |
| 28 | +// ============================================= |
| 29 | + |
| 30 | +function addOneSignal (moduleOptions) { |
| 31 | + // Router Base |
| 32 | + const routerBase = this.options.router.base |
| 33 | + let publicPath = fixUrl(`${routerBase}/${this.options.build.publicPath}`) |
| 34 | + if (isUrl(this.options.build.publicPath)) { // CDN |
| 35 | + publicPath = this.options.build.publicPath |
| 36 | + if (publicPath.indexOf('//') === 0) { |
| 37 | + publicPath = '/' + publicPath // Escape fixUrl |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // Merge options |
| 42 | + const defaults = { |
| 43 | + OneSignalSDK: undefined, |
| 44 | + cdn: false, |
| 45 | + GcmSenderId: '482941778795', |
| 46 | + allowLocalhostAsSecureOrigin: true, |
| 47 | + importScripts: [ |
| 48 | + '/sw.js' |
| 49 | + ] |
| 50 | + } |
| 51 | + |
| 52 | + const options = Object.assign(defaults, moduleOptions, this.options.oneSignal) |
| 53 | + |
| 54 | + if (options.OneSignalSDK === undefined) { |
| 55 | + if (options.cdn) { |
| 56 | + // Use OneSignalSDK.js from CDN |
| 57 | + options.OneSignalSDK = 'https://cdn.onesignal.com/sdks/OneSignalSDK.js' |
| 58 | + } else { |
| 59 | + // Use OneSignalSDK.js from Dist |
| 60 | + const OneSignalSDKJS = readFileSync(path.resolve(__dirname, 'dist/OneSignalSDK.js')) |
| 61 | + const OneSignalSDKHash = hashSum(OneSignalSDKJS) |
| 62 | + const OneSignalSDKFile = `ons.${OneSignalSDKHash}.js` |
| 63 | + |
| 64 | + options.OneSignalSDK = fixUrl(publicPath + '/' + OneSignalSDKFile) |
| 65 | + |
| 66 | + this.options.build.plugins.push({ |
| 67 | + apply (compiler) { |
| 68 | + compiler.plugin('emit', function (compilation, cb) { |
| 69 | + compilation.assets[OneSignalSDKFile] = { |
| 70 | + source: () => OneSignalSDKJS, |
| 71 | + size: () => OneSignalSDKJS.length |
| 72 | + } |
| 73 | + cb() |
| 74 | + }) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Add the oneSignal SDK script to head |
| 81 | + this.options.head.script.push({ |
| 82 | + async: true, |
| 83 | + src: options.OneSignalSDK |
| 84 | + }) |
| 85 | + |
| 86 | + // Adjust manifest for oneSignal |
| 87 | + if (!this.options.manifest) { |
| 88 | + this.options.manifest = {} |
| 89 | + } |
| 90 | + if (this.options.manifest.gcm_sender_id) { |
| 91 | + debug('WARNING: Overriding gcm_sender_id for OnSignal') |
| 92 | + } |
| 93 | + this.options.manifest.gcm_sender_id = options.GcmSenderId |
| 94 | + |
| 95 | + // Adjust swURL option of Workbox for oneSignal |
| 96 | + if (!this.options.workbox) { |
| 97 | + this.options.workbox = {} |
| 98 | + } |
| 99 | + if (this.options.workbox.swURL) { |
| 100 | + debug('WARNING: Overriding swURL for OneSignal') |
| 101 | + } |
| 102 | + this.options.workbox.swURL = 'OneSignalSDKWorker.js' |
| 103 | + |
| 104 | + // Provide OneSignalSDKWorker.js and OneSignalSDKUpdaterWorker.js |
| 105 | + const makeSW = (name, scripts) => { |
| 106 | + const workerScript = `importScripts(${scripts.map(i => `'${i}'`).join(', ')})\r\n` |
| 107 | + writeFileSync(path.resolve(this.options.srcDir, 'static', name), workerScript, 'utf-8') |
| 108 | + } |
| 109 | + |
| 110 | + makeSW('OneSignalSDKWorker.js', [].concat(options.importScripts || []).concat(options.OneSignalSDK)) |
| 111 | + makeSW('OneSignalSDKUpdaterWorker.js', [options.OneSignalSDK]) |
| 112 | + |
| 113 | + // Add OneSignal init plugin |
| 114 | + const onsOpts = Object.assign({}, options) |
| 115 | + delete onsOpts.OneSignalSDK |
| 116 | + delete onsOpts.cdn |
| 117 | + delete onsOpts.GcmSenderId |
| 118 | + delete onsOpts.importScripts |
| 119 | + |
| 120 | + this.addPlugin({ |
| 121 | + src: path.resolve(__dirname, 'templates/plugin.js'), |
| 122 | + ssr: false, |
| 123 | + fileName: 'onesignal.js', |
| 124 | + options: { |
| 125 | + onsOpts |
| 126 | + } |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +module.exports.meta = require('./package.json') |
0 commit comments