-
Notifications
You must be signed in to change notification settings - Fork 18
/
webpack.config.prod.js
193 lines (182 loc) · 5.99 KB
/
webpack.config.prod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const consts = require('./webpack/constants')
const getConfig = require('./webpack/common-config').getConfig
const commonConfig = getConfig('prod')
const extractCss = new ExtractTextPlugin('[name].css', {allChunks: true})
const htmlWebpackMinifyConfig = { // Minifying it while it is parsed
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
const config = {
devtool: 'source-map',
// devtool: 'cheap-module-source-map',
target: 'web',
cache: false,
debug: false,
entry: {
app: [
path.join(consts.SRC_DIR, 'client/index.js')
],
widgets: [
path.join(consts.SRC_DIR, 'client/widgets/index.js')
],
admin: [
path.join(consts.SRC_DIR, 'client/admin/index.js')
],
vendor: commonConfig.entry.vendor.concat([
path.join(consts.SRC_DIR, 'client/styles/bootstrap.scss'),
]),
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].bundle.js',
publicPath: '/'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
},
'__ENV' : {
'Prod': true,
'Dev': false
}
}),
//order of chunks is important, first we define common chunks between app and widgets
// new webpack.optimize.CommonsChunkPlugin({
// names: ['common'], //use this to enable extra common chunk
// // names: ['vendor'],
// filename: '[name].bundle.js',
// chunks: ['app', 'widgets'],
// // (with more entries, this ensures that no other module
// // goes into the vendor chunk)
// minChunks: 2, //set to 2 when enabling 'common' chunk
// }),
//second we extract all vendor deps to a separate chunk from our common chunk
//defines explicit vendor chunks with pre-picked vendor dependencies
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor'], //use this to enable extra common chunk
// names: ['vendor'],
filename: '[name].bundle.js',
// chunks: ['common'], //if common chunk is defined above we can extract vendor deps from it
// (with more entries, this ensures that no other module
// goes into the vendor chunk)
minChunks: 2,
}),
new webpack.optimize.DedupePlugin(),
extractCss,
new webpack.optimize.UglifyJsPlugin({
minimize: true,
compressor: {
screw_ie8: true, // eslint-disable-line camelcase
warnings: false
},
mangle: {
screw_ie8: true // eslint-disable-line camelcase
},
output: {
comments: false,
screw_ie8: true // eslint-disable-line camelcase
}
}),
new HtmlWebpackPlugin({
filename: consts.INDEX_HTML,
title: 'RCN.io',
chunks: ['vendor', 'app'],
chunksSortMode: 'dependency',
template: path.resolve(consts.SRC_DIR, 'client/index.html.ejs'), // Load a custom template
inject: false, // we use custom template to inject scripts,
hash: true,
env: {
Widget: false,
Prod: true,
Dev: false
},
minify: htmlWebpackMinifyConfig,
}),
// separate html file for widgets
new HtmlWebpackPlugin({
filename: 'widgets/index.html',
chunks: ['vendor', 'widgets'],
chunksSortMode: 'dependency',
title: 'RCN.io Widgets',
template: path.resolve(consts.SRC_DIR, 'client/index.html.ejs'), // Load a custom template
inject: false, // we use custom template to inject scripts,
hash: true,
env: {
Widget: true,
Prod: true,
Dev: false
},
minify: htmlWebpackMinifyConfig,
}),
// separate html file for admin
new HtmlWebpackPlugin({
filename: 'admin/index.html',
chunks: ['vendor', 'admin'],
chunksSortMode: 'dependency',
title: 'RCN.io/admin',
template: path.resolve(consts.SRC_DIR, 'client/admin/index.html.ejs'), // Load a custom template
inject: false, // we use custom template to inject scripts,
hash: true,
env: {
Admin: true,
Prod: true,
Dev: false
},
minify: htmlWebpackMinifyConfig,
})
],
resolve: {
root: [
//path.resolve(__dirname, 'src/'),
path.resolve(__dirname, 'src/client'),
path.resolve(__dirname, 'src/')
],
//tells webpack to use static file when import React from 'react' is used
alias: commonConfig.resolve.alias
},
module: {
/* tells webpack to skip parsing following libraries
requires use of "import loader" for certain modules, based on https://github.com/christianalfoni/react-webpack-cookbook/issues/30
*/
noParse: commonConfig.module.noParse,
loaders: commonConfig.module.loaders.concat([{
test: /\.scss$/,
loaders: ['style', extractCss.extract('css!postcss!sass')],
//loaders: ['style', 'css?localIdentName=[name]_[local]_[hash:base64:3]', 'sass'],
exclude: /node_modules/,
include: path.join(consts.SRC_DIR, 'client')
}])
},
//required to have proper rem to px calcualtion, default floating point precision is not enough
//since most browsers use 15, SASS only uses 5, this leads to calculated size in px like 38.0001px
sassLoader: {
precision: 15,
includePaths: [path.join(consts.SRC_DIR, 'client/styles')]
},
postcss: function() {
return [
require('autoprefixer')({
remove: true, //enabling of removal of outdated prefixes, just in case
browsers: ['last 2 versions']
})
]
}
}
module.exports = config
// const addBundleAnalyzer = require('./webpack/utils').addBundleAnalyzer
// module.exports = addBundleAnalyzer(config)