-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
149 lines (140 loc) · 3.07 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
const webpack = require('webpack')
const path = require('path')
const CleanWebpackPlugin = require('clean-webpack-plugin') // 清理目录
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin') // 抽取html
const MiniCssExtractPlugin = require('mini-css-extract-plugin') // 抽取css
const pages = [
{
template: './src/views/vue_test/vue_test.html',
filename: 'vue_test.html',
chunks: ['vue_test']
}
]
module.exports = {
mode: 'development',
entry: {
vue_test: './src/views/vue_test/vue_test.js' // 入口文件
},
output: {
filename: 'static/js/[name].[hash:5].js',
path: path.resolve(__dirname, './build') // 打包目录
},
module: {
rules: [
// js
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
// vue
{
test: /\.vue$/,
use: ['vue-loader']
},
// css
{
test: /\.css$/,
exclude: '/node_modules',
use: [
{
loader: MiniCssExtractPlugin.loader
},
{
loader: 'css-loader'
}
]
},
{
test: /.less$/,
exclude: '/node_modules',
use: [
'style-loader',
'css-loader',
'postcss-loader',
'less-loader',
// {
// loader: 'style-loader'
// }, // 项目运行的时候,动态追加到html中,用style标签把样式包起来
// {
// loader: 'css-loader'
// },
// {
// loader: 'less-loader'
// },
// {
// loader:"postcss-loader",
// options: {
// plugins: () => [
// require('autoprefixer') ({
// overrideBrowserslist: ['last 2 version', '>1%', 'ios 7']
// })
// ]
// }
// }
]
},
// 图片
{
test: /\.(png|jpg|gif)$/,
exclude: '/node_modules',
use: [{
loader: 'url-loader',
options: {
limit: 8192,
name: '[name].[hash:5].[ext]',
outputPath: 'static/images',
// publicPath: '../images' // 指定图片目录,解决图片路径不对的问题
}
}]
}
]
},
externals: {
vue: 'Vue'
},
plugins: [
new CleanWebpackPlugin(), // 默认打包目录
new CopyWebpackPlugin([{
from: path.resolve('public'), //将当前目录下的public
to: 'public', //复制到output.path下的public
ignore: ['.*']
}]),
new MiniCssExtractPlugin({
filename: 'static/css/[name].[hash:5].css',
}),
new HtmlWebpackPlugin(pages[0])
],
devServer: {
open: true, // 自动打开浏览器
hot: true,
compress: true, // 资源文件通过压缩传输
port: 3001,
noInfo: true, // 控制台不打印信息
contentBase: 'build' // 设置根 http://localhost:3001
},
resolve: {
extensions: ['.js', '.json', '.vue'],
alias: {
'@': path.join(__dirname, "src"),
}
},
stats: {
children: false // 用于解决Entrypoint undefined = index.html问题
},
}
// function createHtml({
// template,
// filename,
// chunks
// }) {
// return new HtmlWebpackPlugin({
// template,
// filename,
// chunks,
// })
// }
// pages.forEach(element => {
// module.exports.plugins.push(createHtml(element))
// });