Skip to content

Commit c0efb1d

Browse files
author
Pooya Parsa
committedNov 16, 2017
feat: Add compatibility for nuxt 1.0.0 hooks
1 parent e3c1be2 commit c0efb1d

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed
 

Diff for: ‎packages/icon/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
77
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
88

99
module.exports = function nuxtIcon (options) {
10-
this.nuxt.plugin('build', builder => {
10+
const hook = builder => {
1111
debug('Adding icons')
1212
return generateIcons.call(this, options)
13-
})
13+
}
14+
15+
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook)
1416
}
1517

1618
function generateIcons (options) {

Diff for: ‎packages/manifest/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
66
const find = (arr, key, val) => arr.find(obj => val ? obj[key] === val : obj[key])
77

88
module.exports = function nuxtManifest (options) {
9-
this.nuxt.plugin('build', builder => {
9+
const hook = builder => {
1010
debug('Adding manifest')
1111
addManifest.call(this, options)
12-
})
12+
}
13+
14+
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook)
1315
}
1416

1517
function addManifest (options) {

Diff for: ‎packages/meta/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ const debug = require('debug')('nuxt:pwa')
33
const find = (arr, key, val) => arr.find(obj => val ? obj[key] === val : obj[key])
44

55
module.exports = function nuxtMeta (_options) {
6-
this.nuxt.plugin('build', builder => {
6+
const hook = builder => {
77
debug('Adding meta')
88
generateMeta.call(this, _options)
9-
})
9+
}
10+
11+
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook)
1012
}
1113

1214
function generateMeta (_options) {

Diff for: ‎packages/workbox/index.js

+26-10
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ module.exports = function nuxtWorkbox (moduleOptions) {
1919
return
2020
}
2121

22-
this.nuxt.plugin('build', builder => {
22+
const hook = builder => {
2323
debug('Adding workbox')
2424
getRouterBase.call(this, ctx)
2525
workboxInject.call(this, ctx)
2626
emitAssets.call(this, ctx)
2727
addTemplates.call(this, ctx)
28-
})
28+
}
29+
30+
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook)
2931
}
3032

3133
// =============================================
@@ -89,13 +91,21 @@ function emitAssets (ctx) {
8991
}
9092

9193
// Write assets after build
92-
this.nuxt.plugin('build', builder => {
94+
const hook = builder => {
9395
builder.plugin('built', () => {
9496
assets.forEach(({source, dst}) => {
9597
writeFileSync(path.resolve(this.options.buildDir, 'dist', dst), source, 'utf-8')
9698
})
9799
})
98-
})
100+
}
101+
102+
if (this.nuxt.hook) {
103+
this.nuxt.hook('build.done', hook)
104+
} else {
105+
this.nuxt.plugin('build', builder => {
106+
builder.plugin('built', hook)
107+
})
108+
}
99109

100110
// workbox.js
101111
let wbPath = require.resolve('workbox-sw')
@@ -137,13 +147,19 @@ function workboxInject (ctx) {
137147
]
138148
}, ctx.options)
139149

140-
this.nuxt.plugin('build', builder => {
141-
builder.plugin('built', () => {
142-
const opts = Object.assign({}, ctx.workboxOptions)
143-
delete opts.runtimeCaching
144-
return swBuild.injectManifest(opts)
150+
const hook = () => {
151+
const opts = Object.assign({}, ctx.workboxOptions)
152+
delete opts.runtimeCaching
153+
return swBuild.injectManifest(opts)
154+
}
155+
156+
if (this.nuxt.hook) {
157+
this.nuxt.hook('build.done', hook)
158+
} else {
159+
this.nuxt.plugin('build', builder => {
160+
builder.plugin('built', hook)
145161
})
146-
})
162+
}
147163
}
148164

149165
module.exports.meta = require('./package.json')

0 commit comments

Comments
 (0)
Please sign in to comment.