@@ -19,13 +19,15 @@ module.exports = function nuxtWorkbox (moduleOptions) {
19
19
return
20
20
}
21
21
22
- this . nuxt . plugin ( 'build' , builder => {
22
+ const hook = builder => {
23
23
debug ( 'Adding workbox' )
24
24
getRouterBase . call ( this , ctx )
25
25
workboxInject . call ( this , ctx )
26
26
emitAssets . call ( this , ctx )
27
27
addTemplates . call ( this , ctx )
28
- } )
28
+ }
29
+
30
+ this . nuxt . hook ? this . nuxt . hook ( 'build:before' , hook ) : this . nuxt . plugin ( 'build' , hook )
29
31
}
30
32
31
33
// =============================================
@@ -89,13 +91,21 @@ function emitAssets (ctx) {
89
91
}
90
92
91
93
// Write assets after build
92
- this . nuxt . plugin ( 'build' , builder => {
94
+ const hook = builder => {
93
95
builder . plugin ( 'built' , ( ) => {
94
96
assets . forEach ( ( { source, dst} ) => {
95
97
writeFileSync ( path . resolve ( this . options . buildDir , 'dist' , dst ) , source , 'utf-8' )
96
98
} )
97
99
} )
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
+ }
99
109
100
110
// workbox.js
101
111
let wbPath = require . resolve ( 'workbox-sw' )
@@ -137,13 +147,19 @@ function workboxInject (ctx) {
137
147
]
138
148
} , ctx . options )
139
149
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 )
145
161
} )
146
- } )
162
+ }
147
163
}
148
164
149
165
module . exports . meta = require ( './package.json' )
0 commit comments