Loading...
} + {posts &&{post.title}
+Loading...
} + {posts &&{post.title}
+- 编者按:Change the code, don't Workaround! Webpack - 慢就去改他,优化到位后,Bundle 也可以很快。此方案会在 Umi 4 - 中默认开启,适用于既要 Webpack 功能与生态,又想要 Vite 速度的同学们。 + Editor's Note: Change the code, don't Workaround! If Webpack is slow, fix it. With proper optimization, even Bundle can be fast. This approach will be enabled by default in Umi 4, suitable for those who want both Webpack functionality and ecosystem and Vite speed.
-Umi 4 中同时支持 webpack 和 vite 两种构建方式,跑通了后,迫不及待对比了 Vite 和 Webpack + MFSU 的效果,结果有点意外。关于什么是 MFSU,我在[《SEE Conf: Umi 4 设计思路文字稿》](https://mp.weixin.qq.com/s?__biz=MjM5NDgyODI4MQ%3D%3D&mid=2247484533&idx=1&sn=9b15a67b88ebc95476fce1798eb49146)中有一段详细介绍。 +Umi 4 supports both Webpack and Vite as two different build options. After setting them up and getting them running, there was an eagerness to compare the performance of Vite with that of Webpack + MFSU. The results were a bit surprising. You can find a detailed introduction to what MFSU is in my article [《SEE Conf: Umi 4 Design Concepts》](https://mp.weixin.qq.com/s?__biz=MjM5NDgyODI4MQ%3D%3D&mid=2247484533&idx=1&sn=9b15a67b88ebc95476fce1798eb49146).- 两个示例、四种模式、四个维度的对比。 + Two Examples, Four Modes, Four Dimensions of Comparison. - 两个示例分别是大型的全量 ant-design-pro 和小型的 libs example;四种模式分别是 webpack、webpack - + MFSU、webpack + MFSU with esbuild mode、Vite in umi;四个维度分别是无缓存的冷启动、有缓存的热启动、修改代码后的热更新、页面打开速度。 + The two examples are a large-scale full ant-design-pro project and a small libs example. The four modes are Webpack, Webpack + MFSU, Webpack + MFSU with esbuild mode, and Vite in Umi. The four dimensions of comparison are cold start without cache, hot start with cache, hot update after code change, and page opening speed.
-多说几点和统计相关的。上述 webpack 相关模式全部开启物理缓存;Vite 是 Umi 中集成后的 Vite,也有担心是不是 Umi 对于 Vite 的误用,经开发者确认,基本排除误用的可能性,大段时间消耗在预编译依赖上;Ant Design Pro 中包含 less 的使用,这是使用 esbuild 无法加速的部分,这有影响,但对于不同模式应该是公平的;下图数据是本地用 13-inch M1 2022 重启电脑后跑 5 次后平均取值的结果;Vite 的热更速度没统计是因为由于 esm 的特性,改完后要等请求过来后处理完才算结束,无法统计,但肯定是很快的。 +Let's discuss a few points related to statistics. All the mentioned Webpack-related modes have physical caching enabled. The integrated Vite in Umi is based on Vite but with concerns about possible misuse by Umi, which has been largely ruled out by developers. A significant amount of time was spent on pre-compiling dependencies. The use of LESS in Ant Design Pro affects the acceleration achieved by esbuild, but this should be fair across different modes. The data in the following graphs is the average of five runs after restarting the computer using a 13-inch M1 2022 model. The hot update speed of Vite was not measured due to the nature of ES modules; it only concludes when the request is handled, making it very fast. -直接上结果。有兴趣手动验证的同学可到 umijs/umi 仓库的不同 example 目录下执行 npm run dev 验证。 +Let's directly dive into the results. Those who are interested in manual verification can navigate to different example directories in the umijs/umi repository and run `npm run dev` to verify. ![](https://img.alicdn.com/imgextra/i4/O1CN01Gz9AA81szqy3BbRfK_!!6000000005838-2-tps-2150-1084.png)- 图:全量 ant-design-pro 速度对比图 + Figure: Speed comparison graph for the full ant-design-pro project
![](https://img.alicdn.com/imgextra/i1/O1CN01HNfH7l23L3SRjJUka_!!6000000007238-2-tps-2058-1078.png)- 图:libs example 速度对比图 + Figure: Speed comparison graph for the libs example
-可以看到,在这几个场景下,MFSU with esbuild 数据领先。四个模式的页面打开速度差不多,所以对比数据没在图中列出,这也是让我意外的点,原以为 Vite 请求多会让页面打开速度变慢,也有可能项目还不够复杂? +As can be seen, in these scenarios, MFSU with esbuild data is leading. The page opening speed of all four modes is nearly the same, so the comparison data is not listed in the graphs. This was also an unexpected finding, as I initially thought that more requests in Vite might slow down the page opening speed. It's also possible that the project might not be complex enough? \ No newline at end of file diff --git a/docs/blog/mfsu-faster-than-vite.zh-CN.mdx b/docs/blog/mfsu-faster-than-vite.zh-CN.mdx new file mode 100644 index 000000000000..64cabcfd628e --- /dev/null +++ b/docs/blog/mfsu-faster-than-vite.zh-CN.mdx @@ -0,0 +1,35 @@ +# 比 Vite 还快的 MFSU + ++ 编者按:Change the code, don't Workaround! Webpack + 慢就去改他,优化到位后,Bundle 也可以很快。此方案会在 Umi 4 + 中默认开启,适用于既要 Webpack 功能与生态,又想要 Vite 速度的同学们。 +
+ +Umi 4 中同时支持 webpack 和 vite 两种构建方式,跑通了后,迫不及待对比了 Vite 和 Webpack + MFSU 的效果,结果有点意外。关于什么是 MFSU,我在[《SEE Conf: Umi 4 设计思路文字稿》](https://mp.weixin.qq.com/s?__biz=MjM5NDgyODI4MQ%3D%3D&mid=2247484533&idx=1&sn=9b15a67b88ebc95476fce1798eb49146)中有一段详细介绍。 + ++ + 两个示例、四种模式、四个维度的对比。 + + 两个示例分别是大型的全量 ant-design-pro 和小型的 libs example;四种模式分别是 webpack、webpack + + MFSU、webpack + MFSU with esbuild mode、Vite in umi;四个维度分别是无缓存的冷启动、有缓存的热启动、修改代码后的热更新、页面打开速度。 +
+ +多说几点和统计相关的。上述 webpack 相关模式全部开启物理缓存;Vite 是 Umi 中集成后的 Vite,也有担心是不是 Umi 对于 Vite 的误用,经开发者确认,基本排除误用的可能性,大段时间消耗在预编译依赖上;Ant Design Pro 中包含 less 的使用,这是使用 esbuild 无法加速的部分,这有影响,但对于不同模式应该是公平的;下图数据是本地用 13-inch M1 2022 重启电脑后跑 5 次后平均取值的结果;Vite 的热更速度没统计是因为由于 esm 的特性,改完后要等请求过来后处理完才算结束,无法统计,但肯定是很快的。 + +直接上结果。有兴趣手动验证的同学可到 umijs/umi 仓库的不同 example 目录下执行 npm run dev 验证。 + +![](https://img.alicdn.com/imgextra/i4/O1CN01Gz9AA81szqy3BbRfK_!!6000000005838-2-tps-2150-1084.png) + ++ 图:全量 ant-design-pro 速度对比图 +
+ +![](https://img.alicdn.com/imgextra/i1/O1CN01HNfH7l23L3SRjJUka_!!6000000007238-2-tps-2058-1078.png) + ++ 图:libs example 速度对比图 +
+ +可以看到,在这几个场景下,MFSU with esbuild 数据领先。四个模式的页面打开速度差不多,所以对比数据没在图中列出,这也是让我意外的点,原以为 Vite 请求多会让页面打开速度变慢,也有可能项目还不够复杂? diff --git a/docs/blog/mfsu-independent-usage.md b/docs/blog/mfsu-independent-usage.md index ff9902a91750..6022c1ad4af9 100644 --- a/docs/blog/mfsu-independent-usage.md +++ b/docs/blog/mfsu-independent-usage.md @@ -1,32 +1,32 @@ import { Message } from 'umi'; -# 独立使用 MFSU +# Using MFSU Independently -`MFSU` 支持独立在非 umijs 项目中使用,本文将会介绍如何将 `MFSU` 接入你的 webpack 项目。 +`MFSU` can be used independently in non-UmiJS projects. This article will guide you through the process of integrating `MFSU` into your webpack project. -## 示例项目 +## Sample Projects -如何接入 MFSU ?提供以下几个 示例项目 配置供参考: +To demonstrate MFSU integration, here are a few example projects you can refer to: -Webpack 配置示例:examples/mfsu-independent +Webpack Configuration Example: [examples/mfsu-independent](https://github.com/umijs/umi/tree/master/examples/mfsu-independent) -CRA v5 配置示例:cra-v5-with-mfsu-example +CRA v5 Configuration Example: [cra-v5-with-mfsu-example](https://github.com/umijs/cra-v5-with-mfsu-example) -## 安装 +## Installation -首先安装 `mfsu` 的依赖: +First, install the dependencies for `mfsu`: ```bash - pnpm add -D @umijs/mfsu +pnpm add -D @umijs/mfsu ``` -## 配置 MFSU +## Configuring MFSU -配置 MFSU 一共需要简单的四步操作,请确保以下所有行为都只在开发环境生效。 +Configuring MFSU involves four simple steps. Make sure these steps are applied only in the development environment. -### 1. 初始化实例 +### 1. Initialize an Instance -第一步,初始化一个 `MFSU` 实例,这是 `MFSU` 的基础: +In the first step, initialize an `MFSU` instance, which serves as the foundation for `MFSU`: ```js // webpack.config.js @@ -41,9 +41,9 @@ const mfsu = new MFSU({ }); ``` -### 2. 添加中间件 +### 2. Add Middleware -第二步,添加 `MFSU` 的 `devServer` 中间件到 webpack-dev-server 中,他将为你提供 `MFSU` 所需打包后的资源: +In the second step, add the `MFSU` `devServer` middleware to the webpack-dev-server. This middleware provides the required packaged resources for `MFSU`: #### webpack 5 @@ -80,15 +80,15 @@ module.exports = { } ``` -### 3. 配置转换器 +### 3. Configure Transformers -第三步,你需要配置一种源码转换器,他的作用是用来收集、转换依赖导入路径,替换为 `MFSU` 的模块联邦地址(中间件所提供的)。 +The third step involves configuring a source code transformer. Its purpose is to collect and transform dependency import paths, replacing them with `MFSU`'s module federation addresses provided by the middleware. -此处提供两种方案:`babel plugins` 或 `esbuild handler` ,一般情况下选择 `babel plugins` 即可。 +Two options are provided here: `babel plugins` or `esbuild handler`. Generally, `babel plugins` is the preferred choice. #### Babel Plugins -向 `babel-loader` 添加 `MFSU` 的 `babel plugins` 即可。 +Simply add `MFSU`'s `babel plugins` to the `babel-loader`: ```js // webpack.config.js @@ -117,10 +117,10 @@ module.exports = { #### Esbuild handler -另一种方案是使用内置提供的 `esbuild-loader` 来处理 `js/ts` 资源,**仅用于开发环境** 。 +An alternative is using the built-in `esbuild-loader` to handle `js/ts` resources. Note that this is only for the development environment.- 新官网和文档。 - 下图是新官网的首页,包括重新梳理的文档、信息结构、以及新写的文档插件。目前包含基础的配置、API、升级和快速上手等基础文档,剩余文档还在紧张编写中。有个变化是之前插件的文档集成到 - Umi 官网中,成为 Umi Max 的一部分,之后无需跳转。 + New Official Website and Documentation. + The image below shows the homepage of the new official website, including restructured documentation, information architecture, and newly written documentation plugins. Currently, it includes basic configurations, APIs, upgrades, quick start guides, and other foundational documents. The remaining documentation is still being written diligently. One change is that the documentation for plugins has been integrated into the Umi official website as part of Umi Max, eliminating the need for redirection.
![](https://img.alicdn.com/imgextra/i1/O1CN014dDq4L1Zc3guRwcse_!!6000000003214-2-tps-1600-941.png)- MFSU V3 & 默认开启。 - MFSU 更新了他的第三个大版本,如果你有用 Umi 3 内置的 MFSU 并遇到问题,建议重新尝试,这个版本有很多改进,解决基本所有之前可能会遇到的诡异问题,并且编译速度和页面打开速度都更快。昨天我还有写一篇 - [《比 Vite 更快的 MFSU》](https://mp.weixin.qq.com/s?__biz=MjM5NDgyODI4MQ==&mid=2247484624&idx=1&sn=2addfa8cc2511fbea91faf831195788f)。基于此,我们自信地把这个功能在 - Umi 4 中默认开启。还有值得一提的是,MFSU 可脱离 Umi 运行。 + MFSU V3 & Enabled by Default. + MFSU has released its third major version. If you encountered issues with the built-in MFSU in Umi 3, it is recommended to give it a try again. This version comes with many improvements, addressing almost all the quirky issues that you might have encountered previously. It also provides faster compilation and page loading speeds. Just yesterday, I even wrote an article about + ["MFSU Faster than Vite"](https://mp.weixin.qq.com/s?__biz=MjM5NDgyODI4MQ==&mid=2247484624&idx=1&sn=2addfa8cc2511fbea91faf831195788f). Based on this, we are confident to enable this feature by default in Umi 4. Additionally, it's worth mentioning that MFSU can run independently of Umi.
![](https://img.alicdn.com/imgextra/i2/O1CN01Znj8HD1mCwz72voXv_!!6000000004919-2-tps-1600-807.png)- 双构建引擎和 ESMi。 - Umi 4 提供 Vite 和 Webpack 两种构建模式供开发者选择,并尽可能保证他们之间功能的一致性,可能有些同学会喜欢 - dev 用 vite,build 用 webpack 这样的组合。同时基于 Vite 模式实现了 ESMi 的 Client - 端,ESMi 依赖服务端,在外网还无法使用。 + Dual Build Engines and ESMi. + Umi 4 offers two build modes for developers to choose from: Vite and Webpack. We strive to ensure their functionality consistency as much as possible. Some developers might prefer using Vite for development and Webpack for building. Moreover, based on the Vite mode, we've implemented ESMi on the client side. ESMi requires server-side support and is not yet available on the public internet.
- Webpack 5。Umi 4 - 默认使用 webpack 5 并开启物理缓存。 + Webpack 5. Umi 4 + defaults to using Webpack 5 and enables physical caching.
- React Router 6 & 新路由。 + React Router 6 & New Routing System. - Umi 4 的路由基于 React Router 6 实现,个人非常喜欢这个版本,因为 Remix 的原因,React - Router 6 从设计上考虑了配置式路由的场景,让我得以删除大量 Umi 3 中关于路由渲染的代码。同时基于此,设计了新的路由结构,方便扩展和在未来处理路由的约定式请求。 + Umi 4's routing is built on React Router 6, a version I personally favor due to its consideration of configuration-based routing scenarios, which aligns with Remix's principles. This version allowed for a significant reduction in routing-related code compared to Umi 3. Furthermore, based on this, a new routing structure has been designed to facilitate expansion and handling of conventional routing requests in the future.
- 最佳实践迭代。 - 针对之前 umijs/plugins 仓库中的插件进行了重写、升级,并整合到主仓库。这么做是为了更好的顶层设计,让官方插件之间的风格更一致。 + Improved Best Practices. + We have rewritten, upgraded, and integrated plugins from the umijs/plugins repository into the main repository. This step was taken to improve top-level design and ensure greater consistency in style among official plugins.
- 依赖预打包。 - 由于服务企业内部,安全和稳定是其中很重要点,加上最近 colors 和 faker.js 闹得社区沸沸扬扬,谁都不希望睡一觉醒来,自己负责的业务挂了,还背个故障。Umi - 4 接着 Umi 3 继续做依赖预打包的事,并且更彻底,不仅是 node 侧的依赖,部分运行时的依赖也会做锁定,比如 - core-js 和 @babel/runtime。 + Dependency Pre-bundling. + For enterprises, security and stability are crucial. With the recent buzz around colors and faker.js in the community, no one wants to wake up and find their responsible business down due to an issue. Following Umi 3, Umi 4 continues to work on pre-bundling dependencies, going even further by locking down not only node-side dependencies but also certain runtime dependencies, such as core-js and @babel/runtime.
![](https://img.alicdn.com/imgextra/i1/O1CN01h44iJg1T09DNuYOlm_!!6000000002319-2-tps-1600-758.png)- Umi Max。Umi Max - 是内部 Bigfish - 框架的对外版本,解我们自己的问题,同时也给社区另一个集中化框架的选择。 + Umi Max. Umi Max + is the external version of our internal Bigfish framework. It addresses our own issues and provides another centralized framework option for the community.
- Low Import 研发模式。 + Low Import Development Mode. - 这是 Umi 4 的试验性功能之一,目前已开发完成,解的问题是让开发者少些或不写 import - 语句。项目中大量的 import 其实都可以通过工程化的方式自动处理。Umi 4 里通过 lowImport:{' '} - {} 开启,然后就可以无 import 直接用路由相关的 Link、useLocation 等,数据流相关的 - connect、useModel,antd 组件 Button、Calendar 等,以及其他更多。 + This is one of Umi 4's experimental features, already completed. It addresses the issue of minimizing or eliminating the need for developers to write import statements. Many imports in projects can be automatically managed through engineering. In Umi 4, by enabling lowImport:{' '} + {}, you can directly use route-related elements like Link and useLocation, data flow-related components like connect and useModel, Ant Design components like Button and Calendar, and more, without the need for imports.
![](https://img.alicdn.com/imgextra/i4/O1CN0142Vcpt25kMZqjmioe_!!6000000007564-2-tps-1600-631.png)- 强约束功能集成。Umi - 4 提供 API 让强约束和代码校验变得非常容易。API 包括 - api.onCheck、api.onCheckConfig、api.onCheckPkgJSON 和 - api.onCheckCode,顾名思义,非常好理解他们分别是干嘛的,可以分别对依赖类、代码类和配置类的内容做校验和卡点,适用于团队。 + Strong Constraint Feature Integration. Umi + 4 provides APIs that make strong constraints and code validation easy. These APIs include + api.onCheck, api.onCheckConfig, api.onCheckPkgJSON, and + api.onCheckCode. As the names suggest, they are used for checking and controlling dependencies, configuration, package.json, tsconfig.json, npmClient, code, and other aspects. These are suitable for team-based development.
- Import All From Umi 迭代。 + Import All From Umi Evolution. - 这是两年前 Umi 3 加的功能,最近发现 Remix、prisma、vitekit 等框架和工具都有类似实现。这种方式有好有坏。好处是通过 - umi 将大量依赖管理起来,用户无需手动安装。坏处是更黑盒,同时有点 Hack。Umi 4 不能解其黑盒问题,但解了 - Hack 问题,让实现无副作用,可以和 Vite、MFSU 等方案无缝结合。 + This was a feature introduced in Umi 3 two years ago. Recently, similar implementations have been seen in frameworks and tools like Remix, prisma, and vitekit. This approach has its pros and cons. The advantage is that Umi manages a large number of dependencies, reducing the need for manual installation. The downside is that it's more of a black box and somewhat hacky. Umi 4 cannot eliminate the black box issue, but it has addressed the hackiness, making the implementation side-effect-free and seamlessly integratable with solutions like Vite and MFSU.
- srcTranspiler 和 depTranspiler。 + srcTranspiler and depTranspiler. - 提供针对源码编译和依赖编译更多选择。源码编译可选 babel、swc 和 esbuild,目前没有银弹,合适场景做合适的选择。比如 - swc 由于不支持 top level await,和 mfsu 会有些冲突,但他适用于 build,因为有补丁可以兼容到 - es7;比如 esbuild 适用于 dev,因为快。数据方面以 ant-design-pro 项目为例,源码编译用 - esbuild 相比 babel 在 M1 2020 无缓存情况下会快 3s。 + More options are provided for source code and dependency compilation. Options for source code compilation include babel, swc, and esbuild. There's no silver bullet, and the appropriate choice depends on the context. For example, swc conflicts with top-level await and MFSU due to patching issues, but it's suitable for building as it's compatible with es7. On the other hand, esbuild is suitable for development due to its speed. In terms of data, taking the ant-design-pro project as an example, esbuild is around 3 seconds faster than babel for source code compilation on an M1 2020 MacBook without cache.
- jsMinifier 和 cssMinifier。 + jsMinifier and cssMinifier. - js 压缩和 css 压缩 Umi 4 默认都用的 esbuild,因为快。同时也提供更多选择,js 压缩还支持 - swc、terser 和 uglifyJs,css 压缩还支持 cssnano。 + Umi 4 defaults to using esbuild for js and css compression due to its speed. More options are also provided. For js compression, swc, terser, and uglifyJs are supported, while css compression supports cssnano.
- 应用元数据。Umi 4 - 有通过 api.appData - 收集各种项目数据,从配置、路由、package.json、tsconfig.json、npmClient - 到数据流、国际化、antd 用了哪个版本、react 和 react-dom - 的版本等,应有尽有,这对于插件开发者会非常实用,也适用于有统计需求的场景。 + Application Metadata. Umi 4 + collects various project data through api.appData, including configurations, routes, package.json, tsconfig.json, npmClient, data flow, internationalization, versions of antd, react, and react-dom used, and more. This is extremely useful for plugin developers and scenarios requiring statistics.
- 微生成器。没错,就是 - modern.js 的微生成器,这功能从 modern.js - 里学习了不少,名字就不改了。举个例子,比如 prettier - 功能,可能不是每个项目都需要,就比较适用于微生成器,按需启用、添加配置、安装依赖。 + Micro Generator. Yes, it's the micro generator from modern.js, which was inspired by modern.js and retains its name. For example, features like prettier may not be needed in every project, making it suitable for the micro generator. You can enable it as needed, add configurations, and install dependencies.
- 贴心小改进。 - 还有不少贴心小改进,举两个例子。1 是项目中新增 plugin.ts,会默认作为插件添加,方便项目进行一些插件级的扩展;2 - 是调试问题时通常需要修改编译后的代码看看有没有改对,你把 umi.js 下下来存到项目根目录,umi - 会优先使用这份代码。 + Thoughtful Enhancements. + There are many thoughtful enhancements as well. Here are two examples: 1. In projects, adding plugin.ts will automatically be treated as a plugin, making it convenient for project-level extensions; 2. When debugging issues, you often need to modify compiled code to check if you've made the right changes. If you download umi.js and place it in the project's root directory, Umi will prioritize using this code.
-以上是 Umi 4 目前的新功能。 +The above are the new features in Umi 4 so far. -除此之外,还有一些计划在正式版发布之前做的事情。包括 api route、umi server and adapter、route loader、稳定的 lint、更多命令、组件研发 father 4、文档工具 dumi 2 等,会在之后的 RC 版本中与大家见面。 +In addition to these, there are other plans to be completed before the official release. This includes api route, umi server and adapter, route loader, stable lint, more commands, component development with father 4, and documentation tool dumi 2, which will be introduced in subsequent RC versions. -欢迎大家尝鲜 Umi 4,官方文档有准备 ant-design-pro 从 Umi 3 到 4 的升级文档。同时 RC 阶段,还准备了一个手把手升级的微信交流群,欢迎 Umi 4 的先行者们加入,祝大家升级顺利,也提前祝大家新年快乐 🧨,🐯 年吉祥。 +Feel free to give Umi 4 a try! The official documentation includes an upgrade guide from Umi 3 to 4 for ant-design-pro. During the RC phase, we've also prepared a WeChat group for step-by-step upgrades, welcoming pioneers of Umi 4 to join. We wish you a smooth upgrade and an early happy New Year 🧨, auspicious year of the 🐯.
🚀 MFSU V3 & 默认开启
🎭 双构建引擎和 ESMi
🕸 Webpack 5
⛹🏾♂️ React Router 6 & 新路由
🐹 最佳实践迭代
🛡️ 依赖预打包
🤺 Umi Max
🐛 Low Import 研发模式
⚠️ 强约束功能集成
🎈 Import All From Umi 迭代
🍀 srcTranspiler 和 depTranspiler
🌼 jsMinifier 和 cssMinifier
🌸 应用元数据
❄️ 微生成器
🧪 贴心小改进
+
+
+ 新官网和文档。 + 下图是新官网的首页,包括重新梳理的文档、信息结构、以及新写的文档插件。目前包含基础的配置、API、升级和快速上手等基础文档,剩余文档还在紧张编写中。有个变化是之前插件的文档集成到 + Umi 官网中,成为 Umi Max 的一部分,之后无需跳转。 +
+ +![](https://img.alicdn.com/imgextra/i1/O1CN014dDq4L1Zc3guRwcse_!!6000000003214-2-tps-1600-941.png) + ++ MFSU V3 & 默认开启。 + MFSU 更新了他的第三个大版本,如果你有用 Umi 3 内置的 MFSU 并遇到问题,建议重新尝试,这个版本有很多改进,解决基本所有之前可能会遇到的诡异问题,并且编译速度和页面打开速度都更快。昨天我还有写一篇 + [《比 Vite 更快的 MFSU》](https://mp.weixin.qq.com/s?__biz=MjM5NDgyODI4MQ==&mid=2247484624&idx=1&sn=2addfa8cc2511fbea91faf831195788f)。基于此,我们自信地把这个功能在 + Umi 4 中默认开启。还有值得一提的是,MFSU 可脱离 Umi 运行。 +
+ +![](https://img.alicdn.com/imgextra/i2/O1CN01Znj8HD1mCwz72voXv_!!6000000004919-2-tps-1600-807.png) + ++ 双构建引擎和 ESMi。 + Umi 4 提供 Vite 和 Webpack 两种构建模式供开发者选择,并尽可能保证他们之间功能的一致性,可能有些同学会喜欢 + dev 用 vite,build 用 webpack 这样的组合。同时基于 Vite 模式实现了 ESMi 的 Client + 端,ESMi 依赖服务端,在外网还无法使用。 +
+ ++ Webpack 5。Umi 4 + 默认使用 webpack 5 并开启物理缓存。 +
+ ++ + React Router 6 & 新路由。 + + Umi 4 的路由基于 React Router 6 实现,个人非常喜欢这个版本,因为 Remix 的原因,React + Router 6 从设计上考虑了配置式路由的场景,让我得以删除大量 Umi 3 中关于路由渲染的代码。同时基于此,设计了新的路由结构,方便扩展和在未来处理路由的约定式请求。 +
+ ++ 最佳实践迭代。 + 针对之前 umijs/plugins 仓库中的插件进行了重写、升级,并整合到主仓库。这么做是为了更好的顶层设计,让官方插件之间的风格更一致。 +
+ ++ 依赖预打包。 + 由于服务企业内部,安全和稳定是其中很重要点,加上最近 colors 和 faker.js 闹得社区沸沸扬扬,谁都不希望睡一觉醒来,自己负责的业务挂了,还背个故障。Umi + 4 接着 Umi 3 继续做依赖预打包的事,并且更彻底,不仅是 node 侧的依赖,部分运行时的依赖也会做锁定,比如 + core-js 和 @babel/runtime。 +
+ +![](https://img.alicdn.com/imgextra/i1/O1CN01h44iJg1T09DNuYOlm_!!6000000002319-2-tps-1600-758.png) + ++ Umi Max。Umi Max + 是内部 Bigfish + 框架的对外版本,解我们自己的问题,同时也给社区另一个集中化框架的选择。 +
+ ++ + Low Import 研发模式。 + + 这是 Umi 4 的试验性功能之一,目前已开发完成,解的问题是让开发者少些或不写 import + 语句。项目中大量的 import 其实都可以通过工程化的方式自动处理。Umi 4 里通过 lowImport:{' '} + {} 开启,然后就可以无 import 直接用路由相关的 Link、useLocation 等,数据流相关的 + connect、useModel,antd 组件 Button、Calendar 等,以及其他更多。 +
+ +![](https://img.alicdn.com/imgextra/i4/O1CN0142Vcpt25kMZqjmioe_!!6000000007564-2-tps-1600-631.png) + ++ 强约束功能集成。Umi + 4 提供 API 让强约束和代码校验变得非常容易。API 包括 + api.onCheck、api.onCheckConfig、api.onCheckPkgJSON 和 + api.onCheckCode,顾名思义,非常好理解他们分别是干嘛的,可以分别对依赖类、代码类和配置类的内容做校验和卡点,适用于团队。 +
+ ++ + Import All From Umi 迭代。 + + 这是两年前 Umi 3 加的功能,最近发现 Remix、prisma、vitekit 等框架和工具都有类似实现。这种方式有好有坏。好处是通过 + umi 将大量依赖管理起来,用户无需手动安装。坏处是更黑盒,同时有点 Hack。Umi 4 不能解其黑盒问题,但解了 + Hack 问题,让实现无副作用,可以和 Vite、MFSU 等方案无缝结合。 +
+ ++ + srcTranspiler 和 depTranspiler。 + + 提供针对源码编译和依赖编译更多选择。源码编译可选 babel、swc 和 esbuild,目前没有银弹,合适场景做合适的选择。比如 + swc 由于不支持 top level await,和 mfsu 会有些冲突,但他适用于 build,因为有补丁可以兼容到 + es7;比如 esbuild 适用于 dev,因为快。数据方面以 ant-design-pro 项目为例,源码编译用 + esbuild 相比 babel 在 M1 2020 无缓存情况下会快 3s。 +
+ ++ + jsMinifier 和 cssMinifier。 + + js 压缩和 css 压缩 Umi 4 默认都用的 esbuild,因为快。同时也提供更多选择,js 压缩还支持 + swc、terser 和 uglifyJs,css 压缩还支持 cssnano。 +
+ ++ 应用元数据。Umi 4 + 有通过 api.appData + 收集各种项目数据,从配置、路由、package.json、tsconfig.json、npmClient + 到数据流、国际化、antd 用了哪个版本、react 和 react-dom + 的版本等,应有尽有,这对于插件开发者会非常实用,也适用于有统计需求的场景。 +
+ ++ 微生成器。没错,就是 + modern.js 的微生成器,这功能从 modern.js + 里学习了不少,名字就不改了。举个例子,比如 prettier + 功能,可能不是每个项目都需要,就比较适用于微生成器,按需启用、添加配置、安装依赖。 +
+ ++ 贴心小改进。 + 还有不少贴心小改进,举两个例子。1 是项目中新增 plugin.ts,会默认作为插件添加,方便项目进行一些插件级的扩展;2 + 是调试问题时通常需要修改编译后的代码看看有没有改对,你把 umi.js 下下来存到项目根目录,umi + 会优先使用这份代码。 +
+ +以上是 Umi 4 目前的新功能。 + +除此之外,还有一些计划在正式版发布之前做的事情。包括 api route、umi server and adapter、route loader、稳定的 lint、更多命令、组件研发 father 4、文档工具 dumi 2 等,会在之后的 RC 版本中与大家见面。 + +欢迎大家尝鲜 Umi 4,官方文档有准备 ant-design-pro 从 Umi 3 到 4 的升级文档。同时 RC 阶段,还准备了一个手把手升级的微信交流群,欢迎 Umi 4 的先行者们加入,祝大家升级顺利,也提前祝大家新年快乐 🧨,🐯 年吉祥。 + ++ +
diff --git a/docs/blog/webpack-5-prod-cache.md b/docs/blog/webpack-5-prod-cache.md index a9b1c58778f8..256f6da0473b 100644 --- a/docs/blog/webpack-5-prod-cache.md +++ b/docs/blog/webpack-5-prod-cache.md @@ -1,40 +1,40 @@ import { Message } from 'umi'; -# 物理构建缓存 +# Physical Build Caching -在 `umi build` 构建生产环境产物时,Umi 4 默认没有配置 webpack 5 的物理缓存,这是因为 webpack 的物理缓存失效时机问题,需要依赖用户项目的实际情况,所以没有很好的通用解决方案。 +When building production artifacts with `umi build`, Umi 4 does not configure physical caching for webpack 5 by default. This is due to the issue of when the physical cache of webpack becomes invalid, as it depends on the actual circumstances of the user's project. As a result, there is no universal solution. -所以,当你不明确哪些依赖会让项目物理缓存失效时,很容易产生构建缓存不失效,导致产物是旧的问题,极大影响研发效率。 +Therefore, when you are unsure which dependencies might cause the project's physical cache to become invalid, it's easy to end up with build cache that doesn't expire, leading to outdated artifacts and greatly affecting development efficiency. -## 缓存场景 +## Caching Scenarios -当你的项目需要构建缓存时,是有原因的,我们粗略把场景分成两类:普通项目、Monorepo 中的项目。 +There are reasons why your project might need build caching, and we can roughly divide these scenarios into two categories: regular projects and projects within a Monorepo. -### 普通项目 +### Regular Projects -构建比较慢,如何复用上次的物理缓存,做到多次构建提速? +When building is relatively slow, how can you reuse the physical cache from the last build to speed up multiple builds? -#### 首选解决思路 +#### Preferred Approach -此时首选的优化思路应该是:考虑使用其他更快的现代转译器,比如调整 [`srcTranspiler`](../docs/api/config#srctranspiler) 、[`cssMinifier`](../docs/api/config#cssminifier) 。 +In this case, the preferred optimization approach should be to consider using other faster modern transpilers, such as adjusting [`srcTranspiler`](../docs/api/config#srctranspiler) or [`cssMinifier`](../docs/api/config#cssminifier). -#### CI 中的问题 +#### Issues in CI -物理缓存一般存在于 `node_modules/.cache` ,这就意味着如果你在 CI 中构建,构建的基建必须要支持恢复上次的缓存文件,如果构建容器不支持恢复缓存,同样也无法享受好处。 +Physical caching generally resides in `node_modules/.cache`, which means that if you're building in a CI environment, the build infrastructure must support restoring the previous cache files. If the build container doesn't support cache restoration, you won't benefit from this feature either. -#### 选择依据 +#### Criteria for Selection -所以,当你: +So, you should consider enabling physical caching only when: -1. **多次构建**:确实有多次反复构建的需求。 +1. **Multiple Builds**: There is indeed a need for multiple repeated builds. -2. **能恢复缓存**:在本地构建,或在 CI 有手段能恢复上次的物理缓存文件。 +2. **Cache Restoration Possible**: You're building locally or in CI with a way to restore the previous physical cache files. -3. **时间长**:项目构建时间比较长、开启其他转译器仍无法提速(或有强诉求无法切换转译器)。 +3. **Long Build Time**: The project's build time is relatively long, and using other transpilers still doesn't speed up the process (or there's a strong requirement not to switch transpilers). -满足这些条件后,你才应该考虑开启物理缓存。 +Once these conditions are met, you can consider enabling physical caching. -#### 配置方法 +#### Configuration Method ```ts // .umirc.ts @@ -49,14 +49,14 @@ export default defineConfig({ config.cache({ type: 'filesystem', store: 'pack', - // 🟡 假如你的项目在 CI 中构建每次环境变量都不一样,请挑选或者排除 + // 🟡 If your project's environment variables are different in each CI build, please choose or exclude. version: createEnvironmentHash(process.env), buildDependencies: { config: [__filename], tsconfig: [join(__dirname, 'tsconfig.json')], packagejson: [join(__dirname, 'package.json')], umirc: [join(__dirname, '.umirc.ts')], - // 🟡 其他可能会影响项目的配置文件路径,其内容变更会使缓存失效 + // 🟡 Other configuration file paths that might affect the project and whose content changes would invalidate the cache }, }) } @@ -71,30 +71,24 @@ function createEnvironmentHash(env: Record- 社区有非常多小伙伴在和我们一同建设 Umi,如果你有兴趣,欢迎 - 加入我们 。 + There are many community members who are contributing to Umi. If + you're interested, feel free to + join us.
+ 社区有非常多小伙伴在和我们一同建设 Umi,如果你有兴趣,欢迎 + 加入我们 。 +
+- 内置路由、构建、部署、测试、Lint 等,仅需一个 Umi 依赖即可上手开发。 + Built-in routing, building, deployment, testing, linting, and more. Start developing with just a Umi dependency.
- 蚂蚁集团 10000+ 应用的选择。同时也在阿里、字节、腾讯、网易、美团、快手等公司有大量应用。 + Choice of over 10,000 applications within the Ant Group. Also extensively used in companies such as Alibaba, ByteDance, Tencent, NetEase, Meituan, and Kuaishou.
- 内置微前端、数据流、权限、国际化、icons 方案、埋点、antd、请求、CSS 方案、图表等最佳实践。 + Built-in best practices including micro-frontends, data flows, permissions, internationalization, icons solution, analytics, antd, requests, CSS solutions, charts, and more.
- Umi 实现了 web 应用开发的完整生命周期,并使之插件化,包括 Umi 内部功能也是全由插件实现。 + Umi provides a complete lifecycle of web application development and makes it plugin-friendly. Even internal features of Umi are implemented as plugins.
- 基于 React Router 6,类 Remix,支持嵌套、动态、动态可选、预加载、基于路由的请求优化等。 + Built on top of React Router 6, similar to Remix, supporting nesting, dynamic routing, dynamic optional routing, preloading, and route-based request optimization.
- MFSU 解 Webpack 编译慢的问题,通过 esbuild 解压缩、配置、测试的性能问题,还有运行时... + MFSU solves the slow Webpack compilation problem, and esbuild handles performance issues related to compression, configuration, testing, and runtime...
- 提供 Vite 和 Webpack 两种构建模式供开发者选择,并尽可能保证他们之间功能的一致性。 + Provides both Vite and Webpack as build options for developers, aiming to ensure consistency in functionality between the two.
- Umi 针对依赖做了预打包处理,彻底地锁定依赖,定期更新,让框架的每个版本在 10 年后依旧可用。 + Umi pre-bundles dependencies, ensuring dependency locking and regular updates, making each version of the framework usable even after 10 years.
+ 内置路由、构建、部署、测试、Lint 等,仅需一个 Umi 依赖即可上手开发。 +
++ 蚂蚁集团 10000+ 应用的选择。同时也在阿里、字节、腾讯、网易、美团、快手等公司有大量应用。 +
++ 内置微前端、数据流、权限、国际化、icons 方案、埋点、antd、请求、CSS 方案、图表等最佳实践。 +
++ Umi 实现了 web 应用开发的完整生命周期,并使之插件化,包括 Umi 内部功能也是全由插件实现。 +
++ 基于 React Router 6,类 Remix,支持嵌套、动态、动态可选、预加载、基于路由的请求优化等。 +
++ MFSU 解 Webpack 编译慢的问题,通过 esbuild 解压缩、配置、测试的性能问题,还有运行时... +
++ 提供 Vite 和 Webpack 两种构建模式供开发者选择,并尽可能保证他们之间功能的一致性。 +
++ Umi 针对依赖做了预打包处理,彻底地锁定依赖,定期更新,让框架的每个版本在 10 年后依旧可用。 +
+I am blue
+I am red
+张三, what a nice day!
``` -## 切换语言 +## Switching Languages -通过预设的 `
+
{msg}
; +}; +``` + +注意,用于赋值的键值对对象应当作为 `formatMessage()` 方法的第二个参数传递。 + +渲染的结果如下: + +```html + +张三,今天也是美好的一天!
+ + +张三, what a nice day!
+``` + +## 切换语言 + +通过预设的 `