-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
270 lines (216 loc) · 5.4 KB
/
webpack.config.js
File metadata and controls
270 lines (216 loc) · 5.4 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/****************************************/
/******* WEBPACK CONFIG *********/
/****************************************/
const webpack = require("webpack");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
/****************************************/
/******* CONFIG OBJECT *********/
/****************************************/
const WEBPACK_CONFIG = { module: {} };
/****************************************/
/******* ENVRIONMENTS *********/
/****************************************/
/*
* test whether the script will be run in
* production using "npm run build" from
* the terminal. If true, file names will
* be hashed, js will be minified.
*/
const isProduction = JSON.parse(process.env.PROD_ENV ? true : false);
/***************************************/
/********* INPUT **********/
/***************************************/
const input = {
context: __dirname,
entry: ['./main.js', './main.less'],
devtool: isProduction ? '' : 'eval',
node: {
dns: 'mock',
net: 'mock',
fs: 'empty'
},
resolve: {
alias: {
'masonry': 'masonry-layout',
'isotope': 'isotope-layout',
'vue': 'vue/dist/vue.min.js'
}
}
};
//extend properties to config
Object.assign(WEBPACK_CONFIG, input);
/****************************************/
/******** LOADERS / RULES ***********/
/****************************************/
/*
* each loader will push to this rules
* array then added to WEBPACK_CONFIG.
*/
const rules = [];
/*********************/
// @rule: JS
const JSRules = {
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
use: [
"babel-loader",
{
loader: "eslint-loader",
options: {
emitWarning: true,
fix: true
}
},
]
};
rules.push(JSRules);
//@rule: Vue
const vueRules = {
test: /\.vue$/,
use: [{
loader: 'vue-loader',
options: {
extractCSS: true
}
}]
}
rules.push(vueRules);
/*********************/
// @rule: extract all less, compile and apply post css prefixing
const lessRules = {
test: /\.less$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
},
{
loader: 'less-loader',
}
]
})
};
rules.push(lessRules);
/*********************/
// @rule: HTML
const HTMLRules = {
test: /\.html$/,
exclude: /node_modules/,
use: {
loader: 'html-loader'
}
}
rules.push(HTMLRules);
/*********************/
// @rule: css autoprefixer
const CSSRules = {
test: /\.css$/,
use: [
'style-loader',
{
loader: 'postcss-loader',
}
]
};
rules.push(CSSRules);
/*********************/
// @rule: json
const JSONRules = {
test: /\.json$/,
use: "json-loader"
};
rules.push(JSONRules);
/*********************/
WEBPACK_CONFIG.module.rules = rules;
/***************************************/
/********** PLUGINS **********/
/***************************************/
/*
* each plugin will push to this plugins
* array. Some will only be pushed when
* config is set to production.
*/
const plugins = [];
/*********************/
// @plugin: node env
const nodeENV = new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
});
isProduction ? plugins.push(nodeENV) : false;
/*****************************/
// @plugin: compile all less files into master CSS
const CSSBundle = new ExtractTextPlugin({
filename: "bundle.css"
});
plugins.push(CSSBundle);
/*********************/
// @plugin: extend jquery for jquery plugins
const jQueryExtend = new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
});
plugins.push(jQueryExtend);
/*********************/
// @plugin: handling es6 promises
const promises = new webpack.ProvidePlugin({
'Promise': 'es6-promise',
'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
});
plugins.push(promises);
/*********************/
// @plugin: post CSS
const postCSS = [
require('autoprefixer')
]
postCSS.forEach((item) => {
plugins.push(item);
});
/*********************/
//@plugin: Vue components
const vueComponents = new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
})
if (isProduction) {
plugins.push(vueComponents);
}
// @plugin: for minifying javascript
const minify = new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
output: {
comments: isProduction ? false : true,
},
minimize: isProduction ? true : false,
debug: false,
sourceMap: true,
minify: isProduction ? true : false,
});
//if production is set, js will be minified
plugins.push(minify);
//output to config object
WEBPACK_CONFIG.plugins = plugins;
/************************************/
/******** OUTPUT *******/
/************************************/
const output = {
output: {
publicPath: '/',
path: __dirname + "/template/assets",
filename: "bundle.js"
}
};
//extend properties to config
Object.assign(WEBPACK_CONFIG, output);
//export config
module.exports = WEBPACK_CONFIG;