timarney /react-app-rewired

Override create-react-app webpack configs without ejecting

★ 9,836⑂ 418👁 85⑃ 254● 418▤ 13
JavaScriptMITCreated 2016-08-08
下载:2.2.12.2.1 ↓2.2.12022-02-15
+0
日增长
9,836
stars
418
forks
85
watchers
254
prs
418
issues
13
releases

README

ℹ️
请在提 issue 之前确保 issue 是关于项目代码,而不是 我要怎么配置 Webpack。(配置相关的问题可以在 Stack Overflow 或者 Spectrum 上提问)。

- 🚨 2.0版本移除了rewire helper 功能




 

此工具可以在不 'eject' 也不创建额外 react-scripts 的情况下修改 create-react-app 内置的 webpack 配置,然后你将拥有 create-react-app 的一切特性,且可以根据你的需要去配置 webpack 的 plugins, loaders 等。

 Rewire你的应用 ☠
从 Create React App 2.0开始,这个仓库主要由社区“轻度”维护。

⚠️ 注意

 使用了 react-app-rewired 之后,等于你得到了项目的配置权,但这表示你的项目将无法得到 CRA 提供的配置“保证”,希望你知道自己要做什么。

"Stuff can break" — Dan Abramov https://twitter.com/dan_abramov/status/1045809734069170176



注意: 我个人使用next.js 或者 Razzle 都支持开箱即用的自定义 Webpack。

 备选方案

您可以尝试使用 customize-cra 来获得一组 CRA 2.0兼容的 rewirers,或任何旨在支持2.0的替代项目和分支:
- Rescripts,用于扩展 CRA 配置的替代框架(支持2.0+)
- react-scripts-rewired 为该项目的一个分支,旨在支持 CRA 2.0
- craco

 如何rewire create-react-app项目
 使用 create-react-app 创建您的应用,然后 rewire

 1) 安装 react-app-rewired

 对于使用 Webpack 4 的 create-react-app 2.x

bash
$ npm install react-app-rewired --save-dev


 对于 create-react-app 1.x 或 react-scripts-ts 与 Webpack 3:
bash
$ npm install [email protected] --save-dev


 2) 在根目录中创建一个 config-overrides.js 文件

javascript
/ config-overrides.js /
module.exports = function override(config, env) {
  //do stuff with the webpack config...
  return config;
}



+-- your-project
|   +-- config-overrides.js
|   +-- node_modules
|   +-- package.json
|   +-- public
|   +-- README.md
|   +-- src


  3) 替换 package.json 中 scripts 执行部分

diff
  / package.json /

  "scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test --env=jsdom",
+   "test": "react-app-rewired test --env=jsdom",
    "eject": "react-scripts eject"
}

 注意:不用替换 eject 部分。工具中没有针对 eject 的配置替换,执行了 eject 命令会让工具失去作用(能找到这个插件我也相信你知道 eject 是干嘛的)。

 4) 启动 Dev Server

bash
$ npm start


 5) 构建你的应用程序

bash
$ npm run build


 扩展配置选项
您可以为其设置自定义路径 config-overrides.js
 如果您想要使用在 node_modules 中的第三方 config-overrides.js,您可以将以下内容添加到您的package.json:


json
"config-overrides-path": "node_modules/some-preconfigured-rewire"


默认情况下,该 config-overrides.js 文件导出单个函数,以便在开发或生产模式下自定义 webpack 配置, 方便编译您的 react 应用程序。可以从该文件中导出一个包含最多三个字段的对象,每个字段都是一个函数。这种替代形式允许您自定义用于 Jest(在测试中)和 Webpack Dev Server 本身的配置。
此示例实现用于演示使用每个对象需要的函数。在此示例中,展示的功能是:
 根据 .env 变量有条件地运行一些测试
 设置用于 Development Server 的 https 证书,.env 文件变量中指定的文件名。

javascript
module.exports = {
  // The Webpack config to use when compiling your react app for development or production.
  webpack: function(config, env) {
    // ...add your webpack config
    return config;
  },
  // The Jest config to use when running your jest tests - note that the normal rewires do not
  // work here.
  jest: function(config) {
    // ...add your jest config customisation...
    // Example: enable/disable some tests based on environment variables in the .env file.
    if (!config.testPathIgnorePatterns) {
      config.testPathIgnorePatterns = [];
    }
    if (!process.env.RUN_COMPONENT_TESTS) {
      config.testPat

Top issues

Latest release

2.2.1 v2.2.1 2022-02-15
https://github.com/timarney/react-app-rewired/pull/606
Add /config folder to package.json files array

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON