webpack-sw-plugin
Version: 
plugin for webpack4 to build a ServiceWorker webapp quickly and easily
97 lines (68 loc) • 2.13 kB
Markdown
> plugin for webpack4 , Be quickly and easily to build a ServiceWorker webapp you can access it offline
[](https://www.npmjs.com/package/webpack-sw-plugin) 
[简体中文](https://github.com/violinux666/webpack-sw-plugin/blob/master/README.zh_CN.md)|[English](https://github.com/violinux666/webpack-sw-plugin)
- Easy to use with webpack4
- No ServiceWorker file is required
- Provider a callback API that do something when the webpack output file is changed
```bash
npm install --save-dev webpack-sw-plugin
```
```
npm run example
```
and then, open localhost:3000
webpack.config.js
```jsx
const WebpackSWPlugin = require('webpack-sw-plugin');
module.exports = {
    // entry
    // output
    plugins:[
        new WebpackSWPlugin()
    ]
}
```
client
```jsx
import worker from 'webpack-sw-plugin/lib/worker';
worker.register();
```
You can pass a configuration options to webpack-sw-plugin.
```js
plugins:[
    new WebpackSWPlugin({
        filename: "test-sw.js",
        minify: true
    })
]
```
- **filename**: The output serviceworker file name. default is 'service-worker-builder.js'.
- **minify**: controls if we need a minified sw file. would be true if the mode is production .
when the webpack output file has benn changed, we provide a callback API you can do something
```jsx
import worker from 'webpack-sw-plugin/lib/worker';
worker.register({
    onUpdate:()=>{
        const test="Page has a new version, refresh the page?";
        var result=confirm(test);
        if(result){
            window.location.reload();
        }
    }
});
```
onUpdate will be triggered if webpack output file has been changed.
For example, page will show a dialog, page will use the newest bundle file after refresh
- Contact me on iewnap@outlook.com
- raise an issue on Github.[Submit a issue](https://github.com/violinux666/webpack-sw-plugin/issues/new)
MIT © [violinux666](https://github.com/violinux666)