Skip to content

Commit c413bea

Browse files
committed
update readme and snippets
1 parent a547edb commit c413bea

File tree

4 files changed

+93
-53
lines changed

4 files changed

+93
-53
lines changed

README.md

+43-49
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,14 @@ I suggest you see [scripts](scripts) and [bin](bin) folders. (less than 100 line
5252
Note: 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

5757
snippets:
5858
- `addPlugin`
59-
- `findLoader`
59+
- `findRule`
6060
- `addBabelPlugins`
6161
- `addLoader`
6262
- `addExclude`
63-
- `createTextExtractor`
64-
- `getScssLoader`
6563

6664
## Example
6765
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:
122120
```
123121
npm 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

133129
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 */
138131
}
139132

140133
module.exports = function (webpackConfig, isDevelopment) {
@@ -146,26 +139,11 @@ module.exports = function (webpackConfig, isDevelopment) {
146139
related 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+
169147
related 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 */
181159
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+
};
182171
addExclude(webpackConfig, /\.scss$/);
183-
addLoader(webpackConfig, getScssLoader(isDevelopment));
172+
addRule(webpackConfig, sassRule)
184173
};
185174
```
186175
similar code for less or stylus.
@@ -191,30 +180,34 @@ related issues: [#78][78], [#115][115], [#351][351], [#412][412], [#1509][1509],
191180
If you want change postcss config you can use this code.
192181
```js
193182
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 = () => {
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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "monkey-react-scripts",
3-
"version": "0.1.0",
3+
"version": "0.1.0-beta",
44
"description": "Monkey react script runner",
55
"main": "index.js",
66
"repository": "[email protected]:monkey-patches/monkey-react-scripts.git",

snippets/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
You need know what happen in monkey brain. So Snippets is better than helpers
44

5-
- [snippets for [email protected]](cra-0.9.x.md)
5+
- [snippets for [email protected]](cra-0.9.x.md)
6+
- [snippets for [email protected]](cra-1.x.x.md)

snippets/cra-1.x.x.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,50 @@ useful codes you can copy and use in webpack.monkey.js file.
33

44
In real project I copy some of them in other file and use require function:
55

6-
## TODO
7-
coming soon. you can see [snippets for [email protected]](cra-0.9.x.md) and get some idea.
6+
## webpack plugin
7+
8+
Add webpack plugin
9+
```js
10+
function addPlugin(config, plugin) {
11+
config.plugins.push(plugin);
12+
}
13+
```
14+
## Find Rule
15+
16+
```js
17+
function findRule(webpackConfig, callback) {
18+
const index = webpackConfig.module.rules.findIndex(callback);
19+
if (index === -1) throw Error('Loader not found');
20+
return webpackConfig.module.rules[index];
21+
}
22+
```
23+
24+
## Add Babel plugin
25+
requirement: `findRule`
26+
```js
27+
function addBabelPlugins(webpackConfig, plugins) {
28+
const babelRule = findRule(webpackConfig, function (rule) {
29+
return rule.loader && rule.loader.endsWith('babel-loader/lib/index.js');
30+
});
31+
babelRule.options.plugins = (babelRule.options.plugins || []).concat(plugins);
32+
}
33+
```
34+
35+
## addLoader
36+
```js
37+
function addRule(webpackConfig, rule) {
38+
webpackConfig.module.rules.push(rule);
39+
}
40+
```
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)