Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit 66601d4

Browse files
author
TsaiKoga
committed
Finish lesson 11:添加if-env compression
1 parent e7b3582 commit 66601d4

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lessons/11-productionish-server/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"start": "webpack-dev-server --inline --content-base . --history-api-fallback"
7+
"start": "if-env NODE_ENV=production && npm start:prod || npm run start:dev",
8+
"start:dev": "webpack-dev-server --inline --content-base . --history-api-fallback",
9+
"start:prod": "webpack && node server.js"
810
},
911
"author": "",
1012
"license": "ISC",
1113
"dependencies": {
14+
"compression": "^1.6.2",
15+
"express": "^4.14.0",
16+
"if-env": "^1.0.0",
1217
"react": "^0.14.7",
1318
"react-dom": "^0.14.7",
1419
"react-router": "^2.0.0"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var express = require("express");
2+
var path = require("path");
3+
var compression = require('compression');
4+
5+
var app = express();
6+
// serve our static stuff like index.css
7+
app.use(express.static(__dirname));
8+
app.use(compression());
9+
10+
// send all requests to index.html so browserHistory in React Router works
11+
app.get("*", function(req, res) {
12+
res.sendFile(path.join(__dirname, "index.html"))
13+
});
14+
15+
var PORT = process.env.PORT || 8080;
16+
app.listen(PORT, function() {
17+
console.log('Production Express server running at localhost:' + PORT);
18+
});
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
var webpack = require('webpack');
2+
13
module.exports = {
24
entry: './index.js',
35

46
output: {
7+
path: 'public',
58
filename: 'bundle.js',
69
publicPath: '/'
710
},
@@ -10,5 +13,12 @@ module.exports = {
1013
loaders: [
1114
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react' }
1215
]
13-
}
16+
},
17+
// add this handful of plugins that optimize the build
18+
// when we're in production
19+
plugins: process.env.NODE_ENV === 'production' ? [
20+
new webpack.optimize.DedupePlugin(),
21+
new webpack.optimize.OccurrenceOrderPlugin(),
22+
new webpack.optimize.UglifyJsPlugin()
23+
] : [],
1424
}

0 commit comments

Comments
 (0)