Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
154 lines (150 loc) · 5.47 KB
/
Copy pathwebpack.config.js
File metadata and controls
154 lines (150 loc) · 5.47 KB
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
const path = require('path') //path是webpack中的基本包,处理路径
const HtMLPlugin = require('html-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
// // 启动脚本时的变量存在于process.env变量中
const isDev = process.env.NODE_ENV === 'development'
const webpack = require('webpack')
const ExtractPlugin = require('extract-text-webpack-plugin')
const config = {
// webpack的编译目标是web平台
target: 'web',
//入口文件
entry: path.join(__dirname, 'src/index.js'), //__dirname表示当前文件所在的目录地址,利用join()拼接成绝对路径
// 输出文件
output: {
filename: 'bundle.[hash:8].js',//输出文件名,开发环境不能使用chunkhash,正式环境中需要进行一个修改
path: path.join(__dirname, 'dist')
},
module: {
rules: [
{
// 以vue-load 处理以vue结尾的文件,确保正确输出js代码
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.jsx$/,
loader: 'babel-loader'
},
{
test:/\.css$/,
// style-loader是将外部css文件注入html文件中,最后将html文件中的css 用css-loader进行解析
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(gif|jpg|jpeg|png|svg)$/,
use:[
{
loader: 'url-loader',
//将图片转化成base64的代码,直接写在js内容里而不用生成新的文件,减少http请求
//还可以指定输出的文件名字,
options: {
limit:1024,
name:'[name]-aaa.[ext]'
}
}
]
}
]
},
plugins: [
//能够在js代码中引用到,并且vue也能够根据此进行分类打包
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: isDev ? '"development"' : '"production"'
}
}),
new HtMLPlugin(),
new VueLoaderPlugin()
]
}
// // 配置是根据不同的环境判断,通过设置一个环境变量来判断
if (isDev) {
//如果是开发环境
config.module.rules.push({
test: /\.styl/,
use: [
'style-loader',
'css-loader',
// {
'postcss-loader',
// options: {
// sourceMap: true, //指定这个值,后面stylus编译的时候就不用重新定值,编译效率变快
// }
// },
'stylus-loader'
]
});
config.devtool = '#cheap-module-eval-source-map'
config.devServer = {
port: 8088,
//host 可以通过localhost进行访问,同时也可以通过本机的内网id进行访问,就能够在别的网页或者手机上访问
host:'0.0.0.0',
//overlay 编译过程有任何错误都直接显示到网页上
overlay: {
errors: true
},
// 当修改组件代码时,只重新渲染当前组件,不会让整个页面重新加载
hot: true
// 能够在运行后直接打开浏览器
// open: true
}
// 启动webpack.hot功能的插件
config.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
)
} else {
config.entry = {
app: path.join(__dirname, 'src/index.js'), //__dirname表示当前文件所在的目录地址,利用join()拼接成绝对路径
vendor: ['vue']
}
config.output.filename = '[name].[chunkhash:8].js'
config.module.rules.push({
test: /\.styl/,
use: ExtractPlugin.extract({
fallback: 'style-loader',
use: [
'css-loader',
'postcss-loader',
'stylus-loader'
]
})
});
config.plugins.push(
// 指定输出的静态文件的名字
new ExtractPlugin('styles.[chunkhash:8].css')
// 类库文件单独打包
// new webpack.optimize.CommonsChunkPlugin({
// name: "vendor"
// })
// webpack相关的代码单独打包到一个文件里
// new webpack.optimize.CommonsChunkPlugin({
// name: 'runtime'
// })
)
config.optimization = {
splitChunks: {
cacheGroups: { // 这里开始设置缓存的 chunks
commons: {
chunks: 'initial', // 必须三选一: "initial" | "all" | "async"(默认就是异步)
minSize: 0, // 最小尺寸,默认0,
minChunks: 2, // 最小 chunk ,默认1
maxInitialRequests: 5 // 最大初始化请求书,默认1
},
vendor: {
test: /node_modules/, // 正则规则验证,如果符合就提取 chunk
chunks: 'initial', // 必须三选一: "initial" | "all" | "async"(默认就是异步)
name: 'vendor', // 要缓存的 分隔出来的 chunk 名称
priority: 10, // 缓存组优先级
enforce: true
}
}
},
runtimeChunk: true
}
}
module.exports = config