1
1
# monkey-react-scripts
2
+
2
3
Monkey react script runner: Customize react-scripts webpack config without eject or fork
3
4
4
5
Many of you want to add a small change to your webpack config created by create-react-app. but you don't want to eject. or
5
- use other scripts like [ configurable-react-scripts] [ configurable-react-scripts ] or
6
+ use other scripts like [ configurable-react-scripts] [ configurable-react-scripts ] or
6
7
[ custom-react-scripts] [ custom-react-scripts ] because of update delay.
7
-
8
+
8
9
With monkey-react-scripts you can use react-scripts configs, but monkey patched one. so you always have updated
9
10
react-scripts.
10
11
11
12
## ☢ DANGER ☢
12
13
13
- > As [ @gaearon ] ( https://github.com/gaearon ) mentioned multiple times there, it's not good idea to extend it. From my
14
- point of view, I'm giving you gun, so try not to shot yourself, because probably nobody will help you. When you modify
15
- something, be completely sure what you doing!
14
+ > As [ @gaearon ] ( https://github.com/gaearon ) mentioned multiple times there, it's not good idea to extend it. From my
15
+ > point of view, I'm giving you gun, so try not to shot yourself, because probably nobody will help you. When you modify
16
+ > something, be completely sure what you doing!
16
17
17
18
[ source] [ configurable-react-scripts ]
19
+
18
20
## Usage
21
+
19
22
- use create-react-app and create your project, [ more detail] [ create-react-app ]
23
+
20
24
```
21
25
npm install -g create-react-app
22
26
@@ -31,13 +35,15 @@ npm install monkey-react-scripts --save-dev --save-exact
31
35
```
32
36
33
37
- create ` webpack.monkey.js ` in the root of your project. you can modify webpack config here.
38
+
34
39
``` js
35
- module .exports = function (webpackConfig , isDevelopment ) {
36
- // mutate webpackConfig
40
+ module .exports = function (webpackConfig , isDevelopment ) {
41
+ // mutate webpackConfig
37
42
};
38
43
```
39
44
40
45
- edit ` package.json ` and replace scripts
46
+
41
47
```
42
48
"scripts": {
43
49
"start": "monkey-react-scripts start",
@@ -47,21 +53,25 @@ module.exports = function (webpackConfig, isDevelopment) {
47
53
```
48
54
49
55
## How it works
56
+
50
57
I suggest you see [ scripts] ( scripts ) and [ bin] ( bin ) folders. (less than 100 line of code)
51
58
52
59
Note: returned value of ` require ` function is mutable. so you can mutate that before real build/start script.
53
60
54
61
## Snippets
62
+
55
63
You can use [ snippets] ( snippets/cra-2.x.x.md ) if you want.
56
64
57
65
snippets:
66
+
58
67
- ` addPlugin `
59
68
- ` findRule `
60
69
- ` addBabelPlugins `
61
70
62
71
## Example
72
+
63
73
Before use examples, you should know what happens inside react-scripts webpack config.
64
- first, see and read this files:
74
+ first, see and read this files:
65
75
66
76
- ` node_modules/react-scripts/config/webpack.config.dev.js `
67
77
- ` node_modules/react-scripts/config/webpack.config.prod.js `
@@ -70,31 +80,38 @@ also, you can log `webpackConfig` value.
70
80
71
81
``` js
72
82
// webpack.monkey.js
73
- module .exports = function (webpackConfig , isDevelopment ) {
74
- console .log (webpackConfig)
83
+ module .exports = function (webpackConfig , isDevelopment ) {
84
+ console .log (webpackConfig);
75
85
};
76
86
```
77
87
78
88
Also, you can find complete examples at [ monkey-react-scripts-example] repo
79
89
80
90
### Webpack Visualizer
91
+
81
92
I love visualization so, I add [ webpack-visualizer-plugin] [ webpack-visualizer ] to my project
93
+
82
94
- install plugin:
95
+
83
96
```
84
97
npm install webpack-visualizer-plugin --save-dev
85
98
```
99
+
86
100
- add the plugin to config (only at build time)
101
+
87
102
``` js
88
103
// webpack.monkey.js
89
- var Visualizer = require (' webpack-visualizer-plugin' );
104
+ var Visualizer = require (" webpack-visualizer-plugin" );
90
105
91
- module .exports = function (webpackConfig , isDevelopment ) {
92
- if (! isDevelopment) {
93
- webpackConfig .plugins .push (new Visualizer ());
94
- }
106
+ module .exports = function (webpackConfig , isDevelopment ) {
107
+ if (! isDevelopment) {
108
+ webpackConfig .plugins .push (new Visualizer ());
109
+ }
95
110
};
96
111
```
112
+
97
113
- build
114
+
98
115
```
99
116
$ npm run build
100
117
// some output
@@ -109,58 +126,74 @@ build
109
126
│ └── media
110
127
└── stats.html <-- new file
111
128
```
129
+
112
130
### Decorator support
131
+
113
132
If you love decorators, you can add decorator support:
133
+
114
134
- install decorator plugin
135
+
115
136
```
116
137
npm install --save-dev @babel/plugin-proposal-decorators
117
138
```
139
+
118
140
- edit ` webpack.monkey.js ` like this (copy ` findRule ` , ` addBabelPlugins ` from [ snippets] ( snippets/cra-2.x.x.md ) ):
141
+
119
142
``` js
120
143
function findRule (webpackConfig , callback ) {
121
- /* snippet codes */
144
+ /* snippet codes */
122
145
}
123
146
124
147
function addBabelPlugins (webpackConfig , plugins ) {
125
- /* snippet codes */
148
+ /* snippet codes */
126
149
}
127
150
128
- module .exports = function (webpackConfig , isDevelopment ) {
129
- addBabelPlugins (webpackConfig, [[" @babel/plugin-proposal-decorators" , {legacy: true }]]);
151
+ module .exports = function (webpackConfig , isDevelopment ) {
152
+ addBabelPlugins (webpackConfig, [
153
+ [" @babel/plugin-proposal-decorators" , { legacy: true }]
154
+ ]);
130
155
};
131
156
```
157
+
132
158
related issues: [ #107 ] [ 107 ] , [ #167 ] [ 167 ] , [ #214 ] [ 214 ] , [ #309 ] [ 309 ] , [ #411 ] [ 411 ] , [ #1357 ] [ 1357 ]
133
159
134
160
### Relay support
161
+
135
162
- install ` babel-plugin-relay `
163
+
136
164
```
137
165
yarn add --dev babel-plugin-relay
138
166
```
139
167
140
168
- edit ` webpack.monkey.js `
141
- ``` js
142
169
143
- module .exports = function (webpackConfig , isDevelopment ) {
144
- addBabelPlugins (webpackConfig, [' relay' ]);
170
+ ``` js
171
+ module .exports = function (webpackConfig , isDevelopment ) {
172
+ addBabelPlugins (webpackConfig, [" relay" ]);
145
173
};
146
174
```
175
+
147
176
and continue [ relay documentation] [ relay-setup ] steps.
148
177
149
178
if you are using relay classic you can see [ old readme] [ old-relay-support ] and get some ideas.
150
179
151
180
related issues: [ #462 ] [ 462 ] , [ #662 ] [ 662 ] , [ #900 ] [ 900 ]
152
-
181
+
153
182
## postcss config
154
- If you want to change postcss config you can use this code.
183
+
184
+ If you want to change postcss config you can use this code.
185
+
155
186
``` js
156
- // add rtl css support
157
- // find postCssLoader
158
- const postCssFunction = postCssLoader .options .plugins
159
- postCssLoader .options .plugins = () => {
160
- return [... postCssFunction (), require (' postcss-inline-rtl' )]
161
- }
187
+ // add rtl css support
188
+ // find postCssLoader
189
+ const postCssFunction = postCssLoader .options .plugins ;
190
+ postCssLoader .options .plugins = () => {
191
+ return [... postCssFunction (), require (" postcss-inline-rtl" )];
192
+ };
162
193
```
194
+
163
195
you can find more detail in [ this file] [ css-patch ]
196
+
164
197
## TODOs
165
198
166
199
- [ ] customize test runner (jest)
@@ -176,9 +209,8 @@ you can find more detail in [this file][css-patch]
176
209
| 2.0.0 - 2.1.1 | 0.1.2 |
177
210
| 2.1.2 | 0.1.4 |
178
211
179
-
180
-
181
212
## Thanks
213
+
182
214
@svrcekmichal for [ configurable-react-scripts] [ configurable-react-scripts ]
183
215
184
216
[ create-react-app ] : https://github.com/facebookincubator/create-react-app#tldr
@@ -189,14 +221,12 @@ you can find more detail in [this file][css-patch]
189
221
[ monkey-react-scripts-example ] : https://github.com/monkey-patches/monkey-react-scripts-example
190
222
[ old-relay-support ] : https://github.com/monkey-patches/monkey-react-scripts/blob/b7380bbb873d637cdd6cf911de9f696b90b608fe/README.md#relay-support
191
223
[ css-patch ] : https://github.com/monkey-patches/monkey-react-scripts-example/blob/d759030325ca2d638b1ea0dd44e51655b88d5022/webpack-helpers/cssPatch.js
192
-
193
224
[ 107 ] : https://github.com/facebookincubator/create-react-app/issues/107
194
225
[ 167 ] : https://github.com/facebookincubator/create-react-app/issues/167
195
226
[ 214 ] : https://github.com/facebookincubator/create-react-app/issues/214
196
227
[ 309 ] : https://github.com/facebookincubator/create-react-app/issues/309
197
228
[ 411 ] : https://github.com/facebookincubator/create-react-app/issues/411
198
229
[ 1357 ] : https://github.com/facebookincubator/create-react-app/issues/1357
199
-
200
230
[ 462 ] : https://github.com/facebookincubator/create-react-app/issues/462
201
231
[ 662 ] : https://github.com/facebookincubator/create-react-app/pull/662
202
232
[ 900 ] : https://github.com/facebookincubator/create-react-app/issues/900
0 commit comments