UNPKG

1.29 kBJavaScriptView Raw
1const path = require('path')
2
3const { getOptions } = require('./options')
4const { readJSFiles } = require('./utils')
5
6module.exports = function nuxtWorkbox (moduleOptions) {
7 this.nuxt.hook('build:before', () => {
8 // Get options
9 const options = getOptions.call(this, moduleOptions)
10
11 // Warning for dev option
12 if (options.dev) {
13 console.warn('`workbox.dev` enabled. Remember to clean application data and unregister service workers in your browser or you will experience infinity loop!')
14 }
15
16 // Register plugin
17 if (options.autoRegister) {
18 this.addPlugin({
19 src: path.resolve(__dirname, '../templates/sw.register.js'),
20 ssr: false,
21 fileName: 'sw.register.js',
22 options: {
23 ...options
24 }
25 })
26 }
27
28 // Add sw.js
29 if (options.swTemplate) {
30 this.addTemplate({
31 src: options.swTemplate,
32 fileName: options.swDest,
33 options: {
34 ...options,
35 routingExtensions: readJSFiles.call(this, options.routingExtensions),
36 cachingExtensions: readJSFiles.call(this, options.cachingExtensions),
37 workboxExtensions: readJSFiles.call(this, options.workboxExtensions)
38 }
39 })
40 }
41 })
42}
43
44module.exports.meta = require('../package.json')