ℹ️ Before submitting an issue to this repo - Ensure it's a issue with the code in this repo, not a how do I configure something with Webpack question (post something on Stack Overflow or Spectrum). It's your config you "own" it.
Tweak the create-react-app webpack config(s) without using 'eject' and without creating a fork of the react-scripts.
All the benefits of create-react-app without the limitations of "no config". You can add plugins, loaders whatever you need.
As of Create React App 2.0 this repo is "lightly" maintained mostly by the community at this point.
By doing this you're breaking the "guarantees" that CRA provides. That is to say you now "own" the configs. No support will be provided. Proceed with caution.
"Stuff can break" — Dan Abramovhttps://twitter.com/dan_abramov/status/1045809734069170176
Note: I personally use next.js or Razzle which both support custom Webpack out of the box.
You can try customize-cra for a set of CRA 2.0 compatible rewirers,or any of the alternative projects and forks that aim to support 2.0:
Create your app using create-react-app and then rewire it.
$ npm install react-app-rewired --save-dev
$ npm install react-app-rewired@1.6.2 --save-dev
config-overrides.js
file in the root directory/* 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
react-scripts
in npm
scripts for start, build and test/* 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",
+ "test": "react-app-rewired test",
"eject": "react-scripts eject"
}
Note: Do NOT flip the call for the eject
script.That gets run only once for a project, after which you are given full control over the webpack configuration making react-app-rewired
no longer required.There are no configuration options to rewire for the eject
script.
$ npm start
$ npm run build
You can set a custom path for config-overrides.js
. If you (for instance) wanted to use a 3rd-party config-overrides.js
that exists in node_modules
, you could add the following to your package.json
:
"config-overrides-path": "node_modules/some-preconfigured-rewire"
By default, the config-overrides.js
file exports a single function to use when customising the webpack configuration for compiling your react app in development or production mode. It is possible to instead export an object from this file that contains up to three fields, each of which is a function. This alternative form allows you to also customise the configuration used for Jest (in testing), and for the Webpack Dev Server itself.
This example implementation is used to demonstrate using each of the object require functions. In the example, the functions:
.env
variables.env
file variables.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.testPathIgnorePatterns.push('<rootDir>/src/components/**/*.test.js');
}
if (!process.env.RUN_REDUCER_TESTS) {
config.testPathIgnorePatterns.push('<rootDir>/src/reducers/**/*.test.js');
}
return config;
},
// The function to use to create a webpack dev server configuration when running the development
// server with 'npm run start' or 'yarn start'.
// Example: set the dev server to use a specific certificate in https.
devServer: function(configFunction) {
// Return the replacement function for create-react-app to use to generate the Webpack
// Development Server config. "configFunction" is the function that would normally have
// been used to generate the Webpack Development server config - you can use it to create
// a starting configuration to then modify instead of having to create a config from scratch.
return function(proxy, allowedHost) {
// Create the default config by calling configFunction with the proxy/allowedHost parameters
const config = configFunction(proxy, allowedHost);
// Change the https certificate options to match your certificate, using the .env file to
// set the file paths & passphrase.
const fs = require('fs');
config.https = {
key: fs.readFileSync(process.env.REACT_HTTPS_KEY, 'utf8'),
cert: fs.readFileSync(process.env.REACT_HTTPS_CERT, 'utf8'),
ca: fs.readFileSync(process.env.REACT_HTTPS_CA, 'utf8'),
passphrase: process.env.REACT_HTTPS_PASS
};
// Return your customised Webpack Development Server config.
return config;
};
},
// The paths config to use when compiling your react app for development or production.
paths: function(paths, env) {
// ...add your paths config
return paths;
},
}
The webpack
field is used to provide the equivalent to the single-function exported from config-overrides.js. This is where all the usual rewires are used. It is not able to configure compilation in test mode because test mode does not get run through Webpack at all (it runs in Jest). It is also not able to be used to customise the Webpack Dev Server that is used to serve pages in development mode because create-react-app generates a separate Webpack configuration for use with the dev server using different functions and defaults.
Webpack is not used for compiling your application in Test mode - Jest is used instead. This means that any rewires specified in your webpack config customisation function will not be applied to your project in test mode.
React-app-rewired automatically allows you to customise your Jest configuration in a jest
section of your package.json
file, including allowing you to set configuration fields that create-react-app would usually block you from being able to set. It also automatically sets up Jest to compile the project with Babel prior to running tests. Jest's configuration options are documented separately at the Jest website. Note: Configuration arrays and objects are merged, rather than overwritten. See #240 and #241 for details
If you want to add plugins and/or presets to the Babel configuration that Jest will use, you need to define those plugins/presets in either a babel
section inside the package.json
file or inside a .babelrc
file. React-app-rewired alters the Jest configuration to use these definition files for specifying Babel options when Jest is compiling your react app. The format to use in the Babel section of package.json or the .babelrc file is documented separately at the Babel website.
The jest
field in the module.exports object in config-overrides.js
is used to specify a function that can be called to customise the Jest testing configuration in ways that are not possible in the jest section of the package.json file. For example, it will allow you to change some configuration options based on environment variables. This function is passed the default create-react-app Jest configuration as a parameter and is required to return the modified Jest configuration that you want to use. A lot of the time you'll be able to make the configuration changes needed simply by using a combination of the package.json
file's jest section and a .babelrc
file (or babel section in package.json) instead of needing to provide this jest function in config-overrides.js
.
When running in development mode, create-react-app does not use the usual Webpack config for the Development Server (the one that serves the app pages). This means that you cannot use the normal webpack
section of the config-overrides.js
server to make changes to the Development Server settings as those changes won't be applied.
Instead of this, create-react-app expects to be able to call a function to generate the webpack dev server when needed. This function is provided with parameters for the proxy and allowedHost settings to be used in the webpack dev server (create-react-app retrieves the values for those parameters from your package.json file).
React-app-rewired provides the ability to override this function through use of the devServer
field in the module.exports object in config-overrides.js
. It provides the devServer function a single parameter containing the default create-react-app function that is normally used to generate the dev server config (it cannot provide a generated version of the configuration because react-scripts is calling the generation function directly). React-app-rewired needs to receive as a return value a replacement function for create-react-app to then use to generate the Development Server configuration (i.e. the return value should be a new function that takes the two parameters for proxy and allowedHost and itself returns a Webpack Development Server configuration). The original react-scripts function is passed into the config-overrides.js
devServer function so that you are able to easily call this yourself to generate your initial devServer configuration based on what the defaults used by create-react-app are.
The paths
field is used to provide overrides for the create-react-app
paths passed into webpack and jest.
Some third party tools, like react-cosmos
relies on your webpack config.You can create webpack.config.js
file and export rewired config using following snippet:
const { paths } = require('react-app-rewired');
// require normalized overrides
const overrides = require('react-app-rewired/config-overrides');
const config = require(paths.scriptVersion + '/config/webpack.config.dev');
module.exports = overrides.webpack(config, process.env.NODE_ENV);
Then just point to this file in tool configuration.
At this point in time, it is difficult to change the entry point from the default src/index.js
file due to the way that file is included by create-react-app. The normal rewiring process gets bypassed by several of the create-react-app scripts.
There are three work-arounds available here:
require('./index.tsx');
react-dev-utils/checkRequiredFiles
function to always return true (causing create-react-app to no longer try to enforce that the entry file must exist).It is possible to use a custom version of the react-scripts
package with react-app-rewired by specifying the name of the scripts package in the command line option --scripts-version
or setting REACT_SCRIPTS_VERSION=<...>
via the environment.
A working example for using the scripts version option is:
{
"scripts": {
"start": "react-app-rewired start --scripts-version react-scripts-ts",
"build": "react-app-rewired build --scripts-version react-scripts-ts",
"test": "react-app-rewired test --scripts-version react-scripts-ts",
"eject": "react-scripts eject"
}
}
React-app-rewired imports your config-overrides.js file without the '.js' extension. This means that you have the option of creating a directory called config-overrides
at the root of your project and exporting your overrides from the default index.js
file inside that directory.
If you have several custom overrides using a directory allows you to be able to put each override in a separate file. An example template that demonstrates this can be found in Guria/rewired-ts-boilerplate at Github.
If you need to change the location of your config-overrides.js you can pass a command line option --config-overrides to the react-app-rewired script.
When developing this project, ensure you have yarn installed.
To run the test app, navigate to the directory and run:
yarn setup
yarn start
(when you are finished, run yarn teardown
to clean up)
Here is a list of all the available commands to help you in development
yarn setup
- installs dependences and links test/react-app
yarn start
- starts the react appyarn build
- builds the react appyarn test
- tests the react appyarn teardown
- unlinks test/react-app
and removes dependencies使用 react-app-rewired 和 customize-cra 对 webpack进行配置定义 主要是用的插件react-app-rewired customize-cra 安装yarn add react-app-rewired customize-cra -D 首先先在package.json中配置 把react-scripts替换成为react-app-rewired "scrip
背景 基于 SpringBoot + React 前后端分离的工程,为了便于打包,通常需要配置 React 项目的打包目录为 SpringBoot 工程的 target/classes/static/static ,并在 pom.xml 打包插件中配置 prepare-package 阶段执行 npm run build 先打包前端 React 工程。 这样比从 dist 目录下拷贝前端文件到 s
mobx使用了装饰器语法。 不想用npm run eject把react的默认配置都暴露出来的方式来配置装饰器 为了搞这个,查了一晚上,从 react-app-rewired 查到customize-cra ,最后查了好久,搞定这个。 const {override, addDecoratorsLegacy} = require('customize-cra'); module.exports =
使用了 "scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-app-rewired eject" }, 后报错信息如下: internal/modules/cjs/loader.js
简述 两者使用为了修改webpack配置,无需eject 安装 npm i customize reacta-app-rewired -D 使用 新增config.overrides.js(package.json同级) /* * 基于customize 和 react-app-rewired的定制化配置文件 * */ // 引入相关的方法 const { override,
'react-app-rewired' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! my-app@ start: react-app-rewired start npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the my-app@
通过react-app-rewired插件,react-app-rewired的作用就是在不eject的情况下,覆盖create-react-app的配置。
项目引入antd-mobile;按官方文档介绍安装了react-app-rewired 2.0+ ;并且下载了customize-cra和babel-plugin-import来配置按需加载;也在.babelrc文件中配置了plugins 然鹅(当然没有然鹅,就没有这篇文章啦),运行项目后,控制台还是提示这个:You are using a whole package of antd, pleas
原文地址: http://www.linzichen.cn/article/1580763532192907264 前言 之前在用 react 脚手架 create-react-app 写项目时,发现在引入自定义组件或 js 的时候,不能够像 vue 似的通过别名 @ 来引入,如果涉及层级过深,那么一堆的相对路径 ../../ 也是颇为头痛,且及不雅观。 使用create-react-app 初始
背景 项目中需要获取到homepage字段,供动态加载js使用 方法 使用customize-cra中的addWebpackPlugin方法添加 // cofig-overrides.js addWebpackPlugin(new webpack.DefinePlugin({ "process.package.homepage": JSON.stringify(baseName) })
react-app-rewired降到2.0版本 cnpm uninstall react-app-rewired 用上面的语句删除原来的react-app-rewired cnpm i react-app-rewired@2.0.2-next.0 然后重新安装低版本的react-app-rewired
Error: Cannot find module 'react-dev-utils/crossSpawn’ 这是因为再高版本中 react-app-rewired 已经无从查询了,所以必须将 react-app-rewired 降级: npm i react-app-rewired@2.0.2-next.0 Error: Cannot find module 'react-scripts/pa
重新下载react-app-rewired yarn add react-app-rewired customize-cra
//项目创建 npx create-react-app projectname 1 yarn add react-app-rewired customize-cra 2 在项目根目录下创建一个config-overrides.js 修改package.json: "scripts": { - "start": "react-scripts start",
使用yarn作为包管理工具,安装yarn npm i yarn -g 安装项目所需的第三方包(使用antd要安装less与less-loader) yarn add react-router-dom axios less-loader less antd 需要在webpack中配置less,在项目文件中暴露webpack yarn eject 运行yarn eject可能出现问题,参考:npm r
React App SDK Everything you love about Create React App plusserver-side code support (SSR, GraphQL API, etc) and config overrides (Babel, Webpack, etc.). Seethe demo project. React App SDK is intende
Create React App 不用配置就可以创建 React App。 全局安装: npm install -g create-react-app 创建 App: create-react-app my-appcd my-app/ 启动 npm: npm start 打开 http://localhost:3000/ 查看你的 App。 如果你准备将其部署到生产环境,只需创建一个压缩包,并且运行 npm run build。
React.js Isomorphic Web Application Architecture Learn to build a complete production-level web app for a blogging platform like Medium, MindOrks, and FreeCodeCamp Demo web app running this project: G
react-native-dribbble-app 是 Facebook 员工使用 React Native 构建的 Dribbble 应用。
ML-React-App It's a template on which we can build a React app and call endpoints to make predictions. Usage The complete guide to use this repository: https://towardsdatascience.com/create-a-complete
react-native-app-intro react-native-app-intro is a react native component implementing a parallax effect welcome page using base on react-native-swiper , similar to the one found in Google's app like