-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
121 lines (114 loc) · 3.84 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
var webpack = require("webpack");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var path = require('path');
const WebpackBundleSizeAnalyzerPlugin = require('webpack-bundle-size-analyzer').WebpackBundleSizeAnalyzerPlugin;
var CompressionWebpackPlugin = require('compression-webpack-plugin');
// 定义函数判断是否是在当前生产环境,这个很重要,一位开发环境和生产环境配置上有一些区别
var isProduction = function () {
console.log(process.env.NODE_ENV == 'production');
console.log(process.env.NODE_ENV );
if(process.env.NODE_ENV){
console.log(process.env.NODE_ENV == 'production');
}
// return process.env.NODE_ENV == 'production';
return process.env.NODE_ENV;
};
var plugins = [
new webpack.BannerPlugin("write by dogLin"),
new webpack.HotModuleReplacementPlugin(),
new ExtractTextPlugin("css/styles.css"),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: 'vendor.js',
minChunks: Infinity,
}),
new WebpackBundleSizeAnalyzerPlugin('./plain-report.txt'),
new HtmlWebpackPlugin({
template:__dirname + '/app/index.tmpl.html',
inject: true, // 自动注入
minify: {
removeComments: true, //去注释
collapseWhitespace: true, //压缩空格
removeAttributeQuotes: true //去除属性引用
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
//必须通过上面的 CommonsChunkPlugin 的依赖关系自动添加 js,css 等
chunksSortMode: 'dependency'
}),
// new CompressionWebpackPlugin({ //gzip 压缩
// asset: '[path].gz[query]',
// algorithm: 'gzip',
// test: new RegExp(
// '.(js|less|css)$' //压缩 js 与 css,less
// ),
// threshold: 10240,
// minRatio: 0.8
// }),
]
if( isProduction() ) {
console.log('xx');
plugins.push(
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
new webpack.optimize.UglifyJsPlugin({
comments: false, //去掉注释
compress: {
warnings: false //忽略警告,要不然会有一大堆的黄色字体出现……
}
})
)}
module.exports = {
devtool:isProduction()?null:'source-map',
entry:{
app:path.resolve(__dirname ,'react/index.react.js'),
vendor: ['react', 'react-dom','react-router','jquery']
},
output:{
path: path.resolve(__dirname ,'build'),
publicPath: "/",
filename: "[name].js"
},
module:{
loaders:[
{
test:/\.json$/,
loader:'json'
},
{
test:/.\js$/,
exclude:/node_modules/,
loader:'babel',
query: {
presets: ['react', 'es2015'], //设定babel的转码规则
"plugins": [
["import", { libraryName: "antd", "style": "css" }] // `style: true` 会加载 less 文件
]
}
},
{
test:/\.css$/,
loader:'style!css'
},
{
test:/\.less$/,
loader: ExtractTextPlugin.extract('style-loader', 'css?modules&importLoaders=1&localIdentName=[local]--[hash:base64:5]&-url&sourceMap!less?sourceMap')
}
]
},
watch:true,
postcss:[
require('autoprefixer')
],
plugins:plugins,
devServer:{
colors:true,
historyApiFallback:true,
inline:true,
hot:true
}
}