Skip to content

Commit df1f177

Browse files
committed
add scripts
1 parent 58e1f05 commit df1f177

File tree

6 files changed

+158
-3
lines changed

6 files changed

+158
-3
lines changed

README.md

+86-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,87 @@
11
# monkey-react-scripts
2-
Monkey react script runner
2+
Monkey react script runner: Customize react-scripts webpack config without ejection or fork
3+
4+
## Usage
5+
- use create-react-app and create your project, [more-detail][create-react-app]
6+
```
7+
npm install -g create-react-app
8+
9+
create-react-app my-app
10+
cd my-app/
11+
```
12+
13+
- install monkey-react-scripts
14+
15+
```
16+
npm install monkey-react-scripts --save-dev --save-exact
17+
```
18+
19+
- create `webpack.monkey.js` in root of your project. you can modify webpack config here.
20+
```js
21+
module.exports = function (webpackConfig, isDevelopment) {
22+
// mutate webpackConfig
23+
};
24+
```
25+
26+
- edit `package.json` and replace scripts
27+
```
28+
"scripts": {
29+
"start": "monkey-react-scripts start",
30+
"build": "monkey-react-scripts build",
31+
"test": "monkey-react-scripts test --env=jsdom"
32+
}
33+
```
34+
35+
## Example
36+
### Webpack Visualizer
37+
I love visualization so I wan't add [webpack-visualizer-plugin][webpack-visualizer] to my project
38+
- install plugin:
39+
```
40+
npm install webpack-visualizer-plugin --save-dev
41+
```
42+
- add plugin to config (only at build)
43+
```js
44+
// webpack.monkey.js
45+
var Visualizer = require('webpack-visualizer-plugin');
46+
47+
module.exports = function (webpackConfig, isDevelopment) {
48+
if (!isDevelopment) {
49+
webpackConfig.plugins.push(new Visualizer());
50+
}
51+
};
52+
```
53+
- build
54+
```
55+
$ npm run build
56+
// some output
57+
$ tree build
58+
build
59+
├── asset-manifest.json
60+
├── favicon.ico
61+
├── index.html
62+
├── static
63+
│   ├── css
64+
│   │   ├── main.9a0fe4f1.css
65+
│   │   └── main.9a0fe4f1.css.map
66+
│   ├── js
67+
│   │   ├── main.373f9afc.js
68+
│   │   └── main.373f9afc.js.map
69+
│   └── media
70+
│   └── logo.5d5d9eef.svg
71+
└── stats.html <-- new file
72+
```
73+
74+
## TODOs
75+
- [ ] add helpers
76+
- [ ] customize test runner (jest)
77+
- [ ] add more example
78+
- [ ] scss support
79+
- [ ] decorator support
80+
- [ ] relay support
81+
82+
## Thanks
83+
@svrcekmichal for [configurable-react-scripts][configurable-react-scripts]
84+
85+
[create-react-app]: https://github.com/facebookincubator/create-react-app#tldr
86+
[webpack-visualizer]: https://github.com/chrisbateman/webpack-visualizer
87+
[configurable-react-scripts]: https://github.com/svrcekmichal/configurable-react-scripts

bin/monkey-react-scripts.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
11
#!/usr/bin/env node
2-
console.log("monkey react script is coming");
2+
var spawn = require('cross-spawn');
3+
var script = process.argv[2];
4+
var args = process.argv.slice(3);
5+
6+
switch (script) {
7+
case 'build':
8+
case 'start':
9+
case 'test':
10+
var result = spawn.sync(
11+
'node',
12+
[require.resolve('../scripts/' + script)].concat(args),
13+
{stdio: 'inherit'}
14+
);
15+
if (result.signal) {
16+
if (result.signal == 'SIGKILL') {
17+
console.log(
18+
'The build failed because the process exited too early. ' +
19+
'This probably means the system ran out of memory or someone called ' +
20+
'`kill -9` on the process.'
21+
);
22+
} else if (result.signal == 'SIGTERM') {
23+
console.log(
24+
'The build failed because the process exited too early. ' +
25+
'Someone might have called `kill` or `killall`, or the system could ' +
26+
'be shutting down.'
27+
);
28+
}
29+
process.exit(1);
30+
}
31+
process.exit(result.status);
32+
break;
33+
default:
34+
console.log('Unknown script "' + script + '".');
35+
break;
36+
}

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
22
"name": "monkey-react-scripts",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "Monkey react script runner",
55
"main": "index.js",
66
"repository": "[email protected]:monkey-patches/monkey-react-scripts.git",
77
"author": "Seyyed Morteza Moosavi <[email protected]>",
88
"license": "MIT",
9+
"files": [
10+
"bin",
11+
"scripts"
12+
],
913
"bin": {
1014
"monkey-react-scripts": "./bin/monkey-react-scripts.js"
1115
},

scripts/build.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var chalk = require('chalk');
4+
var appPath = require('react-scripts/config/paths').appPath;
5+
6+
process.env.NODE_ENV = 'production';
7+
8+
var webpackMonkeyPath = path.resolve(appPath, 'webpack.monkey.js');
9+
var webpackConfig = require('react-scripts/config/webpack.config.prod');
10+
11+
if (fs.existsSync(webpackMonkeyPath)) {
12+
console.log(chalk.yellow('WARNING! You are using modified webpack config!'));
13+
require(webpackMonkeyPath)(webpackConfig, false);
14+
}
15+
require('react-scripts/scripts/build');

scripts/start.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var chalk = require('chalk');
4+
var appPath = require('react-scripts/config/paths').appPath;
5+
6+
process.env.NODE_ENV = 'development';
7+
8+
var webpackMonkeyPath = path.resolve(appPath, 'webpack.monkey.js');
9+
var webpackConfig = require('react-scripts/config/webpack.config.dev');
10+
11+
if (fs.existsSync(webpackMonkeyPath)) {
12+
console.log(chalk.yellow('WARNING! You are using modified webpack config!'));
13+
require(webpackMonkeyPath)(webpackConfig, true);
14+
}
15+
require('react-scripts/scripts/start');

scripts/test.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// TODO need help for customization
2+
require('react-scripts/scripts/test');

0 commit comments

Comments
 (0)