Rewire Your App
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.
All you have to do is create your app using create-react-app and then rewire it.
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.
How to rewire your create-react-app project
1) Install react-app-rewired
$ npm install react-app-rewired --save-dev
config-overrides.js
file in the root directory
2) Create a /* 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
Note: You can use one of the default rewires (see the packages dir) or injectBabelPlugin
react-scripts
in npm
scripts
3) 'Flip' the existing calls to /* 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"
}
4) Start the Dev Server
$ npm start
5) Build your app
$ npm run build
Utilities
1) injectBabelPlugin
Adding a Babel plugin can be done via the injectBabelPlugin(pluginName, config)
function. You can also use the "rewire" packages from this repo or listed below to do common config modifications.
const rewireMobX = require('react-app-rewire-mobx');
const rewirePreact = require('react-app-rewire-preact');
const {injectBabelPlugin} = require('react-app-rewired');
/* config-overrides.js */
module.exports = function override(config, env) {
// add a plugin
config = injectBabelPlugin('emotion/babel',config)
// use the Preact rewire
if (env === "production") {
console.log("⚡ Production build with Preact");
config = rewirePreact(config, env);
}
// use the MobX rewire
config = rewireMobX(config,env);
return config;
}
2) compose(after v1.3.4)
You can use this util to compose rewires.
A functional programming utility, performs
right-to-left
function composition.
More detail you can see ramda or redux
Before:
/* config-overrides.js */
module.exports = function override(config, env) {
config = rewireLess(config, env);
config = rewirePreact(config, env);
config = rewireMobX(config, env);
return config;
}
After use compose
:
/* config-overrides.js */
const { compose } = require('react-app-rewired');
module.exports = compose(
rewireLess,
rewirePreact,
rewireMobx
...
)
// custom config
module.exports = function(config, env){
const rewires = compose(
rewireLess,
rewirePreact,
rewireMobx
...
);
// do custom config
// ...
return rewires(config, env);
}
Some change with rewire, if you want to add some extra param
for rewire
-
Optional params:
you can see react-app-rewire-less -
Required params:
// rewireSome.js
function createRewire(requiredParams){
return function rewire(config, env){
///
return config
}
}
module.exports = createRewire;
Community Maintained Rewires
Babel plugins
- react-app-rewire-emotion by @osdevisnot
- react-app-rewire-lodash by @osdevisnot
- react-app-rewire-styled-components by @mxstbr
- react-app-rewire-polished by @rawrmonstar
- react-app-rewire-idx by @viktorivarsson
- react-app-rewire-glamorous-displayname by @CarlRosell
Webpack plugins
- react-app-rewire-appcache-plugin by @icopp
- react-app-rewire-define-plugin by @icopp
- react-app-rewire-imagemin-plugin by @icopp
- react-app-rewire-preload-plugin by @icopp
- react-app-rewire-provide-plugin by @icopp
Loaders
- react-app-rewire-nearley by @icopp
- react-app-rewire-typescript by @icopp
- react-app-rewire-css-modules by @lnhrdt
- react-app-rewire-svg-react-loader by @lnhrdt