Skip to content

Commit 481849c

Browse files
authored
Merge pull request #26 from Geminii/feat/rewrite-module
feat!: rewrite module
2 parents 885888d + ba93d24 commit 481849c

23 files changed

+4322
-2408
lines changed

.eslintignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ node_modules
22
dist
33
.nuxt
44
coverage
5-
templates/*
6-
example/static
5+
test/fixture/static
6+
sdk
7+
src/runtime/plugin.js

.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": [
3+
"@nuxtjs/eslint-config-typescript"
4+
]
5+
}

.eslintrc.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules
66
.vscode
77
.DS_Store
88
coverage
9+
dist

example/nuxt.config.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

example/static/sw.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

jest.config.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
module.exports = {
22
testEnvironment: 'node',
3+
preset: '@nuxt/test-utils',
34
collectCoverage: true,
45
collectCoverageFrom: [
5-
'lib/**/*.js',
6-
'!lib/plugin.js'
7-
],
8-
moduleNameMapper: {
9-
'^~/(.*)$': '<rootDir>/lib/$1',
10-
'^~~$': '<rootDir>',
11-
'^@@$': '<rootDir>',
12-
'^@/(.*)$': '<rootDir>/lib/$1'
13-
},
14-
transform: {
15-
'^.+\\.js$': 'babel-jest'
16-
}
6+
'src/**',
7+
'!src/runtime/**'
8+
]
179
}

lib/module.js

Lines changed: 0 additions & 102 deletions
This file was deleted.

