@@ -52,16 +52,14 @@ I suggest you see [scripts](scripts) and [bin](bin) folders. (less than 100 line
52
52
Note: returned value of ` require ` function is mutable. so you can mutate that before real build/start script.
53
53
54
54
## Snippets
55
- You can use [ snippets] ( snippets/cra-0.9 .x.md ) if you want.
55
+ You can use [ snippets] ( snippets/cra-1.x .x.md ) if you want.
56
56
57
57
snippets:
58
58
- ` addPlugin `
59
- - ` findLoader `
59
+ - ` findRule `
60
60
- ` addBabelPlugins `
61
61
- ` addLoader `
62
62
- ` addExclude `
63
- - ` createTextExtractor `
64
- - ` getScssLoader `
65
63
66
64
## Example
67
65
Before use examples you should know what happen inside react-scripts webpack config.
@@ -122,19 +120,14 @@ If you love decorators, you can add decorator support:
122
120
```
123
121
npm install --save-dev babel-plugin-transform-decorators-legacy
124
122
```
125
- - edit ` webpack.monkey.js ` like this (copy ` findLoader ` , ` addBabelPlugins ` from [ snippets] ( snippets/cra-0.9 .x.md ) ):
123
+ - edit ` webpack.monkey.js ` like this (copy ` findRule ` , ` addBabelPlugins ` from [ snippets] ( snippets/cra-1.x .x.md ) ):
126
124
``` js
127
- function findLoader (config , callback ) {
128
- var index = config .module .loaders .findIndex (callback);
129
- if (index === - 1 ) throw Error (' Loader not found' );
130
- return config .module .loaders [index];
125
+ function findRule (webpackConfig , callback ) {
126
+ /* snippet codes */
131
127
}
132
128
133
129
function addBabelPlugins (webpackConfig , plugins ) {
134
- var babelLoader = findLoader (webpackConfig, function (loader ) {
135
- return loader .loader === ' babel'
136
- });
137
- babelLoader .query .plugins = (babelLoader .query .plugins || []).concat (plugins);
130
+ /* snippet codes */
138
131
}
139
132
140
133
module .exports = function (webpackConfig , isDevelopment ) {
@@ -146,26 +139,11 @@ module.exports = function (webpackConfig, isDevelopment) {
146
139
related issues: [ #107 ] [ 107 ] , [ #167 ] [ 167 ] , [ #214 ] [ 214 ] , [ #309 ] [ 309 ] , [ #411 ] [ 411 ] , [ #1357 ] [ 1357 ]
147
140
148
141
### Relay support
149
- - install ` babel-relay-plugin ` :
150
- ```
151
- npm install --save-dev babel-relay-plugin
152
- ```
153
- - add ` relayPlugin.js ` file:
154
- ``` js
155
- const getBabelRelayPlugin = require (' babel-relay-plugin' );
156
- const schemaData = require (' ./graphql.schema.json' );
157
- const relayPlugin = getBabelRelayPlugin (schemaData .data );
158
- module .exports = relayPlugin;
159
- ```
160
- - edit ` webpack.monkey.js ` like this:
161
- ``` js
162
- /* copy findLoader, addBabelPlugins from decorator example */
163
- module .exports = function (webpackConfig , isDevelopment ) {
164
- addBabelPlugins (webpackConfig, [
165
- require .resolve (' ./relayPlugin.js' )
166
- ]);
167
- };
168
- ```
142
+
143
+ TODO
144
+
145
+ you can find support for relay classic in [ old readme] [ old-relay-support ] and get some ideas
146
+
169
147
related issues: [ #462 ] [ 462 ] , [ #662 ] [ 662 ] , [ #900 ] [ 900 ]
170
148
171
149
### scss support
@@ -177,10 +155,21 @@ npm install --save-dev node-sass sass-loader
177
155
178
156
- edit ` webpack.monkey.js ` like this:
179
157
``` js
180
- /* copy addExclude, findLoader, addLoader, getScssLoader, createTextExtractor from snippets */
158
+ /* copy addExclude, findRule, addRule from snippets */
181
159
module .exports = function (webpackConfig , isDevelopment ) {
160
+ const cssRule = findRule (webpackConfig, (rule ) => {
161
+ return (' ' + rule .test === ' ' + / \. css$ / )
162
+ });
163
+ const cssLoaders = isDevelopment ? cssRule .use : cssRule .loader ;
164
+ cssLoaders[cssLoaders .length - 1 ].options .sourceMap = true ; // add source option to postcss
165
+ const sassLoader = {loader: require .resolve (' sass-loader' ), options: {sourceMap: true }};
166
+ const sassLoaders = cssLoaders .concat (sassLoader);
167
+ const sassRule = {
168
+ test: / \. scss$ / ,
169
+ [isDevelopment ? ' use' : ' loader' ]: sassLoaders
170
+ };
182
171
addExclude (webpackConfig, / \. scss$ / );
183
- addLoader (webpackConfig, getScssLoader (isDevelopment));
172
+ addRule (webpackConfig, sassRule)
184
173
};
185
174
```
186
175
similar code for less or stylus.
@@ -191,30 +180,34 @@ related issues: [#78][78], [#115][115], [#351][351], [#412][412], [#1509][1509],
191
180
If you want change postcss config you can use this code.
192
181
``` js
193
182
module .exports = function (webpackConfig , isDevelopment ) {
194
- webpackConfig .postcss = function () {
195
- const postcssFunc = webpackConfig .postcss ;
183
+ const cssRule = findRule (webpackConfig, (rule ) => {
184
+ return (' ' + rule .test === ' ' + / \. css$ / )
185
+ });
186
+ const loaders = isDevelopment ? cssRule .use : cssRule .loader ;
187
+ const postcssLoader = loaders[loaders .length - 1 ];
188
+ const postcssFunc = postcssLoader .options .plugins ;
189
+ postcssLoader .options .plugins = () => {
196
190
return [
197
191
require (' postcss-inline-rtl' ), // add new postcss plugin
198
192
... postcssFunc () // keep cra postcss plugins
199
- ]
193
+ ]
200
194
};
201
195
};
202
196
```
203
197
204
198
## TODOs
205
- - [ ] <del >add helpers</del > snippets
206
- - [x] addPlugin
207
- - [x] findLoader
208
- - [x] addBabelPlugins
209
- - [x] extract text webpack plugin
210
- - [x] addExclude
211
- - [x] addLoader
199
+
212
200
- [ ] customize test runner (jest)
213
201
- [ ] add more example
214
- - [x] postcss
215
- - [x] scss support
216
- - [x] decorator support
217
- - [x] relay support
202
+ - [ ] relay support
203
+
204
+ ## compatibility
205
+
206
+ | react-scripts | monkey-react-scripts |
207
+ | :-------------:| :--------------------:|
208
+ | 0.9.x | 0.0.5 |
209
+ | 1.x.x | 0.1.0 |
210
+
218
211
219
212
## Thanks
220
213
@svrcekmichal for [ configurable-react-scripts] [ configurable-react-scripts ]
@@ -223,6 +216,7 @@ module.exports = function (webpackConfig, isDevelopment) {
223
216
[ webpack-visualizer ] : https://github.com/chrisbateman/webpack-visualizer
224
217
[ configurable-react-scripts ] : https://github.com/svrcekmichal/configurable-react-scripts
225
218
[ custom-react-scripts ] : https://github.com/kitze/custom-react-scripts
219
+ [ old-relay-support ] : https://github.com/monkey-patches/monkey-react-scripts/blob/b7380bbb873d637cdd6cf911de9f696b90b608fe/README.md#relay-support
226
220
227
221
[ 107 ] : https://github.com/facebookincubator/create-react-app/issues/107
228
222
[ 167 ] : https://github.com/facebookincubator/create-react-app/issues/167
0 commit comments