博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React] Override webpack config for create-react-app without ejection
阅读量:6294 次
发布时间:2019-06-22

本文共 1580 字,大约阅读时间需要 5 分钟。

The default service worker that comes with create-react-app doesn't allow for very much configuration. We'll replace that default service worker in two ways.

First, we'll create a blank service worker js file, and use that as our custom service worker.

Next, we'll re-write the default webpack config with react-app-rewired, and utilize the InjectManifest workbox webpack plugin. This will allow us to create a totally custom service worker that still allows us to use workbox, without ejecting our app.

 

Install:

"react-app-rewired": "^1.6.2",    "react-scripts": "^2.1.1",    "serve": "^10.1.1",    "workbox-webpack-plugin": "^3.6.3"

 

Create a config-overrides.js in root folder:

Default create-react-app using 'GenerateSW' function, we want to override with 'InjectManifest' function. 

/* config-overrides.js */const WorkboxWebpackPlugin = require('workbox-webpack-plugin')module.exports = function override(config, env) {  config.plugins = config.plugins.map(plugin => {    if(plugin.constructor.name === 'GenerateSW') {      return new WorkboxWebpackPlugin.InjectManifest({        swSrc: './src/sw.js', // point to the sw.js file we will create later        swDest: 'service-worker.js' // will be generatedin pulbic folder      })    }    return plugin  })  return config}

 

Update package.json:

"scripts": {    "start": "react-app-rewired start",    "build": "react-app-rewired build",    "build:serve": "serve -s build",    "test": "react-app-rewired test",    "eject": "react-scripts eject"  },

 

Create src/sw.js:

workbox.skipWaiting();workbox.clientsClaim();

 

Run:

npm run build

 

转载地址:http://efvta.baihongyu.com/

你可能感兴趣的文章
Maven 插件
查看>>
初探Angular6.x---进入用户编辑模块
查看>>
计算机基础知识复习
查看>>
【前端词典】实现 Canvas 下雪背景引发的性能思考
查看>>
大佬是怎么思考设计MySQL优化方案的?
查看>>
<三体> 给岁月以文明, 给时光以生命
查看>>
Android开发 - 掌握ConstraintLayout(九)分组(Group)
查看>>
springboot+logback日志异步数据库
查看>>
Typescript教程之函数
查看>>
Android 高效安全加载图片
查看>>
vue中数组变动不被监测问题
查看>>
3.31
查看>>
类对象定义 二
查看>>
收费视频网站Netflix:用户到底想要“点”什么?
查看>>
MacOS High Sierra 12 13系统转dmg格式
查看>>
关于再次查看已做的多选题状态逻辑问题
查看>>
动态下拉菜单,非hover
查看>>
政府安全资讯精选 2017年第十六期 工信部发布关于规范互联网信息服务使用域名的通知;俄罗斯拟建立备用DNS;Google打击安卓应用在未经同意情况下收集个人信...
查看>>
简单易懂的谈谈 javascript 中的继承
查看>>
iOS汇编基础(四)指针和macho文件
查看>>