-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pooya Parsa
committed
Nov 17, 2017
1 parent
f1e1fe1
commit bbc7b72
Showing
9 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
👉 Please refer to [nuxt-community/pwa-module](https://github.com/nuxt-community/pwa-module) for documentation. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
const path = require('path') | ||
const { writeFileSync, readFileSync } = require('fs') | ||
const hashSum = require('hash-sum') | ||
const debug = require('debug')('nuxt:pwa') | ||
|
||
const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://') | ||
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0 | ||
|
||
// ============================================= | ||
// oneSignal Module | ||
// ============================================= | ||
|
||
module.exports = function nuxtOneSignal (moduleOptions) { | ||
const hook = () => { | ||
debug('Adding OneSignal') | ||
addOneSignal.call(this, moduleOptions) | ||
} | ||
|
||
if (this.options.mode === 'spa') { | ||
return hook() | ||
} | ||
|
||
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook) | ||
} | ||
|
||
// ============================================= | ||
// addOneSignal | ||
// ============================================= | ||
|
||
function addOneSignal (moduleOptions) { | ||
// Router Base | ||
const routerBase = this.options.router.base | ||
let publicPath = fixUrl(`${routerBase}/${this.options.build.publicPath}`) | ||
if (isUrl(this.options.build.publicPath)) { // CDN | ||
publicPath = this.options.build.publicPath | ||
if (publicPath.indexOf('//') === 0) { | ||
publicPath = '/' + publicPath // Escape fixUrl | ||
} | ||
} | ||
|
||
// Merge options | ||
const defaults = { | ||
OneSignalSDK: undefined, | ||
cdn: false, | ||
GcmSenderId: '482941778795', | ||
allowLocalhostAsSecureOrigin: true, | ||
importScripts: [ | ||
'/sw.js' | ||
] | ||
} | ||
|
||
const options = Object.assign(defaults, moduleOptions, this.options.oneSignal) | ||
|
||
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 = fixUrl(publicPath + '/' + OneSignalSDKFile) | ||
|
||
this.options.build.plugins.push({ | ||
apply (compiler) { | ||
compiler.plugin('emit', function (compilation, cb) { | ||
compilation.assets[OneSignalSDKFile] = { | ||
source: () => OneSignalSDKJS, | ||
size: () => OneSignalSDKJS.length | ||
} | ||
cb() | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Add the oneSignal SDK script to head | ||
this.options.head.script.push({ | ||
async: true, | ||
src: options.OneSignalSDK | ||
}) | ||
|
||
// Adjust manifest for oneSignal | ||
if (!this.options.manifest) { | ||
this.options.manifest = {} | ||
} | ||
if (this.options.manifest.gcm_sender_id) { | ||
debug('WARNING: Overriding gcm_sender_id for OnSignal') | ||
} | ||
this.options.manifest.gcm_sender_id = options.GcmSenderId | ||
|
||
// Adjust swURL option of Workbox for oneSignal | ||
if (!this.options.workbox) { | ||
this.options.workbox = {} | ||
} | ||
if (this.options.workbox.swURL) { | ||
debug('WARNING: Overriding swURL for OneSignal') | ||
} | ||
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 init plugin | ||
const onsOpts = Object.assign({}, options) | ||
delete onsOpts.OneSignalSDK | ||
delete onsOpts.cdn | ||
delete onsOpts.GcmSenderId | ||
delete onsOpts.importScripts | ||
|
||
this.addPlugin({ | ||
src: path.resolve(__dirname, 'templates/plugin.js'), | ||
ssr: false, | ||
fileName: 'onesignal.js', | ||
options: { | ||
onsOpts | ||
} | ||
}) | ||
} | ||
|
||
module.exports.meta = require('./package.json') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "@nuxtjs/onesignal", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": "https://github.com/nuxt-community/pwa-module", | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var OneSignal = window.OneSignal || []; | ||
|
||
OneSignal.push(['init', <%= JSON.stringify(options.onsOpts, null, 2) %>]); | ||
|
||
window.$OneSignal = OneSignal | ||
|
||
export default function (ctx, inject) { | ||
inject('OneSignal', OneSignal) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
importScripts('https://cdn.onesignal.com/sdks/OneSignalSDK.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
importScripts('/sw.js', 'https://cdn.onesignal.com/sdks/OneSignalSDK.js') |