Skip to content

Commit 52ce581

Browse files
committed
feat: webpack-dev 升级到5.x,提升 dev 性能,优化日志输出
1 parent fad3c95 commit 52ce581

File tree

7 files changed

+1863
-305
lines changed

7 files changed

+1863
-305
lines changed

docs/guide/upgrade3.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22

33
## 版本 3.x 的 break
44

5-
1. 编译时的 [base](../reference/config/index.md/#base) 配置,移到了 [router.base](../reference/config/index.md/#router) 下。
6-
2. [webpack-dev-server](https://github.com/webpack/webpack-dev-server)`v3.x` 升级到了 `v4.x`,如果遇到配置不兼容,可以查看[webpack-dev-server 3.x 升级 4.x](https://github.com/webpack/webpack-dev-server/blob/master/migration-v4.md)
7-
3. [layout 插件](../reference/plugin/plugins/layout.md#_4-x-升级到-5-x) 有一些属性变更
8-
3. [request 插件](../reference/plugin/plugins/request.md#_2-x-升级到-3-x) 有一些参数变更,升级请使用最新版本
5+
1. 环境要求 `node >= v18.12.0`
6+
2. 编译时的 [base](../reference/config/index.md/#base) 配置,移到了 [router.base](../reference/config/index.md/#router)
7+
3. [webpack-dev-server](https://github.com/webpack/webpack-dev-server)`v3.x` 升级到了 `v5.x`,如果遇到配置不兼容,可以查看[webpack-dev-server 3.x 升级 4.x](https://github.com/webpack/webpack-dev-server/blob/master/migration-v4.md)[webpack-dev-server 4.x 升级 5.x](https://github.com/webpack/webpack-dev-server/blob/master/migration-v5.md)
8+
4. [layout 插件](../reference/plugin/plugins/layout.md#_4-x-升级到-5-x) 有一些属性变更
9+
5. [request 插件](../reference/plugin/plugins/request.md#_2-x-升级到-3-x) 有一些参数变更,升级请使用最新版本
910

1011
## 继续使用 Webpack
1112

12-
1. 添加 Webpack 构建依赖包: `npm i @fesjs/builder-webpack -D`
13+
1. 添加 Webpack 构建依赖包: `npm i @fesjs/builder-webpack -D`
1314
2. `dev``publicPath` 配置不能为 `./`,请更改为 `auto`
14-
3. 将 html 模版文件从 `public/index.html` 挪到项目根目录,移除 [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) 相关配置,具体模版变量使用请查看[HTML 模版](../guide/template.html)
15+
3. 将 html 模版文件从 `public/index.html` 挪到项目根目录,移除 [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) 相关配置,具体模版变量使用请查看[HTML 模版](../guide/template.html)
1516

1617
## 换成 Vite
1718

18-
1. 安装依赖包 `npm i @fesjs/builder-vite`
19-
2. 将 Webpack 相关的配置换成 Vite,具体可查看[配置](../reference/config/index.md)
20-
3. 将 html 模版文件从 `public/index.html` 挪到项目根目录,如果有相应的 [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) 配置,需要改成 [vite-plugin-html](https://github.com/vbenjs/vite-plugin-html) 的写法
21-
4.`require` 等 Vite 不支持的代码,改写成 Vite 支持的方式
19+
1. 安装依赖包 `npm i @fesjs/builder-vite`
20+
2. 将 Webpack 相关的配置换成 Vite,具体可查看[配置](../reference/config/index.md)
21+
3. 将 html 模版文件从 `public/index.html` 挪到项目根目录,如果有相应的 [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) 配置,需要改成 [vite-plugin-html](https://github.com/vbenjs/vite-plugin-html) 的写法
22+
4.`require` 等 Vite 不支持的代码,改写成 Vite 支持的方式
2223

2324
## 插件
2425

packages/fes-builder-webpack/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"webpack": "^5.90.3",
6767
"webpack-5-chain": "^8.0.1",
6868
"webpack-bundle-analyzer": "^4.4.0",
69-
"webpack-dev-server": "^4.15.1",
70-
"webpackbar": "^5.0.2"
69+
"webpack-dev-server": "^5.1.0",
70+
"webpackbar": "^7.0.0"
7171
}
7272
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import { extname } from 'path';
1+
import { extname } from 'node:path';
22
import historyFallback from 'connect-history-api-fallback';
33

44
const ASSET_EXT_NAMES = ['.ico', '.png', '.jpg', '.jpeg', '.gif', '.svg'];
55

6-
const proxyMiddleware = (api) => (req, res, next) => {
7-
const proxyConfig = api.config.proxy;
8-
if (proxyConfig && Object.keys(proxyConfig).some((path) => req.url.startsWith(path))) {
9-
return next();
10-
}
11-
if (ASSET_EXT_NAMES.includes(extname(req.url))) {
12-
return next();
13-
}
6+
function proxyMiddleware(api) {
7+
return (req, res, next) => {
8+
const proxyConfig = api.config.proxy;
9+
if (proxyConfig && Object.keys(proxyConfig).some(path => req.url.startsWith(path))) {
10+
return next();
11+
}
12+
if (ASSET_EXT_NAMES.includes(extname(req.url))) {
13+
return next();
14+
}
1415

15-
const history = historyFallback();
16-
history(req, res, next);
17-
};
16+
const history = historyFallback();
17+
history(req, res, next);
18+
};
19+
}
1820

1921
export default proxyMiddleware;

packages/fes-builder-webpack/src/plugins/commands/dev/devServer.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ import { chalk } from '@fesjs/utils';
22
import webpack from 'webpack';
33
import WebpackDevServer from 'webpack-dev-server';
44

5+
function formatProxy(proxy) {
6+
if (!proxy) {
7+
return [];
8+
}
9+
10+
if (Array.isArray(proxy)) {
11+
return proxy;
12+
}
13+
14+
return Object.keys(proxy).map((apiPath) => {
15+
return {
16+
context: [apiPath],
17+
...proxy[apiPath],
18+
};
19+
});
20+
}
21+
522
export function startDevServer({ webpackConfig, host, port, proxy, https, beforeMiddlewares, afterMiddlewares, customerDevServerConfig }) {
623
const options = {
724
hot: true,
@@ -10,6 +27,7 @@ export function startDevServer({ webpackConfig, host, port, proxy, https, before
1027
client: {
1128
logging: 'error',
1229
overlay: false,
30+
progress: true,
1331
webSocketURL: {
1432
hostname: host,
1533
port,
@@ -27,7 +45,7 @@ export function startDevServer({ webpackConfig, host, port, proxy, https, before
2745
...(customerDevServerConfig || {}),
2846
port,
2947
host,
30-
proxy,
48+
proxy: formatProxy(proxy),
3149
};
3250
const compiler = webpack(webpackConfig);
3351
const server = new WebpackDevServer(options, compiler);

packages/fes-builder-webpack/src/plugins/commands/dev/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import path from 'path';
2-
import fs from 'fs';
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import process from 'node:process';
34
import { cleanTmpPathExceptCache, getBundleAndConfigs } from '../../common/buildDevUtils';
45
import connectHistoryMiddleware from './connectHistoryMiddleware';
56

packages/fes-builder-webpack/src/plugins/common/webpackConfig/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { join } from 'node:path';
21
import { existsSync } from 'node:fs';
3-
import Config from 'webpack-5-chain';
2+
import { join } from 'node:path';
43
import webpack from 'webpack';
4+
import Config from 'webpack-5-chain';
55
import createCssWebpackConfig from './css';
6-
import getBabelOpts from './getBabelOpts';
7-
import createVueWebpackConfig from './vue';
86
import createDefineWebpackConfig from './define';
9-
import createMinimizerWebpackConfig from './minimizer';
7+
import getBabelOpts from './getBabelOpts';
108
import createHtmlWebpackConfig from './html';
9+
import createMinimizerWebpackConfig from './minimizer';
10+
import createVueWebpackConfig from './vue';
1111

1212
const DEFAULT_EXCLUDE_NODE_MODULES = [
1313
'vue',
@@ -124,12 +124,14 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
124124
webpackConfig.module
125125
.rule('esm')
126126
.test(/\.m?jsx?$/)
127-
.resolve.set('fullySpecified', false);
127+
.resolve
128+
.set('fullySpecified', false);
128129

129130
webpackConfig.module
130131
.rule('js')
131132
.test(/\.(js|mjs|jsx|ts|tsx)$/)
132-
.exclude.add((filepath) => {
133+
.exclude
134+
.add((filepath) => {
133135
// always transpile js in vue files
134136
if (/(\.vue|\.jsx)$/.test(filepath)) { return false; }
135137

@@ -147,9 +149,11 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
147149
webpackConfig.module
148150
.rule('js-in-node_modules')
149151
.test(/\.(js|mjs)$/)
150-
.include.add(/node_modules/)
152+
.include
153+
.add(/node_modules/)
151154
.end()
152-
.exclude.add((filepath) => {
155+
.exclude
156+
.add((filepath) => {
153157
if (transpileDepRegex && transpileDepRegex.test(filepath)) { return true; }
154158

155159
return false;

0 commit comments

Comments
 (0)