Skip to content

Commit bf465b9

Browse files
committed
update readme
1 parent 506b9e2 commit bf465b9

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

README.md

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ snippets:
5858
- `addPlugin`
5959
- `findRule`
6060
- `addBabelPlugins`
61-
- `addLoader`
62-
- `addExclude`
61+
- `addRule`
6362

6463
## Example
6564
Before use examples you should know what happen inside react-scripts webpack config.
@@ -77,6 +76,8 @@ module.exports = function (webpackConfig, isDevelopment) {
7776
};
7877
```
7978

79+
Also you can find complete examples at [monkey-react-scripts-example] repo
80+
8081
### Webpack Visualizer
8182
I love visualization so I add [webpack-visualizer-plugin][webpack-visualizer] to my project
8283
- install plugin:
@@ -131,18 +132,27 @@ function addBabelPlugins(webpackConfig, plugins) {
131132
}
132133

133134
module.exports = function (webpackConfig, isDevelopment) {
134-
addBabelPlugins(webpackConfig, [
135-
require.resolve('babel-plugin-transform-decorators-legacy')
136-
]);
135+
addBabelPlugins(webpackConfig, ['transform-decorators-legacy']);
137136
};
138137
```
139138
related issues: [#107][107], [#167][167], [#214][214], [#309][309], [#411][411], [#1357][1357]
140139

141140
### Relay support
141+
- install `babel-plugin-relay`
142+
```
143+
yarn add --dev babel-plugin-relay
144+
```
145+
146+
- edit `webpack.monkey.js`
147+
```js
142148

143-
TODO
149+
module.exports = function (webpackConfig, isDevelopment) {
150+
addBabelPlugins(webpackConfig, ['relay']);
151+
};
152+
```
153+
and continue [relay documentation][relay-setup] steps.
144154

145-
you can find support for relay classic in [old readme][old-relay-support] and get some ideas
155+
if you are using relay classic you can see [old readme][old-relay-support] and get some ideas.
146156

147157
related issues: [#462][462], [#662][662], [#900][900]
148158

@@ -155,21 +165,27 @@ npm install --save-dev node-sass sass-loader
155165

156166
- edit `webpack.monkey.js` like this:
157167
```js
158-
/* copy addExclude, findRule, addRule from snippets */
168+
/* copy findRule, addRule from snippets */
159169
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-
};
171-
addExclude(webpackConfig, /\.scss$/);
172-
addRule(webpackConfig, sassRule)
170+
// find css rule
171+
const cssRule = findRule(webpackConfig, (rule) => {
172+
return ('' + rule.test === '' + /\.css$/)
173+
});
174+
const cssLoaders = isDevelopment ? cssRule.use : cssRule.loader;
175+
176+
const postCssLoader = cssLoaders[cssLoaders.length - 1];
177+
postCssLoader.options.sourceMap = true; // add source option to postcss
178+
179+
// add sass support
180+
const sassLoader = {loader: require.resolve('sass-loader'), options: {sourceMap: true}};
181+
const sassLoaders = cssLoaders.concat(sassLoader);
182+
183+
const sassRule = {
184+
test: /\.scss$/,
185+
[isDevelopment ? 'use' : 'loader']: sassLoaders,
186+
};
187+
188+
addRule(webpackConfig, sassRule)
173189
};
174190
```
175191
similar code for less or stylus.
@@ -179,27 +195,18 @@ related issues: [#78][78], [#115][115], [#351][351], [#412][412], [#1509][1509],
179195
## postcss config
180196
If you want change postcss config you can use this code.
181197
```js
182-
module.exports = function (webpackConfig, isDevelopment) {
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 = () => {
190-
return [
191-
require('postcss-inline-rtl'), // add new postcss plugin
192-
...postcssFunc() // keep cra postcss plugins
193-
]
194-
};
195-
};
198+
// add rtl css support
199+
const postCssFunction = postCssLoader.options.plugins
200+
postCssLoader.options.plugins = () => {
201+
return [...postCssFunction(), require('postcss-inline-rtl')]
202+
}
196203
```
197204

198205
## TODOs
199206

200207
- [ ] customize test runner (jest)
201-
- [ ] add more example
202-
- [ ] relay support
208+
- [x] add more example
209+
- [x] relay support
203210

204211
## compatibility
205212

@@ -216,6 +223,8 @@ module.exports = function (webpackConfig, isDevelopment) {
216223
[webpack-visualizer]: https://github.com/chrisbateman/webpack-visualizer
217224
[configurable-react-scripts]: https://github.com/svrcekmichal/configurable-react-scripts
218225
[custom-react-scripts]: https://github.com/kitze/custom-react-scripts
226+
[relay-setup]: https://facebook.github.io/relay/docs/en/installation-and-setup.html
227+
[monkey-react-scripts-example]: https://github.com/monkey-patches/monkey-react-scripts-example
219228
[old-relay-support]: https://github.com/monkey-patches/monkey-react-scripts/blob/b7380bbb873d637cdd6cf911de9f696b90b608fe/README.md#relay-support
220229

221230
[107]: https://github.com/facebookincubator/create-react-app/issues/107

snippets/cra-1.x.x.md

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,37 @@ In real project I copy some of them in other file and use require function:
77

88
Add webpack plugin
99
```js
10-
function addPlugin(config, plugin) {
11-
config.plugins.push(plugin);
10+
function addPlugin(webpackConfig, plugin) {
11+
webpackConfig.plugins.push(plugin);
1212
}
1313
```
1414
## Find Rule
1515

1616
```js
1717
function findRule(webpackConfig, callback) {
18-
const index = webpackConfig.module.rules.findIndex(callback);
18+
const rules = webpackConfig.module.rules[1].oneOf;
19+
const index = rules.findIndex(callback);
1920
if (index === -1) throw Error('Loader not found');
20-
return webpackConfig.module.rules[index];
21+
return rules[index]
2122
}
2223
```
2324

2425
## Add Babel plugin
2526
requirement: `findRule`
2627
```js
2728
function addBabelPlugins(webpackConfig, plugins) {
28-
const babelRule = findRule(webpackConfig, function (rule) {
29-
return rule.loader && rule.loader.endsWith('babel-loader/lib/index.js');
29+
// find babel rule
30+
const babelRule = findRule(webpackConfig, (rule) => {
31+
return ('' + rule.test === '' + /\.(js|jsx|mjs)$/)
3032
});
3133
babelRule.options.plugins = (babelRule.options.plugins || []).concat(plugins);
3234
}
3335
```
3436

35-
## addLoader
37+
## add Rule
3638
```js
37-
function addRule(webpackConfig, rule) {
38-
webpackConfig.module.rules.push(rule);
39+
function addRule (webpackConfig, rule) {
40+
const rules = webpackConfig.module.rules[1].oneOf;
41+
rules.splice(rules.length - 1, 0, rule); // add before exclude rule
3942
}
4043
```
41-
42-
## addExclude
43-
cra use file-loader for all unknown files.
44-
requirement: `findRule`
45-
```js
46-
function addExclude(webpackConfig, regex) {
47-
const excludeRule = findRule(webpackConfig, function(rule) {
48-
return rule.loader && rule.loader.endsWith('file-loader/index.js')
49-
});
50-
excludeRule.exclude.push(regex);
51-
}
52-
```

0 commit comments

Comments
 (0)