Skip to content

Commit c8ecc5a

Browse files
committed
update readme and snippets
1 parent e1f5f06 commit c8ecc5a

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Note: returned value of `require` function is mutable. so you can mutate that be
6060

6161
## Snippets
6262

63-
You can use [snippets](snippets/cra-2.x.x.md) if you want.
63+
You can use [snippets](snippets/cra-4.x.x.md) if you want.
6464

6565
snippets:
6666

@@ -91,7 +91,7 @@ Also, you can find complete examples at [monkey-react-scripts-example] repo
9191

9292
The `.babelrc` file is enabled for tests if you have `webpack.monkey.js` file. also, you can enable `.babelrc` for build and start:
9393

94-
- edit `webpack.monkey.js` like this (copy `findRule`, `findBabelRule` from [snippets](snippets/cra-2.x.x.md)):
94+
- edit `webpack.monkey.js` like this (copy `findRule`, `findBabelRule` from [snippets](snippets/cra-4.x.x.md)):
9595

9696
```js
9797
function findRule(webpackConfig, callback) {

Diff for: snippets/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ You need know what happen in monkey brain. So Snippets is better than helpers
44

55
- [snippets for [email protected]](cra-0.9.x.md)
66
- [snippets for [email protected]](cra-1.x.x.md)
7+
- [snippets for [email protected]](cra-2.x.x.md)
8+
- [snippets for [email protected]](cra-3.x.x.md)
9+
- [snippets for [email protected]](cra-4.x.x.md)

Diff for: snippets/cra-3.x.x.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Code snippets for create-react-app 1.x.x
2+
useful codes you can copy and use in webpack.monkey.js file.
3+
4+
In real project I copy some of them in other file and use require function:
5+
6+
## webpack plugin
7+
8+
Add webpack plugin
9+
```js
10+
function addPlugin(webpackConfig, plugin) {
11+
webpackConfig.plugins.push(plugin);
12+
}
13+
```
14+
## Find Rule
15+
16+
```js
17+
function findRule(webpackConfig, callback) {
18+
const rules = webpackConfig.module.rules[2].oneOf;
19+
const index = rules.findIndex(callback);
20+
if (index === -1) throw Error('Loader not found');
21+
return rules[index]
22+
}
23+
```
24+
25+
## find Babel rule
26+
requirement: `findRule`
27+
```js
28+
function findBabelRule(webpackConfig, plugins) {
29+
// find babel rule
30+
const babelRule = findRule(webpackConfig, (rule) => {
31+
return ('' + rule.test === '' + /\.(js|jsx)$/)
32+
});
33+
return babelRule;
34+
}
35+
```

Diff for: snippets/cra-4.x.x.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Code snippets for create-react-app 1.x.x
2+
useful codes you can copy and use in webpack.monkey.js file.
3+
4+
In real project I copy some of them in other file and use require function:
5+
6+
## webpack plugin
7+
8+
Add webpack plugin
9+
```js
10+
function addPlugin(webpackConfig, plugin) {
11+
webpackConfig.plugins.push(plugin);
12+
}
13+
```
14+
## Find Rule
15+
16+
```js
17+
function findRule(webpackConfig, callback) {
18+
const rules = webpackConfig.module.rules[1].oneOf;
19+
const index = rules.findIndex(callback);
20+
if (index === -1) throw Error('Loader not found');
21+
return rules[index]
22+
}
23+
```
24+
25+
## find Babel rule
26+
requirement: `findRule`
27+
```js
28+
function findBabelRule(webpackConfig, plugins) {
29+
// find babel rule
30+
const babelRule = findRule(webpackConfig, (rule) => {
31+
return ('' + rule.test === '' + /\.(js|jsx)$/)
32+
});
33+
return babelRule;
34+
}
35+
```

0 commit comments

Comments
 (0)