Skip to content

Commit 51ca169

Browse files
committed
feat(build): split bundle into app and vendor, paramterize build
1 parent 93427d0 commit 51ca169

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

webpack.config.js

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,55 @@ var webpack = require('webpack');
66
var HtmlWebpackPlugin = require('html-webpack-plugin');
77
var CleanWebpackPlugin = require('clean-webpack-plugin');
88

9-
var paramsDefault = {
9+
var PARAMS_DEFAULT = {
10+
resolve: {
11+
extensions: ['', '.js', '.tpl.html']
12+
},
13+
entry: {
14+
main: './src/main.js',
15+
vendor: ['lodash', 'jquery', 'bootstrap', 'angular', 'angular-animate', 'angular-ui-bootstrap', 'angular-highlightjs']
16+
},
1017
output: {
11-
path: './build',
12-
filename: '[name].[hash].js',
13-
sourceMapFilename: '[name].[hash].map'
18+
filename: '[name].[chunkhash].js',
19+
sourceMapFilename: '[name].[chunkhash].map'
1420
},
15-
server: {
21+
plugins: [
22+
new HtmlWebpackPlugin({
23+
template: './src/index.html',
24+
inject: 'body'
25+
}),
26+
new webpack.ProvidePlugin({
27+
$: 'jquery',
28+
jQuery: 'jquery'
29+
}),
30+
new webpack.optimize.DedupePlugin()
31+
],
32+
devServer: {
1633
port: 8081
17-
}
34+
},
35+
progress: true,
36+
colors: true
1837
};
19-
var paramsPerTarget = {
38+
var PARAMS_PER_TARGET = {
2039
DEV: {
2140
debug: true,
2241
devtool: 'inline-source-map',
2342
output: {
2443
filename: '[name].js'
25-
}
44+
},
45+
plugins: [
46+
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js')
47+
]
2648
},
2749
BUILD: {
2850
debug: true,
51+
output: {
52+
path: './build'
53+
},
2954
devtool: 'source-map',
3055
plugins: [
31-
new CleanWebpackPlugin(['build'])
56+
new CleanWebpackPlugin(['build']),
57+
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.[chunkhash].js', Infinity)
3258
]
3359
},
3460
DIST: {
@@ -38,28 +64,22 @@ var paramsPerTarget = {
3864
},
3965
plugins: [
4066
new CleanWebpackPlugin(['dist']),
67+
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.[chunkhash].js', Infinity),
4168
new webpack.optimize.UglifyJsPlugin({
4269
mangle: false
4370
})
4471
]
4572
}
4673
};
4774
var TARGET = minimist(process.argv.slice(2)).TARGET || 'BUILD';
48-
var params = _.merge(paramsDefault, paramsPerTarget[TARGET]);
49-
printBuildInfo();
75+
var params = _.merge(PARAMS_DEFAULT, PARAMS_PER_TARGET[TARGET], _mergeArraysCustomizer);
76+
77+
_printBuildInfo(params);
5078

5179
module.exports = {
52-
resolve: {
53-
extensions: ['', '.js', '.tpl.html']
54-
},
55-
entry: {
56-
main: './src/main.js'
57-
},
58-
output: {
59-
path: params.output.path,
60-
filename: params.output.filename,
61-
sourceMapFilename: params.output.sourceMapFilename
62-
},
80+
resolve: params.resolve,
81+
entry: params.entry,
82+
output: params.output,
6383
module: {
6484
loaders: [
6585
{test: /\.js$/, loader: 'babel?optional[]=runtime&stage=1', exclude: /(\.test.js$|node_modules)/},
@@ -68,31 +88,26 @@ module.exports = {
6888
{test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/, loader: 'url?limit=50000'}
6989
]
7090
},
71-
plugins: [
72-
new HtmlWebpackPlugin({
73-
template: './src/index.html',
74-
inject: 'body'
75-
}),
76-
new webpack.ProvidePlugin({
77-
$: 'jquery',
78-
jQuery: 'jquery'
79-
})
80-
].concat(params.plugins || []),
81-
devServer: {
82-
port: params.server.port
83-
},
91+
plugins: params.plugins,
92+
devServer: params.devServer,
8493
debug: params.debug,
8594
devtool: params.devtool,
86-
progress: true,
87-
colors: true
95+
progress: params.progress,
96+
colors: params.colors
8897
};
8998

90-
function printBuildInfo() {
99+
function _printBuildInfo(params) {
91100
console.log('\nStarting ' + chalk.bold.green('"' + TARGET + '"') + ' build');
92101
if (TARGET === 'DEV') {
93102
console.log('Dev server: ' +
94-
chalk.bold.yellow('http://localhost:' + params.server.port + '/webpack-dev-server/index.html') + '\n\n');
103+
chalk.bold.yellow('http://localhost:' + params.devServer.port + '/webpack-dev-server/index.html') + '\n\n');
95104
} else {
96105
console.log('\n\n');
97106
}
98107
}
108+
109+
function _mergeArraysCustomizer(a, b) {
110+
if (_.isArray(a)) {
111+
return a.concat(b);
112+
}
113+
}

0 commit comments

Comments
 (0)