-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
118 lines (113 loc) · 4.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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');//压缩css插件
module.exports = {
//entry入口文件
// 多页面开发,配置多页面,多页面需用数组格式
entry: {
index: './src/example/js/example.js'
},
// output出口文件
output: {
filename: "[name].[hash].js",
path: path.resolve(__dirname, 'dist')
},
devServer: { //webpack-dev-server 能够实时重新加载
disableHostCheck: true,
contentBase: path.join(__dirname, "dist"),
host: 'localhost',
port: 8080,
open: true, // 是否自动打开浏览器
hot: true, // 开启热更新
inline: true //实时刷新
},
// module处理对应模块
module: {
rules: [
{
test: /\.(sa|sc|c|le)ss$/,
use: ExtractTextWebpackPlugin.extract({
use: [
{
loader: 'css-loader'
},
{
loader: 'less-loader'
},
//引入postcss-loader
{
loader: 'postcss-loader',
options: {
plugins: [
autoprefixer({
// browsers: ['ie >= 8', 'Firefox >= 20', 'Safari >= 5', 'Android >= 4', 'Ios >= 6', 'last 4 version'],
remove: true
})
]
}
}
],
publicPath: '../'
})
},
//加载图片
{
test: /\.(png|jpe?g|gif|svg)$/,
use: [
{
loader: 'file-loader',
options: {
limit: 8192,
name: '[name].[ext]',
publicPath: "./images",
outputPath: "images/"
},
}
]
},
//加载字体
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: ['file-loader']
},
//转义ES6兼容低版本浏览器
{
test: /\.js$/,
use: ['babel-loader'],
include: /src/,
exclude: /noder_modules/
}
]
},
// plugins对应的插件
plugins: [
// 在npm start命令下,打包的文件存在于内存中,并不会产生在dist目录下
new CleanWebpackPlugin(['dist']), //打包之前清理dist文件夹里面多余的文件,npm start时会清理掉dist
new webpack.NamedModulesPlugin(),
// 热替换,热替换不是刷新
new webpack.HotModuleReplacementPlugin(),
new OptimizeCssAssetsPlugin(),
new HtmlWebpackPlugin({
template: './src/example/index.html',
filename: 'index.html',
chunks: ['index'],
hash: true,
minify:{
removeAttributeQuotes:true,//去除引号
removeComments:false,//去除注释
removeEmptyAttributes:true,//去除空属性
collapseWhitespace:false//去除空格
}
}),
// 拆分后会把css文件放到dist目录下的css/style.css
new ExtractTextWebpackPlugin('css/[name].[hash].css'),
],
// resovle解析,配置别名和省略后缀名
resolve: {
extensions: ['.js','.json','.css','.scss','.less']
}
};