網頁軟體工程讀書會
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • 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
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • 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
    # vite 概念與快速構建 ## 一、vite 是什麼? vite 的原理是甚麼? > Vite 是 Vue.js 的作者所做的一個新的 bundler,這個工具可以提升開發環境的打包速度。 Vite 是法文的「快」,念法「veet」,與原本的打包工具相比速度快了 10-20 倍。由於 ES6 後多數瀏覽器可以用 native ESM 使用 import 和 export,Vite 的 dev server 藉著改用原生 ES module 的方式而非過去 webpack 走訪整個專案中的模塊一一打包成 require 以提供提供了較快速順暢的開發體驗。 --- ## 二、為什麼 vite 可以節省時間? ### 1. 真正的按需加載:利用瀏覽器對 ES Module 的支持,實現真正的按需加載。 ### 2. 快速的冷啟動:No Bundle + ESBuild 預構建。 - **CommonJS 和 UMD 兼容性** - 由於 Vite 是基於瀏覽器原生支持 ES Module 的能力去實現的 - 但是在開發階段中,Vite 將所有代碼視為原生 ES Module 模塊 - 因此 Vite 必須提前將 CommonJS 和 UMD 發布的依賴項轉換為 ES Module - 並緩存入 node_modules/.vite - **減少模塊和請求的數量** - Vite 將有許多內部模塊的 ESM 依賴關係轉換為單個模塊,以此來提高頁面的加載性能 - 一些包將他們的 ES Module 構建作為許多獨立的文件相互導入,比如 lodash-es 有超過 600 個內置模塊,當解析 import { debounce } from ‘lodash-es’ 時,瀏覽器同時發出 600 多個 HTTP 請求!這些請求會造成網絡擁堵,影響頁面的加載。因此 Vite 通過預構建將 lodash-es 構建為一個模塊,所以只需要一個 HTTP 請求即可。 ### 3. 即時的模塊熱更新:基於 ES Module 的 HMR,同時利用瀏覽器緩存策略提升速度。 熱更新簡單來說就是在 Server 與瀏覽器建立了一個 websocket 連線,當程式碼被修改時,Server 會傳送訊息通知瀏覽器去請求修改模組的程式碼,能在不刷新瀏覽器的前提下進行更新。 在 HMR(熱更新)方面,vite 透過 webSocket/Chokidar/esbuild 即時編譯修改的程式碼,並透過暫存機制加載來更新後的內容。當改動了一個模塊後,vite 僅需讓瀏覽器重新請求該模塊即可,不像 webpack 那樣需要把該模塊的相關依賴模塊全部編譯一次,效率更高。 --- ## 四、vite 與 webpack 差別在哪? 之前多數瀏覽器只支持 CommonJS 所以要打包轉換,webpack,主要是通過 抓取-編譯-構建 整個應用的代碼,之後生成一份編譯,優化後能良好地兼容各個瀏覽器的代碼。 由於 ES6 後多數瀏覽器可以用 native ESM 使用 import 和 export,Vite 就是基於瀏覽器原生支持 ES Module ,根據 Http 的 request 來載入模組進行處理,實現真正的依需求加載,提升開發速度。 > webpack 或者類似的工具叫做「bundler」就是要把你的 JS 檔案跟 dependencies 打包在一起 1. 儘管你在寫程式的時候是用 import 跟 export,但是在輸出時很有可能已經被 babel 或者是 webpack 轉成 CommonJS 或是其他形式外面還有再包一層支持瀏覽器來解析 require 這一些語法。 2. 而這也是 webpack 這些打包工具之所以慢的原因,那就是他們需要靜態分析過 app 的所有檔案以及套件的相依性,然後根據這些資訊把東西包在一起,當你的檔案愈來愈大的時候,花的時間也就自然愈來愈多,因為 webpack 要搞清楚到底要怎麼打包。 > 不過 vite 在 production build 時還是會做打包 vite 打包時不是用 webapck 是用 rollup 這個工具,之所以在正式環境還是需要像 wepack 一樣打包是因為瀏覽器就必須要發出一個個的 request 來取得各個 js 檔,這樣才知道下一個相依性在哪,才能再取得下一個,而在正式環境這樣的加載需要的時間較長,所以要在一開始走訪所有的檔案來取得相依性,這雖使得啟動的時間會加長但能解決套件相依性拉長加載時間的問題。 --- ## 五、vite 使用上有什麼需要注意的問題嗎? - 社群比 webpack 小,套件跟技術討論可能較少 - 打包是用 rollup 不是用 webpack - 可以參考 [亲身体验了一把 vite,感觉真的没有说的那么好,很多坑](https://segmentfault.com/a/1190000041057203) 但很多問題其實我感覺並沒有影響那麼大也可以看文章下面的回覆。 --- ## 六、vite 快速安裝 ``` npm init vite@latest 再來請輸入 **專案名稱** 及 **選擇你想使用的框架**。 cd 你的專案名稱 npm install npm run dev ``` --- ## 七. vite 專案架構 ``` ├── README.md ├── index.html //放置於根目錄 ├── node_modules ├── package.json ├── public ├── src │   ├── App.vue │   ├── assets │   ├── components │   └── main.js ├── vite.config.js └── yarn.lock ``` --- ## 八. 延伸討論的問題 - 為什麽 require 不好用? - 沒有 Module 這樣的設計模式,來讓你的變數不會衝突 - 加入其他的 Dependency(相依函示庫),還是只能用 <script></script> 慢慢加 - 上線時 vite 是否是用 require 的方式 - 不是,Rollup 是一個針對 ES6 Module 所設計的打包工具,使用 ES6 Module > 參考資料 > 1. [webpack与 vite 的区别](https://blog.csdn.net/qq_41189996/article/details/115718987) > 2. [Vite原理浅析 - 知乎专栏](https://zhuanlan.zhihu.com/p/489213559)

    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