Skip to content

Commit ab279d7

Browse files
authored
Merge pull request #7 from AlokTakshak/development
prod server for deployment
2 parents 8ccf7d2 + 8cc48a1 commit ab279d7

File tree

14 files changed

+167
-33
lines changed

14 files changed

+167
-33
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,39 @@ demo-app
4747
└── webpack.config.prod.js
4848
```
4949

50+
```
51+
create-react-webpack demo-app -e
52+
```
53+
54+
It incules the node server for deploying into production into your `demo-app`, with below file footprint.
55+
56+
```
57+
demo-app
58+
├── README.md
59+
├── node_modules
60+
├── package.json
61+
├── .gitignore
62+
├── .babelrc
63+
├── .eslintrc.json
64+
├── .eslintignore
65+
├── .prettierrc
66+
├── .prettierignore
67+
├── public
68+
│   ├── favicon.ico
69+
│   ├── index.html
70+
│   └── manifest.json
71+
├── server
72+
│   └── index.js
73+
├── src
74+
│   ├── App.css
75+
│   ├── App.js
76+
│   ├── App.spec.js
77+
│   └── index.js
78+
├── webpack.config.base.js
79+
├── webpack.config.dev.js
80+
└── webpack.config.prod.js
81+
```
82+
5083
## Available Scripts
5184

5285
After creating project directory you can run following scripts:-
@@ -56,6 +89,11 @@ After creating project directory you can run following scripts:-
5689
builds the application for production to the `dist` folder inside directory.<br>
5790
Uses webpack `prod` `config` along with `base` `config`
5891

92+
### `npm start`
93+
94+
Start the production server on default port `3000`.<br>
95+
Read files from `dist` folder.
96+
5997
### `npm run dev`
6098

6199
Start the development server on default port `8080`.<br>

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-react-webpack",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "create-react-webpack ",
55
"main": "scripts/create.js",
66
"bin": {
@@ -14,6 +14,10 @@
1414
"type": "git",
1515
"url": "git+https://github.com/AlokTakshak/create-react-webpack.git"
1616
},
17+
"keywords": [
18+
"React",
19+
"Webpack"
20+
],
1721
"author": "Alok Takshak",
1822
"license": "ISC",
1923
"bugs": {

scripts/constants.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
const CLEAN_NPM_CACHE = "npm cache clean --force";
22
const SPECIALCHAR = RegExp("/[!@#$%^&*()-=_,.?~:;\\{}|<>]/g");
3-
const UNNECESSORYFOLDERS = RegExp("^node_modules|build|dist$", "i");
3+
const UNNECESSORY_FOLDERS_FOR_DEV = RegExp(
4+
"^node_modules|build|dist|server$",
5+
"i"
6+
);
7+
const UNNECESSORY_FOLDERS_FOR_PROD = RegExp("^node_modules|build|dist$", "i");
48

59
module.exports = {
610
CLEAN_NPM_CACHE,
711
SPECIALCHAR,
8-
UNNECESSORYFOLDERS
12+
UNNECESSORY_FOLDERS_FOR_DEV,
13+
UNNECESSORY_FOLDERS_FOR_PROD
914
};

scripts/create.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const { CLEAN_NPM_CACHE, SPECIALCHAR } = require("./constants");
44
const {
55
installDependencies,
66
getDependencies,
7-
getDevDependencies
7+
getDevDependencies,
8+
getProdDependencies
89
} = require("./dependencies");
910

1011
var args = process.argv.slice(2);
1112
var dirName = args[0];
13+
var end_to_end = args[1];
1214

1315
if (dirName[0].match("^[A-Z0-9]")) {
1416
throw new Error(
@@ -24,14 +26,18 @@ if (dirName[0].match("^[A-Z0-9]")) {
2426
//using process.cwd for getting current path
2527
const TEMPLATE_PATH = path.join(__dirname, "../template");
2628
let destination = path.join(process.cwd() + "/" + args[0]);
27-
copyDirectory(TEMPLATE_PATH, destination);
29+
const prod = end_to_end == "-e";
30+
31+
copyDirectory(TEMPLATE_PATH, destination, prod);
2832

2933
let commands = [],
3034
options;
3135

3236
commands.push(CLEAN_NPM_CACHE);
3337
commands.push(getDependencies());
38+
prod && commands.push(getProdDependencies());
3439
commands.push(getDevDependencies());
40+
3541
options = { cwd: destination, stdio: "inherit" };
3642

3743
installDependencies(commands, options);

scripts/dependencies.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const DEV_DEPENDENCIES = [
2525
"webpack-manifest-plugin",
2626
"webpack-merge"
2727
];
28+
const PROD_DEPENDENCIES = ["express", "express-static-gzip"];
2829

2930
/**
3031
* Returns npm command for installing dependencies
@@ -48,6 +49,17 @@ function getDevDependencies() {
4849
return installCommand;
4950
}
5051

52+
/**
53+
* Returns npm command for installing prod dependencies
54+
*/
55+
function getProdDependencies() {
56+
var installCommand = "npm install --save";
57+
PROD_DEPENDENCIES.forEach(dependency => {
58+
installCommand += ` ${dependency} `;
59+
});
60+
return installCommand;
61+
}
62+
5163
/**
5264
*
5365
* @param {Array<String>} commands list of commands needs to be executed
@@ -69,4 +81,9 @@ function installDependencies(commands, options) {
6981
}
7082
}
7183

72-
module.exports = { installDependencies, getDependencies, getDevDependencies };
84+
module.exports = {
85+
installDependencies,
86+
getDependencies,
87+
getDevDependencies,
88+
getProdDependencies
89+
};

scripts/helper.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
const fs = require("fs");
22
const path = require("path");
33
const chalk = require("chalk");
4-
const { UNNECESSORYFOLDERS } = require("./constants");
4+
const {
5+
UNNECESSORY_FOLDERS_FOR_DEV,
6+
UNNECESSORY_FOLDERS_FOR_PROD
7+
} = require("./constants");
58

69
/**
710
* @summary copies the directory content from source to destination directory
811
* @param {String} source path of source file
912
* @param {String} destination path of destination file
13+
* @param {boolean} prod tells if user require prod environment
1014
*/
11-
function copyDirectory(source, destination) {
15+
function copyDirectory(source, destination, prod) {
16+
let UNNECESSORY_FOLDERS = prod
17+
? UNNECESSORY_FOLDERS_FOR_PROD
18+
: UNNECESSORY_FOLDERS_FOR_DEV;
19+
1220
createDirectory(destination);
1321

1422
var content = fs.readdirSync(source);
1523
for (let i = 0; i < content.length; i++) {
1624
let currentFile = fs.lstatSync(path.join(source, content[i]));
1725

18-
if (String(content[i]).match(UNNECESSORYFOLDERS)) {
26+
if (String(content[i]).match(UNNECESSORY_FOLDERS)) {
1927
continue;
2028
} else if (currentFile.isDirectory()) {
2129
copyDirectory(
2230
path.join(source, content[i]),
23-
path.join(destination, content[i])
31+
path.join(destination, content[i]),
32+
prod
2433
);
2534
} else if (currentFile.isSymbolicLink()) {
2635
var symlink = fs.readlinkSync(source, content[i]);
2736
fs.symlinkSync(symlink, path.join(destination, content[i]));
2837
} else {
2938
copyFile(
3039
path.join(source, content[i]),
31-
path.join(destination, content[i])
40+
path.join(destination, content[i]),
41+
prod
3242
);
3343
}
3444
}
@@ -38,11 +48,15 @@ function copyDirectory(source, destination) {
3848
* @summary copies the file content from source to destination file
3949
* @param {String} source path of source file
4050
* @param {String} destination path of destination file
51+
* @param {boolean} prod tells if user require prod environment
4152
*/
42-
function copyFile(source, destination) {
53+
function copyFile(source, destination, prod) {
4354
var inputFile, outputFile;
4455
if (source.match(".json$")) {
4556
inputFile = JSON.parse(fs.readFileSync(source, "utf8"));
57+
if (prod && source.match("package.json$")) {
58+
inputFile.scripts.start = "node server/";
59+
}
4660
fs.writeFileSync(destination, JSON.stringify(inputFile, null, 2), "utf8");
4761
} else {
4862
inputFile = fs.createReadStream(source);

startApp.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const child_process = require("child_process");
55

66
var args = process.argv.slice(2);
77
var dirName = args[0];
8+
var end_to_end = args[1];
89

910
var p=path.join(__dirname,"scripts/create.js")
10-
child_process.execSync(`node ${p} ${dirName}`,{ stdio: "inherit" });
11+
child_process.execSync(`node ${p} ${dirName} ${end_to_end}`,{ stdio: "inherit" });

template/public/favicon.ico

0 Bytes
Binary file not shown.

template/public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<title>React App</title>
1010
</head>
1111
<body>
12-
<div id="app"></div>
12+
<div id="root"></div>
1313
</body>
1414
</html>

template/server/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const express = require("express");
2+
const expressStaticGzip = require("express-static-gzip");
3+
const path = require("path");
4+
5+
const app = express();
6+
const PORT = process.env.PORT || 3000;
7+
8+
app.use(
9+
"/",
10+
expressStaticGzip(path.join(process.cwd(), "dist"), {
11+
enableBrotli: true,
12+
orderPreference: ["br", "gz"]
13+
})
14+
);
15+
app.use(express.static(path.join(process.cwd(), "dist")));
16+
app.use("/", express.static(path.join(process.cwd(), "public")));
17+
18+
app.get("/", (req, res) => {
19+
res.header = res.sendFile(path.join(process.cwd(), "dist", "index.html"));
20+
});
21+
22+
app.listen(PORT, () => {
23+
console.log(`Started listening on port: ${PORT}`);
24+
});

0 commit comments

Comments
 (0)