diff --git a/packages/icon/index.js b/packages/icon/index.js index e21022df..257222f4 100755 --- a/packages/icon/index.js +++ b/packages/icon/index.js @@ -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 = () => { @@ -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)) { @@ -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}`, diff --git a/packages/icon/package.json b/packages/icon/package.json index 634b263c..2fe3f03c 100644 --- a/packages/icon/package.json +++ b/packages/icon/package.json @@ -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" diff --git a/packages/manifest/index.js b/packages/manifest/index.js index efccecee..5821d8a6 100755 --- a/packages/manifest/index.js +++ b/packages/manifest/index.js @@ -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 = () => { @@ -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 = { @@ -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 { diff --git a/packages/manifest/package.json b/packages/manifest/package.json index 425c84e2..84b8e54a 100755 --- a/packages/manifest/package.json +++ b/packages/manifest/package.json @@ -8,6 +8,7 @@ "access": "public" }, "dependencies": { + "@nuxtjs/pwa-utils": "3.0.0-beta.0", "hash-sum": "^1.0.2" } } diff --git a/packages/meta/index.js b/packages/meta/index.js index b2430062..b6940d7d 100755 --- a/packages/meta/index.js +++ b/packages/meta/index.js @@ -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 = () => { diff --git a/packages/meta/package.json b/packages/meta/package.json index e8b3420b..f19a7056 100755 --- a/packages/meta/package.json +++ b/packages/meta/package.json @@ -6,5 +6,8 @@ "repository": "https://github.com/nuxt-community/pwa-module", "publishConfig": { "access": "public" + }, + "dependencies": { + "@nuxtjs/pwa-utils": "3.0.0-beta.0" } } diff --git a/packages/onesignal/index.js b/packages/onesignal/index.js index 37b1f3cc..0d373e3d 100755 --- a/packages/onesignal/index.js +++ b/packages/onesignal/index.js @@ -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 @@ -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 = { @@ -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) { diff --git a/packages/onesignal/package.json b/packages/onesignal/package.json index a2d54f25..805cd19b 100755 --- a/packages/onesignal/package.json +++ b/packages/onesignal/package.json @@ -6,5 +6,8 @@ "repository": "https://github.com/nuxt-community/pwa-module", "publishConfig": { "access": "public" + }, + "dependencies": { + "@nuxtjs/pwa-utils": "3.0.0-beta.0" } } diff --git a/packages/pwa-utils/README.md b/packages/pwa-utils/README.md new file mode 100644 index 00000000..212ee9b8 --- /dev/null +++ b/packages/pwa-utils/README.md @@ -0,0 +1 @@ +👉 Please refer to [nuxt-community/pwa-module](https://github.com/nuxt-community/pwa-module) for documentation. diff --git a/packages/pwa-utils/index.js b/packages/pwa-utils/index.js new file mode 100755 index 00000000..57cb3162 --- /dev/null +++ b/packages/pwa-utils/index.js @@ -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 +} diff --git a/packages/pwa-utils/package.json b/packages/pwa-utils/package.json new file mode 100755 index 00000000..e641fa43 --- /dev/null +++ b/packages/pwa-utils/package.json @@ -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" + } +} diff --git a/packages/pwa/package.json b/packages/pwa/package.json index 3f3c6a92..067596c9 100755 --- a/packages/pwa/package.json +++ b/packages/pwa/package.json @@ -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" } } diff --git a/packages/workbox/lib/options.js b/packages/workbox/lib/options.js index 3893679a..028ae227 100644 --- a/packages/workbox/lib/options.js +++ b/packages/workbox/lib/options.js @@ -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))' @@ -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 @@ -38,18 +31,18 @@ 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' }) } @@ -57,7 +50,7 @@ function getOptions (moduleOptions) { // 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' }) } diff --git a/packages/workbox/lib/utils.js b/packages/workbox/lib/utils.js index b4c94f81..5e8c0786 100644 --- a/packages/workbox/lib/utils.js +++ b/packages/workbox/lib/utils.js @@ -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) @@ -22,7 +15,5 @@ function readJSFiles (files) { } module.exports = { - fixUrl, - isUrl, readJSFiles } diff --git a/packages/workbox/package.json b/packages/workbox/package.json index 4dc20b19..19901a70 100644 --- a/packages/workbox/package.json +++ b/packages/workbox/package.json @@ -6,5 +6,8 @@ "repository": "https://github.com/nuxt-community/pwa-module", "publishConfig": { "access": "public" + }, + "dependencies": { + "@nuxtjs/pwa-utils": "3.0.0-beta.0" } }