This repository has been archived by the owner on Apr 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
155 lines (145 loc) · 4.19 KB
/
webpack.config.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
'use strict';
// isProd is true when we run this command - npm run build
const isProd = (process.env.npm_lifecycle_event === 'build');
console.log('\x1b[96m isProduction- %s\x1b[0m \n', isProd);
const webpack = require('webpack');
const path = require("path");
// Webpack plugins
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// Add more from here
// https://webpack.js.org/configuration/
module.exports = {
context: __dirname, //home directory for webpack
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules']
},
entry: {
app: './src/js/app.js',
vendor: ['jquery', 'bootstrap']
},
output: {
path: path.resolve(__dirname, 'dist'),// where to store build files
publicPath: '',// to be used in index.html
filename: "js/[name].[" + (isProd ? 'chunkhash' : 'hash') + "].js" // build file name
},
module: {
rules: [
// Catch js files and compile them to es5
{
test: /\.js$/,
loader: 'babel-loader',
options: {
presets: [
['env', {
'modules': false,
'targets': {
'browsers': ['> 2%'],
uglify: true
}
}]
]
},
include: path.resolve(__dirname, 'src'),
exclude: path.resolve(__dirname, 'node_modules'),
},
// Catch all css, extract and store them to a separate file
{
test: /\.css$/,
loader: isProd ? ExtractTextPlugin.extract({
fallback: 'style-loader',
loader: 'css-loader',
}) : 'style-loader!css-loader',
},
// Catch image files and store them in separate folder
// todo use url-loader to base64 small files
{
test: /\.jpe?g$|\.gif$|\.png$/i,
loader: 'file-loader?name=[name].[hash].[ext]' + (isProd ? '&publicPath=/&outputPath=img/' : ''),
},
// Catch all fonts and store them to a separate folder
{
test: /\.(woff|woff2|eot|ttf|svg)(\?.*$|$)/,
loader: 'file-loader?name=[name].[ext]?[hash]' + (isProd ? '&publicPath=/&outputPath=fonts/' : ''),
}
]
},
plugins: [
// Minify HTML + Auto inject assets in html
new HtmlWebpackPlugin({
inject: true,
hash: false,
template: './src/index.html',
minify: {
removeComments: isProd,
collapseWhitespace: isProd,
removeAttributeQuotes: isProd,
minifyJS: isProd,
minifyCSS: isProd,
minifyURLs: isProd
// More options here
// https://github.com/kangax/html-minifier#options-quick-reference
}
}),
// Expose some global vars in our code
new webpack.DefinePlugin({
API_CONFIG: JSON.stringify({
'baseUrl': 'http://localhost:8080/api/v1'
}),
'process.env': {
NODE_ENV: isProd ? '"production"' : '"development"'
}
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
new webpack.optimize.CommonsChunkPlugin('vendor')
],
// Dev server related configs
devServer: {
contentBase: path.resolve(__dirname, 'src'),
port: 9000,
host: 'localhost',
open: true,
inline: true,
hot: true,
noInfo: false,
quiet: false,
stats: 'errors-only'
},
devtool: isProd ? false : '#cheap-eval-source-map',
watch: false,
target: 'web'
};
let plugins = [];
if (isProd) {
// Production only plugins
plugins.push(
// Minify JS files
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console: true,
drop_debugger: true,
},
output: {
comments: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
}),
// Extract css and store them into a separate file
new ExtractTextPlugin('css/styles.[hash].css'),
new webpack.BannerPlugin('Webpack example (c) 2016')
)
} else {
// Dev only plugins
plugins.push(
// Required when hot = true in dev server configs
new webpack.HotModuleReplacementPlugin()
);
}
module.exports.plugins = (module.exports.plugins || []).concat(plugins);