-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
126 lines (119 loc) · 3.24 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
// webpack.config.js
const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
//define output dev folders
const outputDev = "dist/dev";
const outputProd = "dist/prod";
module.exports = function(env){
let outputFolder=outputDev;
let dev = !(env && env.prod);
if(!dev){
outputFolder=outputProd;
}
const config = {
// Tell webpack to start bundling our app at src/index.js
entry: './src',
// Output our app to the output folder
output: {
filename: 'app.js',
path: path.resolve(__dirname, outputFolder)
},
//Emit source map only for dev mode
devtool: dev?'source-map':false,
module: {
rules: [
//for json
{
test: /\.json$/,
loader: 'json-loader'
},
//Tell webpack to run our source code through Babel
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
},
//lint code before building
{
test: /\.(js|jsx)$/,
loader: 'eslint-loader',
enforce: 'pre',
exclude: /node_modules/,
options:{
failOnWarning: true,
failOnError: true
}
},
//compile less files into css
{
test: /\.less$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
//resolve-url-loader may be chained before sass-loader if necessary
use: ['css-loader', 'less-loader']
})
}
],
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
loader: 'less-loader',
options: {
//source map only in dev
sourceMap: dev
}
}
]
},
// Since Webpack only understands JavaScript, we need to
// add a plugin to tell it how to handle html files.
plugins: [
new CleanWebpackPlugin([outputDev,outputProd],{verbose: true,}),
// Configure HtmlPlugin to use our own index.html file
// as a template.
// Check out https://github.com/jantimon/html-webpack-plugin
// for the full list of options.
new HtmlPlugin({
template: 'src/index.html'
}),
new ExtractTextPlugin('style.css'),
//Copy assets from source
new CopyWebpackPlugin([
{
from: 'src/assets',
to: 'assets'
},
])
]
}
if(!dev){
//define some env variables for production
config.plugins.push(
new webpack.DefinePlugin({
'process.env':{
'NODE_ENV': JSON.stringify('production')
}
}));
//uglify for production
config.plugins.push(new webpack.optimize.UglifyJsPlugin({
sourceMap: false,
compress: {
warnings: false,
drop_console: true
},
output: {
comments: false
}
}));
}
return config;
}