webpack.config.js
3.8 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
/**
* @file webpack 配置文件
*/
const path = require('path')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { VueLoaderPlugin } = require('vue-loader')
const hashDigestLength = 6 // 文件名末尾 hash 值长度
/**
* webpack 配置项
* @param {Object} env - 环境对象
* @param {Object} argv - 命令行参数选项
* @param {String} argv.mode - 模式
*/
module.exports = function (env = {}, argv) {
let productionEnv = (!argv.host && argv.mode == 'production'); //生产环境下打包情况不一样,先清除旧的dist,部分文件的publicPath也不一样
let config = {
devtool: productionEnv ? '' : 'source-map',
resolve: {
alias: {
'@': path.resolve(__dirname, '../src'),
components: path.resolve(__dirname, '../src/components'),
images: path.resolve(__dirname, '../src/images'),
modules: path.resolve(__dirname, '../src/modules'),
scripts: path.resolve(__dirname, '../src/scripts'),
styles: path.resolve(__dirname, '../src/styles'),
}
},
entry: {
app: './src/scripts',
},
output: {
// filename: 'scripts/[name]_[chunkhash].js',
filename: 'scripts/[name].js',
hashDigestLength,
// path: path.resolve(__dirname, productionEnv ? '../../dist/m/' : '../dist/'),
path: path.resolve(__dirname, productionEnv ? '../dist/' : '../dist/'),
},
module: {
rules: [{
test: /\.(jpg|png|svg|gif)$/,
use: [{
loader: 'file-loader',
options: {
// name: `[name]_[hash:${hashDigestLength}].[ext]`,
name: `[name].[ext]`,
outputPath: 'images',
publicPath: productionEnv ? '/images/' : '/images/',
//https://inside.wx.luckyxp.com.cn/webup/data/User/wxtest/home/project/lls-m/dist/images/
}
}]
}, {
test: /\.(woff|ttf|woff2|eot)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'styles',
publicPath: './'
}
}
]
}, {
test: /\.(css|postcss)$/,
use: [{
loader: MiniCssExtractPlugin.loader
}, {
loader: 'css-loader',
options: {
importLoaders: 1
}
}, {
loader: 'postcss-loader',
}]
}, {
test: /\.js$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader'
}]
}, {
test: /\.vue$/,
use: [{
loader: 'vue-loader',
options: {
compilerOptions: {
preserveWhitespace: false
}
}
}]
}]
},
plugins: [
new VueLoaderPlugin(),
new MiniCssExtractPlugin({
filename: 'styles/[name]_[chunkhash].css'
// filename: 'styles/[name].css'
}),
new HtmlWebpackPlugin({
title: 'AFK Journey',
template: './src/index.html',
})
],
optimization: {
runtimeChunk: {
name: 'manifest'
},
splitChunks: {
chunks: 'all',
minChunks: 2
}
},
performance: {
hints: false
},
devServer: {
contentBase: path.join(__dirname, '../'),
disableHostCheck: true,
open: true,
host: 'localhost',
// host: '192.168.17.6',
// host: 'inside.wx.luckyxp.com.cn',
port: 8001,
proxy: {
}
}
}
config.plugins = config.plugins.concat([
// new CleanWebpackPlugin([productionEnv ? 'm' : 'dist'], {
// root: path.join(__dirname, productionEnv ? '../../dist/' : '../')
// }),
new CleanWebpackPlugin(['dist'], {
root: path.join(__dirname, '../')
}),
])
return config
}