chencheng (云谦)
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
由于维护 [umi](https://github.com/umijs/umi/) 和 [roadhog](https://github.com/sorrycc/roadhog),最近有收到不少关于 webpack 构建慢的咨询,比如 dev 启动慢,热更新慢,构建需要 30 分钟,内存爆掉等等,花了些时间查资料和实践,整理如下,供参考。 dev 和 build 有不同的优化方式。因为 dev 可不考虑加载问题,所以可以提前多编译一些,并且可以有缓存,所以二次编译会快很多;而 build 下主要的问题还是 uglify 的问题。 ## dev ### dll (动态链接库) node_modules 下的模块通常不会变,除非安装了新的依赖或者修改了依赖的版本,dll 是提前构建好一个包含指定模块(通常是所有依赖)并提供一个 manifest 提供模块信息,然后启动 dev 时根据这份 manifest,既有的模块就无需重新构建,通常能减少 30%~50% 的启动时间。 缺点是配置起来比较麻烦,比如: * 需要做两次构建 * 在需要的时候(依赖有修改)重新构建 dll * 在 html 里引入提前构建好的 dll 脚本 如果你使用 [umi](https://github.com/umijs/umi/),那么这些事情通过 [umi-plugin-dll](https://github.com/umijs/umi/tree/master/packages/umi-plugin-dll) 全部自动处理了。 ### [hard-source-webpack-plugin](https://github.com/mzgoddard/hard-source-webpack-plugin) 这是我最近发现的库,简直神器,让我觉得按需编译都没啥必要做了,以 [antd-pro](https://github.com/ant-design/ant-design-pro) 为例,使用此插件之后,二次启动时[仅需 11s](https://gw.alipayobjects.com/zos/rmsportal/exlNLjwvJXUvTXVlPQDK.png) 。 原理是啥?webpack 的递增编译是很快的,为啥?因为有缓存。所以这个插件实际上是把这部分的缓存保存到物理文件,二次启动时从缓存里读取,所以只需要做递增编译即可。 umi 和 roadhog 的最新版本已内置,大家可以感受下。有遇到问题的可以通过环境变量 `HARD_SOURCE=none` 关闭,目前已知有: * [Invalid string length](https://github.com/mzgoddard/hard-source-webpack-plugin/issues/274) * [某些场景下 cpu 暴涨,但我这不能重新](https://github.com/sorrycc/roadhog/issues/710) ### [cache-loader](https://github.com/webpack-contrib/cache-loader) 应该和上面的功能类似,经测试,效果不明显。以 umi-examples/with-dva 为例,不加 cache-loader 12s,加了 11s。 ### [thread-loader](https://github.com/webpack-contrib/thread-loader) CRA 里加在,但我测过发现反而慢了。以 umi-examples/with-dva 为例,不加 thread-loader 12s,加了 13s。 ### 按需编译(不是按需加载) 假设我们有 100 个页面,而 SPA 通常只有一个 entry,所以就算我们只需调试一个页面,也需要把这 100 个页面都编译一遍。这!完全不合理嘛!所以就有了按需编译,最早是在 next.js 里看到,然后我把他应用到 umi 里,实现方式有所不同。 原理其实很简单,根据 url 的访问来判断出需要加载哪些页面(路由),然后把这些路由编译了。 ## build ### uglify 问题 uglify 即代码压缩,有两个问题,1 是慢,2 是内存消耗高,一些大的项目 8G 甚至 16G 内存都不够用。而像 parcel、webpack@4 发布时所说的速度提升,通常都是抛开了 uglify 而言的。 uglify 所需的时间只和依赖文件的大小多少有关,比如依赖了一个图形库,通常就很慢很慢了。 那么怎么办?按我的理解,有两个办法: 1. 减少依赖文件 2. 减少需要压缩的文件 减少依赖文件有不少办法,比如: 1. 优化 common 提取策略,让整体尺寸尽可能少 2. externals 掉一些大的库,降低整体尺寸 3. 利用 webpack 的 tree shake + es module,排除掉一些没有用到的模块 4. ... ### common 提取策略 common 提取即利用 webpack.optimize.CommonsChunkPlugin 提取公共部分,而如何提取需要根据自己的项目需要决定。 我理解 common 提取有两个极端, 1. 被两个 entry/page 依赖即提取为公共,这样 common 会比较大,整体尺寸最小,但加载首个页面时需要下载的尺寸会比较大 2. 被所有 entry/page 依赖才提取为公共,这样 common 比较小,整体尺寸较大 目前 umi 和 next.js 都是采用折中的方案,即被一半 entry/page 依赖才提取为公共。这是通用的解,但不是最优解。具体如何提取,可以视项目需要自行决定。 ### externals externals 是非常有效的一个方案,可以一下子减少大量需要编译、压缩的模块。 比如配置: ``` externals: { react: 'window.React' } ``` 然后在 html 里引入 react.js,这样 react 就不用解析了。 比较极端的是我们把所有模块都 externals 掉,这样只需要编译项目文件(支付宝小程序是这么做的),所以速度就会很快。 但是会带来的问题是: 1. 无法利用 webpack 的 tree-shake 1. 多个库之间如果存在公共模块(比如 lodash),就无法复用 这样子,实际上用户访问时需要下载的文件就会增加。 另外,使用 externals 的一个麻烦的地方就是你需要在 html 里引用额外的 js 文件,这里也有几个潜在的问题: 1. 如果你的 cdn 不支持 combo,并且不是 http/2,那么速度会慢 2. 你需要手动维护一份 cdn 文件列表,并且跟进他们的更新,也是件麻烦的事情 而这些麻烦的事情,umi 会计划帮你做掉,请期待后续 umi 的方案。 ### uglify 的 exclude 参数 ### uglify-webpack-plugin https://github.com/umijs/umi/pull/418 ### [webpack-parallel-uglify-plugin](https://github.com/gdborton/webpack-parallel-uglify-plugin) ## 通用 ### sourcemap ### 依赖分析 ## 其他 * happypack * parcel ## 参考: * https://github.com/webpack/webpack/issues/250#issuecomment-240643985 * https://medium.com/ottofellercom/0-100-in-two-seconds-speed-up-webpack-465de691ed4a * https://blog.box.com/blog/how-we-improved-webpack-build-performance-95/ * https://github.com/jmblog/how-to-optimize-momentjs-with-webpack * https://github.com/webpack/docs/wiki/build-performance

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully