Skip to content

Commit

Permalink
refactor: @nuxtjs/pwa-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya parsa committed Feb 7, 2019
1 parent cb2eb2d commit 4afaa67
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 64 deletions.
16 changes: 3 additions & 13 deletions packages/icon/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const { existsSync } = require('fs-extra')
const path = require('path')
const Jimp = require('jimp')

const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
const { joinUrl, getRouteParams } = require('@nuxtjs/pwa-utils')

module.exports = function nuxtIcon (options) {
const hook = () => {
Expand All @@ -28,15 +26,7 @@ function generateIcons (moduleOptions) {
const options = Object.assign({}, defaults, this.options.icon, moduleOptions)
const iconSrc = options.iconSrc || path.resolve(this.options.srcDir, this.options.dir.static, options.iconFileName)

// routerBase and publicPath
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
}
}
const { publicPath } = getRouteParams(this.options)

// Ensure icon file exists
if (!existsSync(iconSrc)) {
Expand Down Expand Up @@ -66,7 +56,7 @@ function generateIcons (moduleOptions) {
const assetIcons = []
const exportIcons = {}
icons.forEach(icon => {
const src = fixUrl(`${publicPath}/${icon.fileName}`)
const src = joinUrl(publicPath, icon.fileName)
assetIcons.push({
src,
sizes: `${icon.size}x${icon.size}`,
Expand Down
1 change: 1 addition & 0 deletions packages/icon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"access": "public"
},
"dependencies": {
"@nuxtjs/pwa-utils": "3.0.0-beta.0",
"fs-extra": "^7.0.1",
"hash-sum": "^1.0.2",
"jimp": "^0.6.0"
Expand Down
16 changes: 3 additions & 13 deletions packages/manifest/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const hash = require('hash-sum')

const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
const find = (arr, key, val) => arr.find(obj => val ? obj[key] === val : obj[key])
const { joinUrl, getRouteParams, find } = require('@nuxtjs/pwa-utils')

module.exports = function nuxtManifest (options) {
const hook = () => {
Expand All @@ -17,15 +15,7 @@ module.exports = function nuxtManifest (options) {
}

function addManifest (options) {
// routerBase and publicPath
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
}
}
const { routerBase, publicPath } = getRouteParams(this.options)

// Defaults
const defaults = {
Expand Down Expand Up @@ -69,7 +59,7 @@ function addManifest (options) {

// Add manifest meta
if (!find(this.options.head.link, 'rel', 'manifest')) {
const baseAttribute = { rel: 'manifest', href: fixUrl(`${manifest.publicPath}/${manifestFileName}`) }
const baseAttribute = { rel: 'manifest', href: joinUrl(manifest.publicPath, manifestFileName) }
const attribute = manifest.crossorigin ? Object.assign({}, baseAttribute, { crossorigin: manifest.crossorigin }) : baseAttribute
this.options.head.link.push(attribute)
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/manifest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"access": "public"
},
"dependencies": {
"@nuxtjs/pwa-utils": "3.0.0-beta.0",
"hash-sum": "^1.0.2"
}
}
3 changes: 1 addition & 2 deletions packages/meta/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const find = (arr, key, val) => arr.find(obj => val ? obj[key] === val : obj[key])
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
const { find, isUrl } = require('@nuxtjs/pwa-utils')

module.exports = function nuxtMeta (_options) {
const hook = () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/meta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"repository": "https://github.com/nuxt-community/pwa-module",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@nuxtjs/pwa-utils": "3.0.0-beta.0"
}
}
16 changes: 3 additions & 13 deletions packages/onesignal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ const path = require('path')
const { writeFileSync, readFileSync } = require('fs')
const hashSum = require('hash-sum')
const { defaultsDeep } = require('lodash')

const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
const { getRouteParams, joinUrl } = require('@nuxtjs/pwa-utils')

// =============================================
// oneSignal Module
Expand All @@ -27,15 +25,7 @@ module.exports = function nuxtOneSignal (moduleOptions) {
// =============================================

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
}
}
const { publicPath } = getRouteParams(this.options)

// Merge options
const defaults = {
Expand Down Expand Up @@ -65,7 +55,7 @@ function addOneSignal (moduleOptions) {
const OneSignalSDKHash = hashSum(OneSignalSDKJS)
const OneSignalSDKFile = `ons.${OneSignalSDKHash}.js`

options.OneSignalSDK = fixUrl(publicPath + '/' + OneSignalSDKFile)
options.OneSignalSDK = joinUrl(publicPath, OneSignalSDKFile)

this.options.build.plugins.push({
apply (compiler) {
Expand Down
3 changes: 3 additions & 0 deletions packages/onesignal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"repository": "https://github.com/nuxt-community/pwa-module",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@nuxtjs/pwa-utils": "3.0.0-beta.0"
}
}
1 change: 1 addition & 0 deletions packages/pwa-utils/README.md
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.
38 changes: 38 additions & 0 deletions packages/pwa-utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { posix as path } from 'path'

function find (arr, key, val) {
return arr.find(obj => val ? obj[key] === val : obj[key])
}

function isUrl (url) {
return url.indexOf('http') === 0 || url.indexOf('//') === 0
}

function joinUrl (...args) {
return path.join(...args)
}

function getRouteParams (options) {
// routerBase
const routerBase = options.router.base

// publicPath
let publicPath
if (isUrl(options.build.publicPath)) {
publicPath = options.build.publicPath
} else {
publicPath = joinUrl(routerBase, options.build.publicPath)
}

return {
routerBase,
publicPath
}
}

module.exports = {
find,
isUrl,
joinUrl,
getRouteParams
}
10 changes: 10 additions & 0 deletions packages/pwa-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@nuxtjs/pwa-utils",
"version": "3.0.0-beta.0",
"license": "MIT",
"main": "index.js",
"repository": "https://github.com/nuxt-community/pwa-module",
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions packages/pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@nuxtjs/icon": "3.0.0-beta.0",
"@nuxtjs/manifest": "3.0.0-beta.0",
"@nuxtjs/meta": "3.0.0-beta.0",
"@nuxtjs/pwa-utils": "3.0.0-beta.0",
"@nuxtjs/workbox": "3.0.0-beta.0"
}
}
21 changes: 7 additions & 14 deletions packages/workbox/lib/options.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')

const defaults = require('./defaults')
const { fixUrl, isUrl } = require('./utils')
const { joinUrl, getRouteParams } = require('@nuxtjs/pwa-utils')

const HMRRegex = '(?!.*(__webpack_hmr|hot-update))'

Expand All @@ -15,15 +15,8 @@ function getOptions (moduleOptions) {

// publicPath
if (!options.publicPath) {
if (isUrl(this.options.build.publicPath)) {
// CDN
options.publicPath = this.options.build.publicPath
if (options.publicPath.indexOf('//') === 0) {
options.publicPath = '/' + options.publicPath
}
} else {
options.publicPath = fixUrl(`${options.routerBase}/${this.options.build.publicPath}`)
}
const { publicPath } = getRouteParams(this.options)
options.publicPath = publicPath
}

// swTemplate
Expand All @@ -38,26 +31,26 @@ function getOptions (moduleOptions) {

// swURL
if (!options.swURL) {
options.swURL = fixUrl(`${options.routerBase}/sw.js`)
options.swURL = joinUrl(options.routerBase, 'sw.js')
}

// swScope
if (!options.swScope) {
options.swScope = fixUrl(`${options.routerBase}/`)
options.swScope = options.routerBase
}

// Cache all _nuxt resources at runtime
if (options.cacheAssets) {
options.runtimeCaching.push({
urlPattern: fixUrl(`${options.publicPath}/${HMRRegex}`),
urlPattern: joinUrl(options.publicPath, HMRRegex),
handler: 'cacheFirst'
})
}

// Optionally cache other routes for offline
if (options.offline && !options.offlinePage) {
options.runtimeCaching.push({
urlPattern: fixUrl(`^${options.routerBase}/${HMRRegex}`),
urlPattern: joinUrl(`^${options.routerBase}`, HMRRegex),
handler: 'networkFirst'
})
}
Expand Down
9 changes: 0 additions & 9 deletions packages/workbox/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
const { readFileSync, existsSync } = require('fs')

function fixUrl (url) {
return url.replace(/\/\//g, '/').replace(':/', '://')
}
function isUrl (url) {
return url.indexOf('http') === 0 || url.indexOf('//') === 0
}

function readJSFiles (files) {
return Array.from(files)
.map(Boolean)
Expand All @@ -22,7 +15,5 @@ function readJSFiles (files) {
}

module.exports = {
fixUrl,
isUrl,
readJSFiles
}
3 changes: 3 additions & 0 deletions packages/workbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"repository": "https://github.com/nuxt-community/pwa-module",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@nuxtjs/pwa-utils": "3.0.0-beta.0"
}
}

0 comments on commit 4afaa67

Please sign in to comment.