Skip to content

Commit 2ffd187

Browse files
xD3VHAXcalvinl
xD3VHAX
authored andcommitted
Enhance prod minification (#38)
* Enhance prod minification Added postcss-csso for css/scss minification Added DEV_SERVER_HOST_URL directly in .env (dev server under subdomain support) * Update prod webpack * Fix dist public path
1 parent 2a332f5 commit 2ffd187

10 files changed

+101
-41
lines changed

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ APPLICATION_BASE_URL=http://localhost:3000
1111
ASSET_HOST=/dist
1212

1313
# The output path for webpack builds.
14-
WEBPACK_OUTPUT_PATH=/dist
14+
WEBPACK_OUTPUT_PATH=/dist/public
1515

1616
# Settings for webpack-dev-server.
1717
DEV_SERVER_PORT=3001
1818
DEV_SERVER_HOSTNAME=localhost
19+
DEV_SERVER_HOST_URL=http://localhost:3001
20+

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ For production builds:
3030
npm run prod:build
3131
npm run serve
3232
```
33+
Or simply
34+
35+
```
36+
npm run prod
37+
```
3338

3439
For Heroku, simply add a `Procfile`:
3540
```

package-lock.json

Lines changed: 59 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"scripts": {
1616
"start": "better-npm-run dev:build && better-npm-run dev:start",
17+
"prod": "better-npm-run prod:build && better-npm-run serve",
1718
"serve": "better-npm-run serve",
1819
"dev:start": "better-npm-run dev:start",
1920
"dev:start:server": "better-npm-run dev:start:server",
@@ -70,7 +71,7 @@
7071
}
7172
},
7273
"prod:build:server": {
73-
"command": "$(npm bin)/webpack --config webpack/ssr.babel.js --progress && $(npm bin)/babel ./server -d ./dist",
74+
"command": "$(npm bin)/webpack --config webpack/ssr.babel.js --progress && $(npm bin)/babel ./server -d ./dist && $(rm ./server/renderer/handleRender.built.js)",
7475
"env": {
7576
"NODE_ENV": "production"
7677
}
@@ -116,24 +117,24 @@
116117
"babel-eslint": "^8.0.3",
117118
"better-npm-run": "^0.1.0",
118119
"debug": "^3.1.0",
119-
"eslint": "^4.12.1",
120+
"eslint": "^4.13.0",
120121
"eslint-loader": "^1.9.0",
121122
"eslint-plugin-react": "^7.5.1",
122123
"nodemon": "^1.12.5",
123124
"react-transform-catch-errors": "^1.0.2",
124125
"redbox-react": "^1.5.0",
125126
"supertest": "^3.0.0",
126-
"webpack-dev-server": "^2.9.6",
127+
"webpack-dev-server": "^2.9.7",
127128
"webpack-sources": "^1.1.0"
128129
},
129130
"dependencies": {
130131
"app-module-path": "^2.2.0",
131-
"autoprefixer": "^7.2.1",
132+
"autoprefixer": "^7.2.2",
132133
"axios": "^0.17.1",
133134
"babel": "^6.23.0",
134135
"babel-cli": "^6.26.0",
135136
"babel-core": "^6.26.0",
136-
"babel-loader": "^7.1.1",
137+
"babel-loader": "^7.1.2",
137138
"babel-plugin-react-transform": "^3.0.0",
138139
"babel-plugin-resolver": "^1.1.0",
139140
"babel-plugin-syntax-class-properties": "^6.13.0",
@@ -171,6 +172,7 @@
171172
"lodash": "^4.17.4",
172173
"mocha": "^4.0.1",
173174
"node-sass": "^4.7.2",
175+
"postcss-csso": "^3.0.0",
174176
"postcss-loader": "^2.0.9",
175177
"react": "^16.2.0",
176178
"react-dom": "^16.2.0",
@@ -183,11 +185,11 @@
183185
"redux": "^3.7.2",
184186
"redux-logger": "^3.0.6",
185187
"redux-thunk": "^2.2.0",
186-
"resolve-url-loader": "^2.2.0",
188+
"resolve-url-loader": "^2.2.1",
187189
"sass-loader": "^6.0.6",
188190
"sass-resources-loader": "^1.3.1",
189191
"semantic-ui-css": "^2.2.12",
190-
"semantic-ui-react": "^0.77.0",
192+
"semantic-ui-react": "^0.77.1",
191193
"serve-static": "^1.13.1",
192194
"style-loader": "^0.19.0",
193195
"url-loader": "^0.6.2",

postcss.config.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const autoprefixer = require('autoprefixer');
2+
const csso = require('postcss-csso')({ restructure: true, comments: false });
23

4+
const pluginsList = [autoprefixer];
5+
if (process.env.NODE_ENV === 'production') {
6+
pluginsList.push(csso);
7+
}
38
module.exports = {
4-
plugins: [autoprefixer]
5-
};
9+
plugins: pluginsList
10+
};

