-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
148 lines (132 loc) · 4.08 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
/**
* webpack.config
*/
// 定义模式 development/production
const mode = 'development';
// 外部插件(constructor)
// webpack
const webpack = require('webpack');
// 清理输出目录
const cleanWebpackPlugin = require('clean-webpack-plugin');
// 生成入口模板,主要添加资源文件的引用
const htmlWebpackPlugin = require('html-webpack-plugin');
// 合并css,分隔第三方库
const extractTextWebpackPlugin = require('extract-text-webpack-plugin');
const externalCss = new extractTextWebpackPlugin('css/external-[contenthash:6].css');
const appCss = new extractTextWebpackPlugin('css/app-[contenthash:6].css');
module.exports = (function webpackConfig() {
var config = {};
// 入口文件
config.entry = {
vendor: ['jquery', 'angular', '@uirouter/angularjs'],
app: __dirname + '/app/bootstrap/bootstrap.js'
};
// 输出路径
config.output = {
path: __dirname + '/dist',
filename: 'js/[name]-[chunkhash:6].js',
publicPath: '/'
};
// module
config.module = {
// 加载文件的配置
rules: [
// 使用exclude与include区分第三方库样式
{
test: /\.(less|css)$/,
exclude: /node_modules/,
use: appCss.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
// 启用css压缩
options: {
minimize: true
}
}, {
loader: 'less-loader'
}]
})
},
{
test: /\.css$/,
include: /node_modules/,
use: externalCss.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
// 启用css压缩
options: {
minimize: true
}
}]
})
},
// 文件loader
{
test: /\.(ttf|woff2|woff|eot|svg|dtd|jpg|jpeg|png|gif)$/,
use: [{
loader: 'file-loader',
options: {
outputPath: 'fonts/',
name: '[hash:6].[ext]'
}
}]
},
{
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: 'jQuery'
},
{
loader: 'expose-loader',
options: '$'
}
]
}
]
};
// 插件
config.plugins = [
// 提取公共模块,这里对应entry的vendor
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: 'js/vendor-[chunkhash:6].js'
}),
new cleanWebpackPlugin(['dist/**/**'], {
root: __dirname
}),
new htmlWebpackPlugin({
template: __dirname + '/app/index.html'
}),
externalCss, appCss
];
// 开发环境配置
if (mode === 'development') {
config.devtool = 'inline-source-map';
// 开发环境服务器配置
config.devServer = {
// 服务器文件路径
contentBase: __dirname + '/dist',
// 开启GZIP
compress: true
};
}
// 生产环境配置
if (mode === 'production') {
config.devtool = 'source-map';
// 插件
config.plugins.push(
// 压缩js代码
new webpack.optimize.UglifyJsPlugin({
sourceMap: true
}),
// 设置node为生产环境
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
);
}
return config;
})();