Skip to content

Commit 9095483

Browse files
jeffposnickyyx990803
authored andcommittedFeb 6, 2018
feat: Use the Workbox webpack plugin in pwa template (#769)
* feat: Use the Workbox webpack plugin in pwa template * feat: Added pwa template configuration docs.
1 parent 67df3eb commit 9095483

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed
 
+44
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
11
# @vue/cli-plugin-pwa
22

33
> pwa plugin for vue-cli
4+
5+
## Configuration
6+
7+
Configuration is handled via the `pwa` property of either the `vue.config.js`
8+
file, or the `"vue"` field in `package.json`.
9+
10+
### pwa.workboxPluginMode
11+
12+
This allows you to the choose between the two modes supported by the underlying
13+
[`workbox-webpack-plugin`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin).
14+
15+
- `'GenerateSW'` (default), will lead to a new service worker file being created
16+
each time you rebuild your web app.
17+
18+
- `'InjectManifest'` allows you to start with an existing service worker file,
19+
and creates a copy of that file with a "precache manifest" injected into it.
20+
21+
The "[Which Plugin to Use?](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#which_plugin_to_use)"
22+
guide can help you choose between the two modes.
23+
24+
### pwa.workboxOptions
25+
26+
These options are passed on through to the underlying `workbox-webpack-plugin`.
27+
28+
For more information on what values are supported, please see the guide for
29+
[`GenerateSW`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#full_generatesw_config)
30+
or for [`InjectManifest`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#full_injectmanifest_config).
31+
32+
### Example Configuration
33+
34+
```js
35+
// Inside vue.config.js
36+
module.exports = {
37+
// ...other vue-cli plugin options...
38+
pwa: {
39+
workboxPluginMode: 'InjectManifest',
40+
workboxOptions: {
41+
// swSrc is required in InjectManifest mode.
42+
swSrc: 'dev/sw.js',
43+
// ...other Workbox options...
44+
},
45+
},
46+
};
47+
```

‎packages/@vue/cli-plugin-pwa/index.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,39 @@ module.exports = (api, options) => {
22
api.chainWebpack(webpackConfig => {
33
const name = api.service.pkg.name
44

5+
const userOptions = options.pwa || {}
6+
57
// the pwa plugin hooks on to html-webpack-plugin
68
// and injects icons, manifest links & other PWA related tags into <head>
79
webpackConfig
810
.plugin('pwa')
911
.use(require('./lib/HtmlPwaPlugin'), [Object.assign({
1012
name
11-
}, options.pwa)])
13+
}, userOptions)])
1214

1315
// generate /service-worker.js in production mode
1416
if (process.env.NODE_ENV === 'production') {
15-
webpackConfig
16-
.plugin('sw-precache')
17-
.use(require('sw-precache-webpack-plugin'), [{
18-
cacheId: name,
19-
filename: 'service-worker.js',
20-
staticFileGlobs: [`${options.outputDir}/**/*.{js,html,css}`],
21-
minify: true,
22-
stripPrefix: `${options.outputDir}/`
23-
}])
17+
// Default to GenerateSW mode, though InjectManifest also might be used.
18+
const workboxPluginMode = userOptions.workboxPluginMode || 'GenerateSW'
19+
const workboxWebpackModule = require('workbox-webpack-plugin')
20+
if (workboxPluginMode in workboxWebpackModule) {
21+
const workBoxConfig = Object.assign({
22+
cacheId: name,
23+
exclude: [
24+
new RegExp('\.map$'),
25+
new RegExp('img/icons/'),
26+
new RegExp('favicon\.ico$'),
27+
new RegExp('manifest\.json$')
28+
]
29+
}, userOptions.workboxOptions)
30+
31+
webpackConfig
32+
.plugin('workbox')
33+
.use(workboxWebpackModule[workboxPluginMode], [workBoxConfig])
34+
} else {
35+
throw new Error(`${workboxPluginMode} is not a supported Workbox webpack plugin mode. ` +
36+
`Valid modes are: ${Object.keys(workboxWebpackModule).join(', ')}`)
37+
}
2438
}
2539
})
2640

‎packages/@vue/cli-plugin-pwa/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"access": "public"
2323
},
2424
"dependencies": {
25-
"sw-precache-webpack-plugin": "^0.11.4"
25+
"workbox-webpack-plugin": "3.0.0-beta.0"
2626
},
2727
"devDependencies": {
2828
"register-service-worker": "^1.0.0"

0 commit comments

Comments
 (0)
Please sign in to comment.