Skip to content

Commit fc49f44

Browse files
committed
initial commit
1 parent 5e8c2dc commit fc49f44

File tree

8 files changed

+179
-0
lines changed

8 files changed

+179
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Lock files
2+
package-lock.json
3+
14
# Logs
25
logs
36
*.log

bin/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env;
2+
const spawn = require('react-dev-utils/crossSpawn');
3+
const chalk = require('react-dev-utils/chalk');
4+
const args = process.argv.slice(2);
5+
6+
const scriptIndex = args.findIndex(
7+
(x) => x === 'build' || x === 'start' || x === 'test' || x === 'eject'
8+
);
9+
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
10+
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
11+
12+
switch (script) {
13+
case 'build':
14+
case 'start': {
15+
const result = spawn.sync(
16+
'node',
17+
nodeArgs
18+
.concat(require.resolve(`../scripts/${script}`))
19+
.concat(args.slice(scriptIndex + 1)),
20+
{ stdio: 'inherit' }
21+
);
22+
23+
if (result.signal) {
24+
if (result.signal === 'SIGKILL') {
25+
console.log('The build failed because the process exited too early.');
26+
console.log(
27+
'This probably means the system ran out of memory or someone called kill -9` on the process.'
28+
);
29+
}
30+
if (result.signal === 'SIGTERM') {
31+
console.log('The build failed because the process exited too early.');
32+
console.log(
33+
'Someone might have called `kill` or `killall`, or the system could be shutting down.'
34+
);
35+
}
36+
process.exit(1);
37+
}
38+
39+
process.exit(result.status);
40+
break;
41+
}
42+
case 'test': {
43+
console.log('This is not supported by us, you should just use:');
44+
console.log('react-scripts test, in your npm package.json');
45+
break;
46+
}
47+
case 'eject': {
48+
console.log('This is not supported by us, you should just use:');
49+
console.log('react-scripts eject, in your npm package.json');
50+
break;
51+
}
52+
default: {
53+
console.log(`Unknown script ${chalk.green(script)}.`);
54+
console.log('Kindly ensure you have the latest react-scripts installed');
55+
console.log(`${chalk.green('Go to this link:')}`);
56+
console.log('https://create-react-app.dev/docs/updating-to-new-releases');
57+
}
58+
}

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "react-scripts-extend",
3+
"version": "1.0.0",
4+
"description": "utility library to extend react-scripts configuration",
5+
"main": "bin/index.js",
6+
"bin": {
7+
"react-scripts-extend": "index.js"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/withvoid/react-scripts-extend.git"
15+
},
16+
"keywords": [
17+
"react-scripts",
18+
"react",
19+
"reactjs"
20+
],
21+
"author": "Adeel Imran",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/withvoid/react-scripts-extend/issues"
25+
},
26+
"homepage": "https://github.com/withvoid/react-scripts-extend#readme",
27+
"dependencies": {
28+
"react-dev-utils": "^10.2.1"
29+
}
30+
}

scripts/build.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { nodeEnv, scriptName, configFilePath, paths } = require('./paths.utils');
2+
const { getFile, defaultOverridesConfig } = require('./helpers.utils');
3+
const overrides = getFile(configFilePath) || defaultOverridesConfig;
4+
5+
// react-scripts files
6+
const pathsPath = `${scriptName}/config/paths`;
7+
const webpackConfigPath = `${scriptName}/config/webpack.config`;
8+
const devServerConfigPath = `${scriptName}/config/webpackDevServer.config`;
9+
const webpackConfig = require(webpackConfigPath);
10+
const devServerConfig = require(devServerConfigPath);
11+
12+
// override config in memory
13+
require.cache[require.resolve(pathsPath)].exports = overrides.paths(
14+
paths,
15+
nodeEnv
16+
);
17+
require.cache[require.resolve(webpackConfigPath)].exports = overrides.webpack(
18+
webpackConfig,
19+
nodeEnv
20+
);
21+
22+
// run original script
23+
require(`${scriptName}/scripts/build`);

scripts/helpers.utils.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const getFile = (file) => {
5+
try {
6+
const configPath = path.resolve(process.cwd(), file);
7+
console.log(configPath);
8+
const result = require(configPath);
9+
return result;
10+
} catch (error) {
11+
return false;
12+
}
13+
}
14+
15+
const defaultOverridesConfig = {
16+
paths: (paths, env) => {
17+
return paths;
18+
},
19+
webpack: (config, env) => {
20+
return config;
21+
},
22+
devServer: (configFn) => (proxy, allowedHost) => {
23+
const config = configFn(proxy, allowedHost);
24+
return config;
25+
}
26+
};
27+
28+
module.exports = { getFile, defaultOverridesConfig }

scripts/paths.utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
2+
const nodeEnv = process.env.NODE_ENV || 'development';
3+
4+
module.exports = {
5+
nodeEnv,
6+
scriptName: 'react-scripts',
7+
configFilePath: './config-extends',
8+
paths: require(`react-scripts/config/paths`)
9+
}

scripts/start.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { nodeEnv, scriptName, configFilePath, paths } = require('./paths.utils');
2+
const { getFile, defaultOverridesConfig } = require('./helpers.utils');
3+
const overrides = getFile(configFilePath) || defaultOverridesConfig;
4+
5+
// react-scripts files
6+
const pathsPath = `${scriptName}/config/paths`;
7+
const webpackConfigPath = `${scriptName}/config/webpack.config`;
8+
const devServerConfigPath = `${scriptName}/config/webpackDevServer.config`;
9+
const webpackConfig = require(webpackConfigPath);
10+
const devServerConfig = require(devServerConfigPath);
11+
12+
// override config in memory
13+
require.cache[require.resolve(pathsPath)].exports = overrides.paths(
14+
paths,
15+
nodeEnv
16+
);
17+
require.cache[require.resolve(webpackConfigPath)].exports = overrides.webpack(
18+
webpackConfig,
19+
nodeEnv
20+
);
21+
require.cache[require.resolve(devServerConfigPath)].exports = overrides.devServer(
22+
devServerConfig,
23+
nodeEnv
24+
);
25+
26+
// run original script
27+
require(`${scriptName}/scripts/start`);

scripts/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('test js');

0 commit comments

Comments
 (0)