-
Notifications
You must be signed in to change notification settings - Fork 69
/
webpack.config.js
168 lines (159 loc) · 5.44 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var path = require('path');
var config = require('./config');
var webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var vueConfig = require('./vue-loader.config');
var env = process.argv[2];
var fs = require('fs');
var entries = { 'vendor': config.vendor };
// 支持驼峰
var fistLetterUpper = function(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};
var buildEntryPoint = function(entryPoint) {
if (env !== 'hot-reload') {
return entryPoint;
}
return [
'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000&reload=true',
entryPoint
]
}
// 动态生成entry
fs.readdirSync(config.entryDir).filter(function(folder) {
var path = config.entryDir + folder;
var stat = fs.lstatSync(path);
if (stat.isDirectory()) {
fs.readdirSync(path).filter(function(name) {
var c_path = path + '/' + name;
var c_stat = fs.lstatSync(c_path);
if (c_stat.isDirectory()) {
entries[folder + fistLetterUpper(name)] = buildEntryPoint(c_path + '/' + name + '.js');
}
});
}
});
// 定义plugin
var plugins = [
new ExtractTextPlugin({
filename: "css/[name].css",
allChunks: true
}),
new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
filename: "js/vendor.js",
minChunks: Infinity
}),
new webpack.DefinePlugin({
'__DEV__': process.env.NODE_ENV === 'production' ? false : true
})
];
if (env === 'hot-reload') {
var arr = [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
]
plugins = arr.concat(plugins);
console.log('hot reload listening……')
}
module.exports = {
entry: entries,
output: {
path: path.join(__dirname, config.distDir),
filename: 'js/[name].js',
publicPath: '/wuji/dist/' //方便定位到CDN静态资源,配合url-loader
},
//引入第三方库
externals: {
'calendar': 'calendar',
'particlesJS': 'particlesJS'
},
module: {
rules: [{
test: /\.vue$/,
loader: 'vue-loader',
options: vueConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
//需要有相应的css-loader,因为element-ui css在node_moudle
test: /\.css$/,
loader: ExtractTextPlugin.extract({
use: "css-loader",
fallback: 'style-loader'
})
},
{
test: /\.(scss|sass)$/,
loader: ExtractTextPlugin.extract({
use: "css-loader!sass-loader",
fallback: 'style-loader'
})
},
{
//同上
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loader: 'file-loader'
},
{
test: /\.(png|jpg|gif)$/,
loader: 'url-loader?limit=8192'
}]
},
resolve: {
// require时省略的扩展名,不再需要强制转入一个空字符串,如:require('module') 不需要module.js
extensions: [".js", ".json", ".vue", ".scss", ".css"],
//require路径简化
alias: {
//Vue 最早会打包生成三个文件,一个是 runtime only 的文件 vue.common.js,一个是 compiler only 的文件 compiler.js,一个是 runtime + compiler 的文件 vue.js。
//vue.js = vue.common.js + compiler.js,默认package.json的main是指向vue.common.js,而template 属性的使用一定要用compiler.js,因此需要在alias改变vue指向
vue: 'vue/dist/vue',
config: path.resolve(__dirname, config.srcDir + '/config'),
filter: path.resolve(__dirname, config.srcDir + '/filters'),
utils: path.resolve(__dirname, config.srcDir + '/utils'),
stores: path.resolve(__dirname, config.srcDir + '/stores'),
routers: path.resolve(__dirname, config.srcDir + '/routers'),
modules: path.resolve(__dirname, config.srcDir + '/modules'),
component: path.resolve(__dirname, config.srcDir + '/component')
}
},
plugins: plugins,
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
vueConfig.loaders = {
sass: ExtractTextPlugin.extract({
use: 'css-loader!sass-loader',
fallback: 'vue-style-loader'
})
}
//是否压缩
if(config.isGzip){
var CompressionWebpackPlugin = require('compression-webpack-plugin');
module.exports.plugins.push(new CompressionWebpackPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.html$|\.css$/,
threshold: 10240,
minRatio: 0.8
}))
}
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}