@nuxtjs/workbox
Advanced tools
Comparing version 1.2.2 to 2.0.0
@@ -6,2 +6,17 @@ # Change Log | ||
<a name="2.0.0"></a> | ||
# [2.0.0](https://github.com/nuxt-community/pwa-module/compare/@nuxtjs/workbox@1.2.2...@nuxtjs/workbox@2.0.0) (2017-11-16) | ||
### Features | ||
* Add compatibility for nuxt 1.0.0 hooks ([c0efb1d](https://github.com/nuxt-community/pwa-module/commit/c0efb1d)) | ||
* add option to register third-party sw ([#12](https://github.com/nuxt-community/pwa-module/issues/12)) ([9c0b61e](https://github.com/nuxt-community/pwa-module/commit/9c0b61e)) | ||
* **workbox:** expose registration as window.$sw ([a5ddf59](https://github.com/nuxt-community/pwa-module/commit/a5ddf59)) | ||
* **workbox:** importScripts option ([7c8644c](https://github.com/nuxt-community/pwa-module/commit/7c8644c)) | ||
* **workbox:** rewrite with better template support ([b0af84e](https://github.com/nuxt-community/pwa-module/commit/b0af84e)) | ||
<a name="1.2.2"></a> | ||
@@ -8,0 +23,0 @@ ## 1.2.2 (2017-10-04) |
139
index.js
const path = require('path') | ||
const swBuild = require('workbox-build') | ||
const { readFileSync, writeFileSync } = require('fs') | ||
const hashSum = require('hash-sum') | ||
const debug = require('debug')('nuxt:pwa') | ||
@@ -7,3 +10,7 @@ const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://') | ||
module.exports = function nuxtWorkbox (options) { | ||
// ============================================= | ||
// workboxModule | ||
// ============================================= | ||
module.exports = function nuxtWorkbox (moduleOptions) { | ||
if (this.options.dev) { | ||
@@ -13,3 +20,19 @@ return | ||
// routerBase and publicPath | ||
const hook = builder => { | ||
debug('Adding workbox') | ||
const options = getOptions.call(this, moduleOptions) | ||
workboxInject.call(this, options) | ||
emitAssets.call(this, options) | ||
addTemplates.call(this, options) | ||
} | ||
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook) | ||
} | ||
// ============================================= | ||
// getRouterBase | ||
// ============================================= | ||
function getOptions (moduleOptions) { | ||
// Router Base | ||
const routerBase = this.options.router.base | ||
@@ -20,13 +43,11 @@ let publicPath = fixUrl(`${routerBase}/${this.options.build.publicPath}`) | ||
if (publicPath.indexOf('//') === 0) { | ||
publicPath = '/' + publicPath // escape fixUrl | ||
publicPath = '/' + publicPath // Escape fixUrl | ||
} | ||
} | ||
const swFileName = 'sw.js' | ||
// Use swBuild to generate sw file | ||
// We set dest to static dir that is served as / to allow global sw scope | ||
// https://workboxjs.org/reference-docs/latest/module-workbox-build.html#.generateSW | ||
const workboxOptions = Object.assign({ | ||
swDest: path.resolve(this.options.srcDir, 'static', swFileName), | ||
const options = Object.assign({ | ||
routerBase, | ||
publicPath, | ||
swSrc: path.resolve(this.options.buildDir, 'sw.template.js'), | ||
swDest: path.resolve(this.options.srcDir, 'static', 'sw.js'), | ||
directoryIndex: '/', | ||
@@ -53,23 +74,36 @@ cacheId: process.env.npm_package_name + '_' + process.env.npm_package_version, | ||
] | ||
}, options) | ||
}, moduleOptions, this.options.workbox) | ||
// Use nuxt plugins to prevent race conditions with webpack plugin | ||
// (https://github.com/nuxt-community/modules/issues/110) | ||
this.nuxt.plugin('build', builder => { | ||
builder.plugin('built', () => { | ||
if (workboxOptions.swSrc) { | ||
return swBuild.injectManifest(workboxOptions) | ||
} else { | ||
return swBuild.generateSW(workboxOptions) | ||
return options | ||
} | ||
// ============================================= | ||
// addTemplates | ||
// ============================================= | ||
function addTemplates (options) { | ||
// Add sw.template.js | ||
this.addTemplate({ | ||
src: path.resolve(__dirname, 'templates/sw.template.js'), | ||
fileName: 'sw.template.js', | ||
options: { | ||
importScripts: [options.wbDst].concat(options.importScripts || []), | ||
runtimeCaching: options.runtimeCaching, | ||
wbOptions: { | ||
cacheId: options.cacheId, | ||
clientsClaim: options.clientsClaim, | ||
directoryIndex: options.directoryIndex | ||
} | ||
}) | ||
} | ||
}) | ||
// Register runtime plugin | ||
// Add sw.plugin.js | ||
const swURL = `${options.routerBase}/${options.swURL || 'sw.js'}` | ||
this.addPlugin({ | ||
src: path.resolve(__dirname, 'plugin.js'), | ||
src: path.resolve(__dirname, 'templates/sw.plugin.js'), | ||
ssr: false, | ||
fileName: 'sw.plugin.js', | ||
options: { | ||
swURL: fixUrl(`${routerBase}/${swFileName}`), | ||
swScope: fixUrl(`${routerBase}/`) | ||
swURL: fixUrl(swURL), | ||
swScope: fixUrl(`${options.routerBase}/`) | ||
} | ||
@@ -79,2 +113,59 @@ }) | ||
// ============================================= | ||
// emitAssets | ||
// ============================================= | ||
function emitAssets (options) { | ||
const assets = [] | ||
const emitAsset = (path, name, ext = 'js') => { | ||
const source = readFileSync(path) | ||
const hash = hashSum(source) | ||
const dst = `${name}.${hash}.${ext}` | ||
assets.push({source, dst}) | ||
return dst | ||
} | ||
// Write assets after build | ||
const hook = builder => { | ||
assets.forEach(({source, dst}) => { | ||
writeFileSync(path.resolve(this.options.buildDir, 'dist', dst), source, 'utf-8') | ||
}) | ||
} | ||
if (this.nuxt.hook) { | ||
this.nuxt.hook('build:done', hook) | ||
} else { | ||
this.nuxt.plugin('build', builder => { | ||
builder.plugin('built', hook) | ||
}) | ||
} | ||
// workbox.js | ||
let wbPath = require.resolve('workbox-sw') | ||
if (options.dev) { | ||
wbPath = wbPath.replace(/prod/g, 'dev') | ||
} | ||
options.wbDst = fixUrl(options.publicPath + '/' + emitAsset(wbPath, 'workbox' + (options.dev ? '.dev' : ''))) | ||
} | ||
// ============================================= | ||
// workboxInject | ||
// ============================================= | ||
function workboxInject (options) { | ||
const hook = () => { | ||
const opts = Object.assign({}, options) | ||
delete opts.runtimeCaching | ||
return swBuild.injectManifest(opts) | ||
} | ||
if (this.nuxt.hook) { | ||
this.nuxt.hook('build:done', hook) | ||
} else { | ||
this.nuxt.plugin('build', builder => { | ||
builder.plugin('built', hook) | ||
}) | ||
} | ||
} | ||
module.exports.meta = require('./package.json') |
{ | ||
"name": "@nuxtjs/workbox", | ||
"version": "1.2.2", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": "https://github.com/nuxt-community/pwa", | ||
"repository": "https://github.com/nuxt-community/pwa-module", | ||
"publishConfig": { | ||
@@ -11,4 +11,4 @@ "access": "public" | ||
"dependencies": { | ||
"workbox-build": "^1.3.0" | ||
"workbox-build": "^2.1.1" | ||
} | ||
} |
@@ -1,1 +0,1 @@ | ||
Please refer to [nuxt-community/pwa](https://github.com/nuxt-community/pwa) for documentation. | ||
👉 Please refer to [nuxt-community/pwa-module](https://github.com/nuxt-community/pwa-module) for documentation. |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
11240
6
164
2
4
+ Addedworkbox-build@2.1.3(transitive)
+ Addedworkbox-sw@2.1.3(transitive)
- Removedworkbox-build@1.3.0(transitive)
- Removedworkbox-sw@1.3.0(transitive)
Updatedworkbox-build@^2.1.1