-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
161 lines (159 loc) · 6.13 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
/**
* Created by Administrator on 2017/10/9.
*/
const path = require('path');
var webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');//清理dist目录文件
const HtmlWebpackPlugin = require('html-webpack-plugin'); //html导出
const ExtractTextPlugin = require('extract-text-webpack-plugin');//分离样式表
const extractCSS = new ExtractTextPlugin(process.env.NODE_ENV === 'production' ? 'css/[name]-css.min.css' : 'css/[name]-css.css');//导出css
const extractSass = new ExtractTextPlugin(process.env.NODE_ENV === 'production' ? 'css/[name]-less.min.css' : 'css/[name]-less.css')//导出sass
const balili = require('babili-webpack-plugin');//压缩代码
const autoprefixer = require('autoprefixer'); //补全css各种hack
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');//压缩css
module.exports = {
devtool: 'cheap-source-map',
/*devServer: {
historyApiFallback: false,
inline: true,//注意:不写hot: true,否则浏览器无法自动更新;也不要写colors:true,progress:true等,webpack2.x已不支持这些
},*/
entry: {
'index': ['./src/index.js'],
},
output: {
filename: 'js/[name].[hash].js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
//使用babel加载.js文件
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['es2015', 'react','stage-1']
}
},
},
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['es2015','react','stage-1']
}
}
},
//加载css
{
test: /\.css$/,
exclude: /(node_modules|bower_components)/,
use: extractCSS.extract({
fallback: 'style-loader',
use: ['css-loader', 'postcss-loader'],
publicPath: '../'
})
},
{
test: /\.scss/i,
use: extractSass.extract({
fallback: 'style-loader',
use: ['css-loader', 'sass-loader', 'postcss-loader'],
publicPath: "../"
})
},
{
test: /\.less$/,
use: extractSass.extract({
fallback: 'style-loader',
use: ['css-loader', 'less-loader', 'postcss-loader'],
publicPath: '../'
})
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
'css': ExtractTextPlugin.extract({
use: ['css-loader?minimize=true', 'postcss-loader'],
fallback: 'vue-style-loader',
publicPath: "../"
}),
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
scss: ExtractTextPlugin.extract({
use: ['css-loader?minimize=true!', 'sass-loader', 'postcss-loader'],
fallback: 'vue-style-loader',
publicPath: "../"
}),
'sass': ExtractTextPlugin.extract({
use: ['css-loader?minimize=true!', 'sass-loader?indentedSyntax', 'postcss-loader'],
fallback: 'vue-style-loader',
publicPath: "../"
}),
}
// other vue-loader options go here
}
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
'url-loader?limit=8192000&hash=sha512&digest=hex&name=img/[hash].[ext]',
{
loader: 'image-webpack-loader',
options: {
gifsicle: {
interlaced: false
},
optipng: {
optimizationLevel: 1
},
pngquant: {
quality: '65-90',
speed: 4
},
mozjpeg: {
progressive: true,
quality: 65
}
}
}
]
}
]
},
resolve: {
/*alias: {
'vue$': 'vue/dist/vue.esm.js'
},*/
extensions: ['*', '.js', '.json','.jsx']
},
plugins: [
//全局挂载
/*new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),*/
new CleanWebpackPlugin(['dist/js', 'dist/css', 'dist/img']),
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.html',
inject: 'body',
chunks: ['index'],
hash: true
}),
extractCSS,
extractSass,
(process.env.NODE_ENV === 'production') ? new balili() : function () {
},//区分生产环境与开发环境,压缩js
(process.env.NODE_ENV === 'production') ? new OptimizeCssAssetsPlugin() : function () {
}//区分生产环境与开发环境,压缩css
//new webpack.HotModuleReplacementPlugin()
]
}