package.json

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,40 @@
99
"name": "pooya parsa <pooya@pi0.ir>"
1010
}
1111
],
12+
"main": "dist/module.js",
1213
"files": [
13-
"lib",
14-
"templates",
15-
"dist"
14+
"dist",
15+
"sdk"
1616
],
17-
"main": "lib/module.js",
1817
"scripts": {
19-
"dev": "nuxt example",
20-
"lint": "eslint --ext .js,.vue .",
18+
"build": "siroc build && mkdist --src src/runtime --dist dist/runtime",
19+
"dev": "nuxt test/fixture",
20+
"format": "yarn lint --fix",
21+
"lint": "eslint --ext .js,.ts,.vue .",
2122
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
2223
"test": "yarn lint && jest"
2324
},
2425
"dependencies": {
25-
"defu": "^1.0.0",
26+
"defu": "^3.2.2",
2627
"hasha": "^5.2.0"
2728
},
2829
"devDependencies": {
29-
"@babel/core": "latest",
30-
"@babel/preset-env": "latest",
30+
"@babel/preset-env": "^7.12.11",
31+
"@babel/preset-typescript": "^7.12.7",
3132
"@commitlint/cli": "latest",
3233
"@commitlint/config-conventional": "latest",
33-
"@nuxtjs/eslint-config": "latest",
34-
"@nuxtjs/module-test-utils": "latest",
35-
"@nuxtjs/pwa": "latest",
36-
"babel-eslint": "latest",
37-
"babel-jest": "latest",
38-
"eslint": "latest",
39-
"husky": "latest",
40-
"jest": "latest",
41-
"nuxt-edge": "latest",
42-
"puppeteer": "latest",
43-
"standard-version": "latest",
44-
"tib": "latest"
34+
"@nuxt/test-utils": "^0.1.2",
35+
"@nuxt/types": "^2.14.12",
36+
"@nuxtjs/eslint-config-typescript": "^5.0.0",
37+
"@nuxtjs/pwa": "^3.3.4",
38+
"eslint": "^7.18.0",
39+
"husky": "^4.3.8",
40+
"jest": "^26.6.3",
41+
"mkdist": "^0.1.1",
42+
"nuxt-edge": "^2.15.0-26854632.498f8553",
43+
"playwright": "^1.8.0",
44+
"siroc": "^0.6.3",
45+
"standard-version": "latest"
4546
},
4647
"publishConfig": {
4748
"access": "public"
File renamed without changes.

src/module.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { writeFileSync, readFileSync } from 'fs'
2+
import { resolve, join } from 'path'
3+
import defu from 'defu'
4+
import hasha from 'hasha'
5+
import { ModuleOptions, moduleDefaults } from './options'
6+
import { getRouteParams, joinUrl } from './utils'
7+
import './types'
8+
9+
// https://github.com/OneSignal/OneSignal-Website-SDK
10+
function oneSignalModule (moduleOptions) {
11+
const { nuxt } = this
12+
13+
const hook = () => {
14+
addOneSignal.call(this, moduleOptions)
15+
}
16+
17+
if (nuxt.options.mode === 'spa') {
18+
return hook()
19+
}
20+
21+
nuxt.hook('build:before', hook)
22+
}
23+
24+
function addOneSignal (moduleOptions) {
25+
const { nuxt, addPlugin } = this
26+
const { publicPath } = getRouteParams(nuxt.options)
27+
28+
// Merge all options sources
29+
const options: ModuleOptions = defu(
30+
moduleOptions,
31+
nuxt.options.oneSignal,
32+
moduleDefaults
33+
)
34+
35+
// Define oneSignalSDK usage
36+
/* istanbul ignore next */
37+
if (options.OneSignalSDK === undefined) {
38+
if (options.cdn) {
39+
// Use OneSignalSDK.js from CDN
40+
options.OneSignalSDK = 'https://cdn.onesignal.com/sdks/OneSignalSDK.js'
41+
} else {
42+
// Use OneSignalSDK.js from Sdk
43+
const OneSignalSDKJS = readFileSync(resolve(__dirname, '../sdk/OneSignalSDK.js'))
44+
const OneSignalSDKHash = hasha(OneSignalSDKJS)
45+
const OneSignalSDKFile = `ons.${OneSignalSDKHash}.js`
46+
47+
options.OneSignalSDK = joinUrl(publicPath, OneSignalSDKFile)
48+
49+
nuxt.options.build.plugins.push({
50+
apply (compiler: any) {
51+
compiler.hooks.emit.tap('nuxt-pwa-onesignal', (compilation: any) => {
52+
compilation.assets[OneSignalSDKFile] = {
53+
source: () => OneSignalSDKJS,
54+
size: () => OneSignalSDKJS.length
55+
}
56+
})
57+
}
58+
})
59+
}
60+
}
61+
62+
// Add the oneSignal SDK script to head
63+
if (!nuxt.options.head.script.find((script: any) => script.hid === 'onesignal')) {
64+
nuxt.options.head.script.push({
65+
async: true,
66+
src: options.OneSignalSDK,
67+
hid: 'onesignal'
68+
})
69+
}
70+
71+
// Adjust manifest for oneSignal
72+
nuxt.options.manifest = options.manifest
73+
74+
// Adjust swURL option of Workbox for oneSignal
75+
nuxt.options.workbox = options.workbox
76+
77+
// Provide OneSignalSDKWorker.js and OneSignalSDKUpdaterWorker.js
78+
const makeSW = (name: string, scripts: Array<String>) => {
79+
const workerScript = `importScripts(${scripts.map(i => `'${i}'`).join(', ')})\r\n`
80+
writeFileSync(resolve(nuxt.options.srcDir, 'static', name), workerScript, 'utf-8')
81+
}
82+
83+
makeSW('OneSignalSDKWorker.js', [].concat(options.importScripts).concat(options.OneSignalSDK))
84+
makeSW('OneSignalSDKUpdaterWorker.js', [options.OneSignalSDK])
85+
86+
// Add OneSignal plugin
87+
addPlugin({
88+
src: resolve(__dirname, 'runtime/plugin.js'),
89+
ssr: false,
90+
fileName: join('onesignal.js'),
91+
options
92+
})
93+
}
94+
95+
oneSignalModule.meta = require('../package.json')
96+
97+
export default oneSignalModule

0 commit comments

Comments
 (0)