[React] Override webpack config for create-react-app without ejection
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
[React] Override webpack config for create-react-app without ejection的更多相关文章
- react+babel+webpack初试
在上一篇,我们简单学习了webpack学习,现在这里我们简单学习一下react+babel+webpack,进行编译react语法jsx以及结合es6写法. 这里我就简单的直接上demo: packa ...
- 如何扩展 Create React App 的 Webpack 配置
如何扩展 Create React App 的 Webpack 配置 原文地址https://zhaozhiming.github.io/blog/2018/01/08/create-react-a ...
- 使用create react app教程
This project was bootstrapped with Create React App. Below you will find some information on how to ...
- 深入 Create React App 核心概念
本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...
- 在 .NET Core 5 中集成 Create React app
翻译自 Camilo Reyes 2021年2月22日的文章 <Integrate Create React app with .NET Core 5> [1] Camilo Reyes ...
- react webpack.config.js 入门学习
在学习react 的时候必然会用到webpack打包工具,webpack的快速入门另外一篇文章中有记录,这里只记录webpack.config.js文件,因为每个项目下都必须配置,通俗的讲,它的作用就 ...
- Create React App 安装less 报错
执行npm run eject 暴露模块 安装 npm i less less-loader -D 1.打开 react app 的 webpack.config.js const sassRege ...
- tap news:week5 0.0 create react app
参考https://blog.csdn.net/qtfying/article/details/78665664 先创建文件夹 安装create react app 这个脚手架(facebook官方提 ...
- 利用 Create React Native App 快速创建 React Native 应用
本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...
随机推荐
- java的struts2整理
readbook:struts2 先说一下struts1: struts1使用model II 模式开发,即jsp+java bean+servlet 再说它的缺陷: 1.表现层支持 ...
- Android ANR 详解
ANR简介 ANR,是“Application Not Responding”的缩写,即“应用程序无响应”.在Android中,ActivityManagerService(简称AMS)和Window ...
- 一种机制,与js类似
我们知道,当两个条件进行逻辑与操作的时候,其中任何一个条件为假,则表达式的结果为假.所以,遇到(A 且 B)这种表达式,如果A为假的话,B是不是真假都无所谓了,当遇到一个假条件的时候,程序也就没有必要 ...
- 获得NOTEPAD++ Download Manager的所有下载列表的内容的au3脚本
;~ 获得NOTEPAD++ Download Manager的所有下载列表的内容的au3脚本 ;~ 作者: 鹏程万里 ;~ Email:aprial@163.com ;~ 创建日期: 2014年11 ...
- undefined reference to XXX 问题原因
原文地址:http://blog.csdn.net/cserchen/article/details/5503556 Linux下编译程序时,经常会遇到“undefined reference to ...
- asp.net core 身份认证/权限管理系统简介及简单案例
如今的网站大多数都离不开账号注册及用户管理,而这些功能就是通常说的身份验证.这些常见功能微软都为我们做了封装,我们只要利用.net core提供的一些工具就可以很方便的搭建适用于大部分应用的权限管理系 ...
- python实现无重复字符串的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...
- 洛谷——P1348 Couple number
P1348 Couple number 题目描述 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple number.你的工作就是判断一个数N是不是Couple ...
- view update
CREATE OR REPLACE VIEW [Current Product List] ASSELECT ProductID, ProductName, CategoryFROM Products ...
- java Iterable
Iterable