Skip to content

Commit

Permalink
Merge pull request #26 from Geminii/feat/rewrite-module
Browse files Browse the repository at this point in the history
feat!: rewrite module
  • Loading branch information
Geminii committed Jul 17, 2023
2 parents 885888d + ba93d24 commit 481849c
Show file tree
Hide file tree
Showing 23 changed files with 4,322 additions and 2,408 deletions.
5 changes: 3 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ node_modules
dist
.nuxt
coverage
templates/*
example/static
test/fixture/static
sdk
src/runtime/plugin.js
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"@nuxtjs/eslint-config-typescript"
]
}
10 changes: 0 additions & 10 deletions .eslintrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
.vscode
.DS_Store
coverage
dist
14 changes: 0 additions & 14 deletions example/nuxt.config.js

This file was deleted.

32 changes: 0 additions & 32 deletions example/static/sw.js

This file was deleted.

16 changes: 4 additions & 12 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
module.exports = {
testEnvironment: 'node',
preset: '@nuxt/test-utils',
collectCoverage: true,
collectCoverageFrom: [
'lib/**/*.js',
'!lib/plugin.js'
],
moduleNameMapper: {
'^~/(.*)$': '<rootDir>/lib/$1',
'^~~$': '<rootDir>',
'^@@$': '<rootDir>',
'^@/(.*)$': '<rootDir>/lib/$1'
},
transform: {
'^.+\\.js$': 'babel-jest'
}
'src/**',
'!src/runtime/**'
]
}
102 changes: 0 additions & 102 deletions lib/module.js

This file was deleted.

43 changes: 22 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,40 @@
"name": "pooya parsa <[email protected]>"
}
],
"main": "dist/module.js",
"files": [
"lib",
"templates",
"dist"
"dist",
"sdk"
],
"main": "lib/module.js",
"scripts": {
"dev": "nuxt example",
"lint": "eslint --ext .js,.vue .",
"build": "siroc build && mkdist --src src/runtime --dist dist/runtime",
"dev": "nuxt test/fixture",
"format": "yarn lint --fix",
"lint": "eslint --ext .js,.ts,.vue .",
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && jest"
},
"dependencies": {
"defu": "^1.0.0",
"defu": "^3.2.2",
"hasha": "^5.2.0"
},
"devDependencies": {
"@babel/core": "latest",
"@babel/preset-env": "latest",
"@babel/preset-env": "^7.12.11",
"@babel/preset-typescript": "^7.12.7",
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"@nuxtjs/eslint-config": "latest",
"@nuxtjs/module-test-utils": "latest",
"@nuxtjs/pwa": "latest",
"babel-eslint": "latest",
"babel-jest": "latest",
"eslint": "latest",
"husky": "latest",
"jest": "latest",
"nuxt-edge": "latest",
"puppeteer": "latest",
"standard-version": "latest",
"tib": "latest"
"@nuxt/test-utils": "^0.1.2",
"@nuxt/types": "^2.14.12",
"@nuxtjs/eslint-config-typescript": "^5.0.0",
"@nuxtjs/pwa": "^3.3.4",
"eslint": "^7.18.0",
"husky": "^4.3.8",
"jest": "^26.6.3",
"mkdist": "^0.1.1",
"nuxt-edge": "^2.15.0-26854632.498f8553",
"playwright": "^1.8.0",
"siroc": "^0.6.3",
"standard-version": "latest"
},
"publishConfig": {
"access": "public"
Expand Down
File renamed without changes.
97 changes: 97 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { writeFileSync, readFileSync } from 'fs'
import { resolve, join } from 'path'
import defu from 'defu'
import hasha from 'hasha'
import { ModuleOptions, moduleDefaults } from './options'
import { getRouteParams, joinUrl } from './utils'
import './types'

// https://github.com/OneSignal/OneSignal-Website-SDK
function oneSignalModule (moduleOptions) {
const { nuxt } = this

const hook = () => {
addOneSignal.call(this, moduleOptions)
}

if (nuxt.options.mode === 'spa') {
return hook()
}

nuxt.hook('build:before', hook)
}

function addOneSignal (moduleOptions) {
const { nuxt, addPlugin } = this
const { publicPath } = getRouteParams(nuxt.options)

// Merge all options sources
const options: ModuleOptions = defu(
moduleOptions,
nuxt.options.oneSignal,
moduleDefaults
)

// Define oneSignalSDK usage
/* istanbul ignore next */
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 Sdk
const OneSignalSDKJS = readFileSync(resolve(__dirname, '../sdk/OneSignalSDK.js'))
const OneSignalSDKHash = hasha(OneSignalSDKJS)
const OneSignalSDKFile = `ons.${OneSignalSDKHash}.js`

options.OneSignalSDK = joinUrl(publicPath, OneSignalSDKFile)

nuxt.options.build.plugins.push({
apply (compiler: any) {
compiler.hooks.emit.tap('nuxt-pwa-onesignal', (compilation: any) => {
compilation.assets[OneSignalSDKFile] = {
source: () => OneSignalSDKJS,
size: () => OneSignalSDKJS.length
}
})
}
})
}
}

// Add the oneSignal SDK script to head
if (!nuxt.options.head.script.find((script: any) => script.hid === 'onesignal')) {
nuxt.options.head.script.push({
async: true,
src: options.OneSignalSDK,
hid: 'onesignal'
})
}

// Adjust manifest for oneSignal
nuxt.options.manifest = options.manifest

// Adjust swURL option of Workbox for oneSignal
nuxt.options.workbox = options.workbox

// Provide OneSignalSDKWorker.js and OneSignalSDKUpdaterWorker.js
const makeSW = (name: string, scripts: Array<String>) => {
const workerScript = `importScripts(${scripts.map(i => `'${i}'`).join(', ')})\r\n`
writeFileSync(resolve(nuxt.options.srcDir, 'static', name), workerScript, 'utf-8')
}

makeSW('OneSignalSDKWorker.js', [].concat(options.importScripts).concat(options.OneSignalSDK))
makeSW('OneSignalSDKUpdaterWorker.js', [options.OneSignalSDK])

// Add OneSignal plugin
addPlugin({
src: resolve(__dirname, 'runtime/plugin.js'),
ssr: false,
fileName: join('onesignal.js'),
options
})
}

oneSignalModule.meta = require('../package.json')

export default oneSignalModule
Loading

0 comments on commit 481849c

Please sign in to comment.