Skip to content

Commit

Permalink
feat: Add compatibility for nuxt 1.0.0 hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Nov 16, 2017
1 parent e3c1be2 commit c0efb1d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
6 changes: 4 additions & 2 deletions packages/icon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions packages/manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions packages/meta/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 26 additions & 10 deletions packages/workbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

// =============================================
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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.