-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.base.js
More file actions
203 lines (201 loc) · 6.66 KB
/
webpack.config.base.js
File metadata and controls
203 lines (201 loc) · 6.66 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const webpack = require("webpack");
const { merge } = require("webpack-merge");
const HtmlWebpackPlugin = require("html-webpack-plugin");
// const CopyWebpackPlugin = require("copy-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCssPlugin = require("optimize-css-assets-webpack-plugin");
const path = require("path");
const baseWebpackConfig = {
entry: "./src/index.js",
output: {
path: path.resolve(__dirname, "dist"), //必须是绝对路径
filename: "[name].[hash:8].js",
publicPath: "/", //通常是CDN地址
chunkFilename: "[id].[chunkhash].js",
},
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
},
{
test: /\.jsx?$/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: [
[
"@babel/plugin-transform-runtime",
{
corejs: 3,
},
],
],
cacheDirectory: true, // Webpack 构建,将会尝试读取缓存,来避免在每次执行时,可能产生的、高性能消耗的 Babel 重新编译过程
},
},
include: [path.resolve(__dirname, "src")],
exclude: /node_modules/,
},
{
test: /\.(le|c)ss$/,
// loader 的执行顺序是从后向前执行的
use: [
// "style-loader", // style-loader 动态创建 style 标签,将 css 插入到 head 中
MiniCssExtractPlugin.loader, // 抽离css,将CSS文件单独打包
"css-loader", // css-loader 负责处理 @import 等语句
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: ["autoprefixer"], // 自动生成浏览器兼容性前缀
},
},
},
"less-loader", // less-loader 负责处理编译 .less 文件,将其转为 css
],
exclude: /node_modules/,
},
{
test: /\.(png|jpg|gif|jpeg|webp|svg|eot|ttf|woff|woff2)$/i,
use: [
{
loader: "url-loader",
options: {
limit: 10240, // 大于10K保留原图,小于10K转base64
esModule: false,
fallback: "file-loader",
outputPath: "assets", // 指定构建目录
name: "[name]_[hash:6].[ext]", // img_f0f46d.jpeg
},
},
],
exclude: /node_modules/,
},
{
test: /.html$/,
use: "html-withimg-loader",
},
],
noParse: /jquery|lodash/, // 不进行转化和解析
},
plugins: [
// 读取 manifest.json 获得动态链接库的 api
// new webpack.DllReferencePlugin({
// manifest: path.resolve(__dirname, "dist", "dll", "manifest.json"),
// }),
//数组 放着所有的webpack插件
new HtmlWebpackPlugin({
template: "./public/index.html",
filename: "index.html", //打包后的文件名
minify: {
removeAttributeQuotes: false, //是否删除属性的双引号
collapseWhitespace: false, //是否折叠空白
},
// hash: true //是否加上hash,默认是 false
}),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ["**/*", "!dll", "!dll/**"], //不删除dll目录
}),
// new CopyWebpackPlugin([
// {
// from: "public/js/*.js",
// to: path.resolve(__dirname, "dist", "js"),
// flatten: true,
// },
// ]),
new webpack.ProvidePlugin({
React: "react",
Component: ["react", "Component"],
Vue: ["vue/dist/vue.esm.js", "default"],
$: "jquery",
_map: ["lodash", "map"],
}),
new MiniCssExtractPlugin({
filename: "css/[name].css",
}),
new OptimizeCssPlugin(), // css压缩
new webpack.HotModuleReplacementPlugin(), // 热更新插件
new webpack.DefinePlugin({
// 定义环境变量
DEV: JSON.stringify("dev"), //字符串
FLAG: "true", //FLAG 是个布尔类型
}),
//忽略 moment 下的 ./locale 目录
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
}),
],
devServer: {
// 自动打开浏览器,或打开多个指定页面
open: false,
// open: ["/my-page", "/another-page"],
proxy: {
"/api": {
target: "http://localhost:4000",
pathRewrite: {
"/api": "",
},
},
},
static: path.join(__dirname, "public"), // boolean | string | array | object, static file location
compress: true, // enable gzip compression
historyApiFallback: true, // true for index.html upon 404, object for multiple paths
hot: true, // hot module replacement. Depends on HotModuleReplacementPlugin
https: false, // true for self-signed, object for cert authority
client: {
logging: "error", // webpack 在浏览器的log level
overlay: true, // 当编译出错时,会在浏览器窗口全屏输出错误
progress: true,
},
},
devtool: "cheap-module-source-map",
resolve: {
modules: ["./src/components", "node_modules"], // 从左到右依次查找,可以通过 import Dialog from 'dialog' 从 /src/components 引入
alias: {
"@src": path.resolve(__dirname, "src"),
},
extensions: [".tsx", ".ts", ".js"], // 在缺省文件后缀时,告诉 webpack 优先访问哪个后缀文件,记住将频率最高的后缀放在第一位,并且控制列表的长度,以减少尝试次数
// enforceExtension: true, // 导入语句不能缺省文件后缀
},
externals: {
// jquery通过script引入之后,全局中即有了 jQuery 变量
// 可以将一些JS文件存储在 CDN 上(减少 Webpack打包出来的 js 体积)
jquery: "jQuery",
},
optimization: {
// runtimeChunk: {
// name: "manifest",
// },
splitChunks: {
//分割代码块
cacheGroups: {
vendor: {
//第三方依赖
priority: 1, //设置优先级,首先抽离第三方模块
name: "vendor",
test: /node_modules/,
chunks: "initial",
minSize: 0,
minChunks: 1, //最少引入了1次
},
//缓存组
common: {
//公共模块
chunks: "initial",
name: "common",
minSize: 100, //大小超过100个字节
minChunks: 3, //最少引入了3次
},
},
},
},
};
module.exports = function mergeBase(config) {
return merge(baseWebpackConfig, config);
};