Skip to content

Commit baddade

Browse files
committed
refactor: webpack 配置重构
1 parent b4c659f commit baddade

File tree

5 files changed

+385
-274
lines changed

5 files changed

+385
-274
lines changed

webpack.common.cjs

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
const fs = require('fs')
2+
const webpack = require('webpack')
3+
const HtmlWebpackPlugin = require('html-webpack-plugin')
4+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
5+
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
6+
const ESLintPlugin = require('eslint-webpack-plugin')
7+
const { resolveApp } = require('./webpack.utils.cjs')
8+
9+
const NODE_ENV = process.env.NODE_ENV
10+
const isProduction = NODE_ENV === 'production'
11+
const IS_ANALYZE = process.env.IS_ANALYZE === 'true'
12+
13+
const getStyleLoader = (preprocessing) => {
14+
const preset = [
15+
isProduction ? MiniCssExtractPlugin.loader : 'style-loader',
16+
// 对于 .module. 的文件会自动处理 CSS modules
17+
{
18+
loader: 'css-loader',
19+
options: {
20+
importLoaders: preprocessing ? 2 : 1
21+
}
22+
},
23+
{
24+
loader: 'postcss-loader',
25+
options: {
26+
postcssOptions: {
27+
plugins: [
28+
[
29+
// 支持转换高级 CSS 特性
30+
// 将读取 browserslist 目标浏览器配置
31+
'postcss-preset-env',
32+
{
33+
// 添加浏览器前缀
34+
autoprefixer: {
35+
flexbox: 'no-2009'
36+
},
37+
stage: 3
38+
}
39+
]
40+
]
41+
}
42+
}
43+
}
44+
]
45+
return [...preset, preprocessing]
46+
}
47+
48+
// corrected-package 需修正的不支持目标浏览器的node_modules包
49+
const correctedPackage = require(resolveApp('package.json'))['corrected-package']
50+
let correctedPackageInclude = []
51+
if (correctedPackage && Array.isArray(correctedPackage)) {
52+
correctedPackageInclude = correctedPackage.map((name) => resolveApp(`node_modules/${name}`))
53+
}
54+
55+
module.exports = ({ PUBLIC_URL, BUILD_PATH, GENERATE_SOURCEMAP, resolveClientEnv }) => {
56+
return {
57+
// 入口文件 这里为单入口
58+
entry: [resolveApp('src/main.tsx')],
59+
// 资源输出配置
60+
output: {
61+
// 输出目录
62+
path: resolveApp(BUILD_PATH),
63+
// 公共基础路径(资源部署时所处的url路径)
64+
publicPath: PUBLIC_URL,
65+
// bundle的文件名
66+
filename: 'static/js/[name].[contenthash:8].js',
67+
// 分包chunk的文件名
68+
chunkFilename: 'static/js/[name].[contenthash:8].js',
69+
// 静态资源输出文件名
70+
assetModuleFilename: 'static/media/[name].[hash][ext]',
71+
// 在生成文件之前清空 output 目录
72+
clean: true
73+
},
74+
mode: NODE_ENV,
75+
// sourcemap 配置
76+
devtool: GENERATE_SOURCEMAP
77+
? 'source-map'
78+
: isProduction
79+
? false
80+
: 'eval-cheap-module-source-map',
81+
module: {
82+
rules: [
83+
{
84+
test: /\.js$/,
85+
use: ['thread-loader', 'babel-loader'],
86+
include: [resolveApp('src'), ...correctedPackageInclude]
87+
},
88+
{
89+
test: /\.tsx?$/,
90+
include: [resolveApp('src')],
91+
use: [
92+
'thread-loader',
93+
'babel-loader',
94+
{
95+
loader: 'ts-loader',
96+
options: {
97+
happyPackMode: true,
98+
transpileOnly: true
99+
}
100+
}
101+
]
102+
},
103+
{
104+
test: /\.css$/,
105+
use: getStyleLoader()
106+
},
107+
{
108+
test: /\.less$/,
109+
use: getStyleLoader('less-loader')
110+
},
111+
{
112+
test: /\.(svg)(\?.*)?$/,
113+
// 资源模块类型
114+
type: 'asset/resource'
115+
},
116+
{
117+
test: /\.(png|jpe?g|gif|webp|avif)(\?.*)?$/,
118+
type: 'asset',
119+
parser: {
120+
// 转换成base64的最大size
121+
dataUrlCondition: {
122+
maxSize: 10000
123+
}
124+
}
125+
},
126+
{
127+
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
128+
type: 'asset'
129+
},
130+
{
131+
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/i,
132+
type: 'asset'
133+
}
134+
]
135+
},
136+
performance: IS_ANALYZE
137+
? false
138+
: {
139+
// warn 500K
140+
maxAssetSize: 500000,
141+
maxEntrypointSize: 500000
142+
},
143+
// 模块解析配置
144+
resolve: {
145+
// 不带文件后缀时,尝试找寻的文件后缀
146+
extensions: ['.js', '.jsx', '.mjs', '.ts', '.tsx', '.mts', '.json'],
147+
// 路径别名
148+
alias: {
149+
'@': resolveApp('src'),
150+
'~': resolveApp('node_modules')
151+
}
152+
},
153+
// 构建文件系统级别的缓存
154+
cache: {
155+
type: 'filesystem',
156+
// 缓存依赖项(依赖项变更可使缓存失效)
157+
buildDependencies: {
158+
defaultWebpack: ['webpack/lib/'],
159+
config: [__filename],
160+
tsconfig: [resolveApp('tsconfig.json')].filter((f) => fs.existsSync(f)),
161+
env: [
162+
resolveApp('.env'),
163+
resolveApp('.env.development'),
164+
resolveApp('.env.production')
165+
].filter((f) => fs.existsSync(f))
166+
}
167+
},
168+
plugins: [
169+
new ESLintPlugin({
170+
extensions: ['js', 'jsx', 'ts', 'tsx']
171+
}),
172+
new ForkTsCheckerWebpackPlugin({
173+
typescript: {
174+
diagnosticOptions: {
175+
semantic: true,
176+
syntactic: true
177+
}
178+
}
179+
}),
180+
new webpack.ProgressPlugin(),
181+
// 编译时可替换的变量
182+
new webpack.DefinePlugin(resolveClientEnv()),
183+
// 输出html文件 并处理好 js css 等资源的引入
184+
new HtmlWebpackPlugin({
185+
template: resolveApp('public/index.html'),
186+
templateParameters: resolveClientEnv(true)
187+
})
188+
],
189+
// bundle 信息只在发生错误时输出
190+
stats: 'errors-warnings'
191+
}
192+
}

0 commit comments

Comments
 (0)