forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (62 loc) · 2.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const fs = require('fs')
const { chalk, warn } = require('@vue/cli-shared-utils')
module.exports = (api, options) => {
const userOptions = options.pwa || {}
const manifestPath = api.resolve('public/manifest.json')
if (fs.existsSync(manifestPath)) {
if (!userOptions.manifestOptions) {
userOptions.manifestOptions = require(manifestPath)
} else {
warn(
`The ${chalk.red('public/manifest.json')} file will be ignored in favor of ${chalk.cyan('pwa.manifestOptions')}`
)
}
}
api.chainWebpack(webpackConfig => {
const target = process.env.VUE_CLI_BUILD_TARGET
if (target && target !== 'app') {
return
}
const name = api.service.pkg.name
// the pwa plugin hooks on to html-webpack-plugin
// and injects icons, manifest links & other PWA related tags into <head>
webpackConfig
.plugin('pwa')
.use(require('./lib/HtmlPwaPlugin'), [Object.assign({
name
}, userOptions)])
.after('html')
// generate /service-worker.js in production mode
if (process.env.NODE_ENV === 'production') {
// Default to GenerateSW mode, though InjectManifest also might be used.
const workboxPluginMode = userOptions.workboxPluginMode || 'GenerateSW'
const workboxWebpackModule = require('workbox-webpack-plugin')
if (!(workboxPluginMode in workboxWebpackModule)) {
throw new Error(
`${workboxPluginMode} is not a supported Workbox webpack plugin mode. ` +
`Valid modes are: ${Object.keys(workboxWebpackModule).join(', ')}`
)
}
const defaultOptions = {
exclude: [
/\.map$/,
/img\/icons\//,
/favicon\.ico$/,
/^manifest.*\.js?$/
]
}
const defaultGenerateSWOptions = workboxPluginMode === 'GenerateSW' ? {
cacheId: name
} : {}
const workBoxConfig = Object.assign(defaultOptions, defaultGenerateSWOptions, userOptions.workboxOptions)
webpackConfig
.plugin('workbox')
.use(workboxWebpackModule[workboxPluginMode], [workBoxConfig])
}
})
// install dev server middleware for resetting service worker during dev
const createNoopServiceWorkerMiddleware = require('./lib/noopServiceWorkerMiddleware')
api.configureDevServer(app => {
app.use(createNoopServiceWorkerMiddleware())
})
}