server/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ app.use(compression());
3333

3434
// Add middleware to serve up all static files
3535
app.use('/dist',
36-
express.static(path.join(__dirname, '../dist')),
36+
express.static(path.join(__dirname, '../' + process.env.WEBPACK_OUTPUT_PATH)),
3737
express.static(path.join(__dirname, '../common/images')),
3838
express.static(path.join(__dirname, '../common/fonts'))
3939
);

webpack/base.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ export default {
121121
test: /\.css$/,
122122
use: extractText.extract({
123123
fallback: 'style-loader',
124-
use: [
125-
{ loader: 'css-loader' }
126-
]
124+
use: ['css-loader', 'postcss-loader'],
127125
})
128126
},
129127
{

webpack/constants.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { mapValues, keyBy } from 'lodash';
66
*/
77
export const {
88
NODE_ENV, DEV_SERVER_PORT, DEV_SERVER_HOSTNAME, WEBPACK_OUTPUT_PATH,
9-
ASSET_HOST, ASSET_PATH, ANALYZE, APPLICATION_BASE_URL
9+
ASSET_HOST, ASSET_PATH, ANALYZE, APPLICATION_BASE_URL, DEV_SERVER_HOST_URL
1010
} = process.env;
1111

1212
// The env vars to expose on the client side.
@@ -21,7 +21,6 @@ export const CLIENT_ENV_VARS = mapValues(
2121
);
2222

2323
// The URL of the dev server including the hostname and port
24-
export const DEV_SERVER_HOST_URL = `http://${DEV_SERVER_HOSTNAME}:${DEV_SERVER_PORT}`;
2524

2625
// The asset host to use for webpack files. In development, we will always use
2726
// the dev server's URL.

webpack/development.hot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ new WebpackDevServer(webpack(config), {
6363
version: false,
6464
chunks: false,
6565
children: false
66-
}
66+
},
67+
disableHostCheck: true
6768
}).listen(DEV_SERVER_PORT, DEV_SERVER_HOSTNAME, function errorCallback(err) {
6869
if (err) {
6970
console.error(err);

webpack/production.babel.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import webpack from 'webpack';
22
import baseConfig from './base';
33
import CompressionPlugin from 'compression-webpack-plugin';
4-
4+
import UglifyJSPlugin from 'uglifyjs-webpack-plugin';
55
const plugins = [
6+
new webpack.BannerPlugin({
7+
banner: 'hash:[hash], chunkhash:[chunkhash], name:[name], filebase:[filebase], query:[query], file:[file]',
8+
}),
69
new webpack.optimize.CommonsChunkPlugin({
710
name: 'vendor',
811
filename: '[name].[hash].js',
912
minChunks: module => /node_modules/.test(module.resource)
1013
}),
1114
new webpack.optimize.AggressiveMergingPlugin(),
1215
new webpack.optimize.ModuleConcatenationPlugin(),
13-
new webpack.optimize.UglifyJsPlugin({
14-
compress: {
15-
screw_ie8: true,
16-
warnings: false
16+
new UglifyJSPlugin({
17+
uglifyOptions: {
18+
compress: {
19+
warnings: false
20+
},
21+
mangle: true,
22+
output: {
23+
comments: false,
24+
},
1725
}
1826
}),
1927
new CompressionPlugin({

0 commit comments

Comments
 (0)