Gathering detailed insights and metrics for react-app-rewired
Gathering detailed insights and metrics for react-app-rewired
Gathering detailed insights and metrics for react-app-rewired
Gathering detailed insights and metrics for react-app-rewired
react-app-rewire-hot-loader
Add react-hot-loader to a react-app-rewired config.
react-app-rewire-babel-loader
Rewire `babel-loader` loader in your `create-react-app` project using `react-app-rewired`.
react-app-rewire-yaml
Add yaml-loader to a react-app-rewired config.
react-app-rewire-webpack-bundle-analyzer
Add webpack-bundle-analyzer to a react-app-rewired config.
Override create-react-app webpack configs without ejecting
npm install react-app-rewired
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
Node Version
NPM Version
9,836 Stars
457 Commits
429 Forks
89 Watching
1 Branches
98 Contributors
Updated on 27 Nov 2024
JavaScript (83.95%)
HTML (10.46%)
CSS (5.59%)
Cumulative downloads
Total Downloads
Last day
6.4%
77,926
Compared to previous day
Last week
4.1%
389,231
Compared to previous week
Last month
5.3%
1,618,466
Compared to previous month
Last year
-11.8%
20,866,102
Compared to previous year
ℹ️ 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.
⚠️ Please Note:
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 Abramov https://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.
1npm install react-app-rewired --save-dev
1npm install react-app-rewired@1.6.2 --save-dev
config-overrides.js
file in the root directory1/* config-overrides.js */ 2 3module.exports = function override(config, env) { 4 //do stuff with the webpack config... 5 return config; 6}
+-- your-project
| +-- config-overrides.js
| +-- node_modules
| +-- package.json
| +-- public
| +-- README.md
| +-- src
react-scripts
in npm
scripts for start, build and test1 /* package.json */ 2 3 "scripts": { 4- "start": "react-scripts start", 5+ "start": "react-app-rewired start", 6- "build": "react-scripts build", 7+ "build": "react-app-rewired build", 8- "test": "react-scripts test", 9+ "test": "react-app-rewired test", 10 "eject": "react-scripts eject" 11}
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.
1npm start
1npm 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
:
1"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.1module.exports = { 2 // The Webpack config to use when compiling your react app for development or production. 3 webpack: function(config, env) { 4 // ...add your webpack config 5 return config; 6 }, 7 // The Jest config to use when running your jest tests - note that the normal rewires do not 8 // work here. 9 jest: function(config) { 10 // ...add your jest config customisation... 11 // Example: enable/disable some tests based on environment variables in the .env file. 12 if (!config.testPathIgnorePatterns) { 13 config.testPathIgnorePatterns = []; 14 } 15 if (!process.env.RUN_COMPONENT_TESTS) { 16 config.testPathIgnorePatterns.push('<rootDir>/src/components/**/*.test.js'); 17 } 18 if (!process.env.RUN_REDUCER_TESTS) { 19 config.testPathIgnorePatterns.push('<rootDir>/src/reducers/**/*.test.js'); 20 } 21 return config; 22 }, 23 // The function to use to create a webpack dev server configuration when running the development 24 // server with 'npm run start' or 'yarn start'. 25 // Example: set the dev server to use a specific certificate in https. 26 devServer: function(configFunction) { 27 // Return the replacement function for create-react-app to use to generate the Webpack 28 // Development Server config. "configFunction" is the function that would normally have 29 // been used to generate the Webpack Development server config - you can use it to create 30 // a starting configuration to then modify instead of having to create a config from scratch. 31 return function(proxy, allowedHost) { 32 // Create the default config by calling configFunction with the proxy/allowedHost parameters 33 const config = configFunction(proxy, allowedHost); 34 35 // Change the https certificate options to match your certificate, using the .env file to 36 // set the file paths & passphrase. 37 const fs = require('fs'); 38 config.https = { 39 key: fs.readFileSync(process.env.REACT_HTTPS_KEY, 'utf8'), 40 cert: fs.readFileSync(process.env.REACT_HTTPS_CERT, 'utf8'), 41 ca: fs.readFileSync(process.env.REACT_HTTPS_CA, 'utf8'), 42 passphrase: process.env.REACT_HTTPS_PASS 43 }; 44 45 // Return your customised Webpack Development Server config. 46 return config; 47 }; 48 }, 49 // The paths config to use when compiling your react app for development or production. 50 paths: function(paths, env) { 51 // ...add your paths config 52 return paths; 53 }, 54}
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:
1const { paths } = require('react-app-rewired'); 2// require normalized overrides 3const overrides = require('react-app-rewired/config-overrides'); 4const config = require(paths.scriptVersion + '/config/webpack.config.dev'); 5 6module.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:
1require('./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:
1{ 2 "scripts": { 3 "start": "react-app-rewired start --scripts-version react-scripts-ts", 4 "build": "react-app-rewired build --scripts-version react-scripts-ts", 5 "test": "react-app-rewired test --scripts-version react-scripts-ts", 6 "eject": "react-scripts eject" 7 } 8}
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
When developing this project, ensure you have yarn installed.
To run the test app, navigate to the directory and run:
1yarn setup 2yarn 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 dependenciesNo vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 19/28 approved changesets -- score normalized to 6
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
118 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More