@@ -52,16 +52,14 @@ I suggest you see [scripts](scripts) and [bin](bin) folders. (less than 100 line
5252Note: returned value of ` require ` function is mutable. so you can mutate that before real build/start script.
5353
5454## 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.
5656
5757snippets:
5858- ` addPlugin `
59- - ` findLoader `
59+ - ` findRule `
6060- ` addBabelPlugins `
6161- ` addLoader `
6262- ` addExclude `
63- - ` createTextExtractor `
64- - ` getScssLoader `
6563
6664## Example
6765Before 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:
122120```
123121npm install --save-dev babel-plugin-transform-decorators-legacy
124122```
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 ) ):
126124``` 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 */
131127}
132128
133129function 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 */
138131}
139132
140133module .exports = function (webpackConfig , isDevelopment ) {
@@ -146,26 +139,11 @@ module.exports = function (webpackConfig, isDevelopment) {
146139related issues: [ #107 ] [ 107 ] , [ #167 ] [ 167 ] , [ #214 ] [ 214 ] , [ #309 ] [ 309 ] , [ #411 ] [ 411 ] , [ #1357 ] [ 1357 ]
147140
148141### 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+
169147related issues: [ #462 ] [ 462 ] , [ #662 ] [ 662 ] , [ #900 ] [ 900 ]
170148
171149### scss support
@@ -177,10 +155,21 @@ npm install --save-dev node-sass sass-loader
177155
178156- edit ` webpack.monkey.js ` like this:
179157``` js
180- /* copy addExclude, findLoader, addLoader, getScssLoader, createTextExtractor from snippets */
158+ /* copy addExclude, findRule, addRule from snippets */
181159module .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+ };
182171 addExclude (webpackConfig, / \. scss$ / );
183- addLoader (webpackConfig, getScssLoader (isDevelopment));
172+ addRule (webpackConfig, sassRule)
184173};
185174```
186175similar code for less or stylus.
@@ -191,30 +180,34 @@ related issues: [#78][78], [#115][115], [#351][351], [#412][412], [#1509][1509],
191180If you want change postcss config you can use this code.
192181``` js
193182module .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 = () => {
196190 return [
197191 require (' postcss-inline-rtl' ), // add new postcss plugin
198192 ... postcssFunc () // keep cra postcss plugins
199- ]
193+ ]
200194 };
201195};
202196```
203197
204198## 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+
212200- [ ] customize test runner (jest)
213201- [ ] 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+
218211
219212## Thanks
220213@svrcekmichal for [ configurable-react-scripts] [ configurable-react-scripts ]
@@ -223,6 +216,7 @@ module.exports = function (webpackConfig, isDevelopment) {
223216[ webpack-visualizer ] : https://github.com/chrisbateman/webpack-visualizer
224217[ configurable-react-scripts ] : https://github.com/svrcekmichal/configurable-react-scripts
225218[ 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
226220
227221[ 107 ] : https://github.com/facebookincubator/create-react-app/issues/107
228222[ 167 ] : https://github.com/facebookincubator/create-react-app/issues/167
0 commit comments