Permalink
Browse files

feat: Add compatibility for nuxt 1.0.0 hooks

  • Loading branch information...
pi0 committed Nov 16, 2017
1 parent e3c1be2 commit c0efb1d2f9923f227ffbb5421ec285c97d3daca6
Showing with 38 additions and 16 deletions.
  1. +4 −2 packages/icon/index.js
  2. +4 −2 packages/manifest/index.js
  3. +4 −2 packages/meta/index.js
  4. +26 −10 packages/workbox/index.js
@@ -7,10 +7,12 @@ const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
module.exports = function nuxtIcon (options) {
this.nuxt.plugin('build', builder => {
const hook = builder => {
debug('Adding icons')
return generateIcons.call(this, options)
})
}
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook)
}
function generateIcons (options) {
@@ -6,10 +6,12 @@ const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
const find = (arr, key, val) => arr.find(obj => val ? obj[key] === val : obj[key])
module.exports = function nuxtManifest (options) {
this.nuxt.plugin('build', builder => {
const hook = builder => {
debug('Adding manifest')
addManifest.call(this, options)
})
}
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook)
}
function addManifest (options) {
@@ -3,10 +3,12 @@ const debug = require('debug')('nuxt:pwa')
const find = (arr, key, val) => arr.find(obj => val ? obj[key] === val : obj[key])
module.exports = function nuxtMeta (_options) {
this.nuxt.plugin('build', builder => {
const hook = builder => {
debug('Adding meta')
generateMeta.call(this, _options)
})
}
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook)
}
function generateMeta (_options) {
@@ -19,13 +19,15 @@ module.exports = function nuxtWorkbox (moduleOptions) {
return
}
this.nuxt.plugin('build', builder => {
const hook = builder => {
debug('Adding workbox')
getRouterBase.call(this, ctx)
workboxInject.call(this, ctx)
emitAssets.call(this, ctx)
addTemplates.call(this, ctx)
})
}
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook)
}
// =============================================
@@ -89,13 +91,21 @@ function emitAssets (ctx) {
}
// Write assets after build
this.nuxt.plugin('build', builder => {
const hook = builder => {
builder.plugin('built', () => {
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')
@@ -137,13 +147,19 @@ function workboxInject (ctx) {
]
}, ctx.options)
this.nuxt.plugin('build', builder => {
builder.plugin('built', () => {
const opts = Object.assign({}, ctx.workboxOptions)
delete opts.runtimeCaching
return swBuild.injectManifest(opts)
const hook = () => {
const opts = Object.assign({}, ctx.workboxOptions)
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')

0 comments on commit c0efb1d

Please sign in to comment.