-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
142 lines (138 loc) · 3.34 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
const {
resolve
} = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
//css单独分离
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const webpack = require('webpack');
//每次打包后的缓存文件进行删除确保每次都是最新的
const {
CleanWebpackPlugin
} = require('clean-webpack-plugin');
//设置环境变量
process.env.NODE_ENV = 'development'
console.log(this.mode)
module.exports = {
entry: {
//首页
index: ['./src/js/index.js', './src/index.html'],
url: ['./src/js/url.js', './src/url.html'],
},
output: {
path: resolve(__dirname, './dist'),
filename: this.mode === 'production' ? 'js/[name].[chunkhash].js' : 'js/[hash:10].js' //每次打包hash改变,造成缓存失效 在生产环境才有效(chunkhash,hash,contenthash)
},
module: {
rules: [
//css
{
test: /\.css$/,
use: [
//"style-loader",
{
loader: MiniCssExtractPlugin.loader
},
{
loader: 'css-loader',
// options: {
// modules: {
// localIdentName: '[path][name]__[local]--[hash:base64:5]'
// }
// }
},
{
loader: "postcss-loader"
}
]
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
},
'less-loader'
]
},
//图片
{
test: /\.(png|jpg|gif)$/,
loader: 'url-loader',
options: {
limit: 8 * 1024,
esModule: false,
name: "[hash:10].[ext]",
outputPath: 'images'
}
},
{
test: /\.html$/,
loader: 'html-loader'
},
//处理其他资源
{
exclude: /\.(html|css|js|less|jpg|png|gif)/,
loader: "file-loader",
options: {
name: "[hash:10].[ext]",
outputPath: 'vendor'
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
title: "首页",
template: './src/index.html',
filename: 'index.html',
inject: 'body',
chunks: ['index'],
}),
new HtmlWebpackPlugin({
title: "网址",
template: './src/url.html',
filename: 'url.html',
chunks: ['url']
}),
new MiniCssExtractPlugin({
// 对输出的css文件进行重命名
filename: 'css/[contenthash].css', //多文件不同命名 (hash不行) 用contenthash
chunkFilename: '[id].css',
ignoreOrder: true,
}),
new CleanWebpackPlugin()
// new webpack.HotModuleReplacementPlugin({
// // Options...
// })
],
mode: 'development',
externals: {
//jquery: 'jquery'
},
devtool: 'source-map', //调试代码
//配置测试服务器
devServer: {
contentBase: resolve(__dirname, 'dist'),
compress: true,
hot: true,
port: 3000,
open: false,
// 除了一些基本启动信息以外,其他内容都不要显示
//quiet: true
// proxy: {
// '/api': {
// target: 'https://cli.im',
// ws: true, //代理websockets,配置这个参数
// pathRewrite: { '^/api': '/' },
// changeOrigin: true,
// secure: false
// }
// }
},
optimization: {
splitChunks: {
chunks: 'all'
}
}
}