-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
118 lines (115 loc) · 2.99 KB
/
webpack.config.js
File metadata and controls
118 lines (115 loc) · 2.99 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
const { resolve } = require("path")
const htmlPlugin = require("html-webpack-plugin")
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const VueLoaderPlugin = require("vue-loader/lib/plugin") // 插件必须要
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer")
const MiniCssExtractPlugin = require("mini-css-extract-plugin") // 将css 打包为一个文件
const htmlPluginConfig = {
title: "CAT! A",
template: resolve(__dirname, "html/index.html"),
}
const devServer = {
contentBase: resolve(__dirname, "dist"),
compress: true, // 压缩
port: 8888,
hot: true,
host: "127.0.0.1",
open: true,
}
module.exports = {
// mode: "development",
mode: "production",
entry: {
main: resolve(__dirname, "src/main.js"), // 入口文件
},
optimization: {
splitChunks: {
chunks: "all",
},
},
devtool: "inline-source-map", // 跟踪错误信息
devServer,
output: {
path: resolve(__dirname, "public"),
filename: "js/[name].[hash].js",
publicPath: "/",
},
resolve: {
extensions: [".js", ".vue", ".json", ".css", ".less"],
alias: {
"@": resolve("src"),
Style: resolve("src/assets/styles"),
},
// 只需在这两位置查找文件 提高打包速度
modules: [resolve("src"), resolve("node_modules")],
},
performance: { hints: false },
module: {
rules: [
{
test: /\.css$/i,
use: [
// 'style-loader',// 与 MiniCssExtractPlugin.loader 冲突
MiniCssExtractPlugin.loader,
"css-loader",
],
exclude: /node_modules/,
},
{
test: /\.less$/i,
use: [
// 'style-loader', // creates style nodes from JS strings
MiniCssExtractPlugin.loader,
"css-loader",
"less-loader",
],
exclude: /node_modules/,
},
{
test: /\.(jpg|svg|png|jepg|gif)$/i,
type: "asset/resource",
exclude: /node_modules/,
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
use: "url-loader",
exclude: /node_modules/,
},
{
test: /\.vue$/,
loader: "vue-loader",
exclude: /node_modules/,
},
{
test: /\.js$/,
exclude: /node_modules/,
include: [resolve("src")], // 只在src文件夹下查找
use: {
loader: "babel-loader",
},
},
],
},
plugins: [
new CleanWebpackPlugin(),
new htmlPlugin(htmlPluginConfig),
new VueLoaderPlugin(),
// 打包压缩浏览
// new BundleAnalyzerPlugin(),
// 剥离CSS文件
new MiniCssExtractPlugin({
filename: "css/[name].[hash].css",
// filename: "css/[name].[chunkhash:8].css",
// chunkFilename: "[id].css"
}),
],
// 不打包 使用cdn
externals: {
vue: "Vue", // vue
"element-ui": "ELEMENT", // 因为element-ui对外暴露的全局变量是ELEMENT
nprogress: "NProgress",
"vue-router": "VueRouter",
vuex: "Vuex",
axios: "axios",
},
}