Static Import

patternsdev/skills/javascript/static-import

作者 patternsdev48bf58a488cdMIT253 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 個月前更新

Teaches static ES2015 import syntax for module dependencies. Use when you need to import code that is required at load time and benefits from static analysis and tree shaking.

AI 產生的概覽

說明 ES2015 靜態 import 語法,以及何時應優先使用它而非動態匯入模組相依性。

功能
此技能說明 ES2015 靜態 import 語法:使用預設 import 陳述式匯入的模組會被納入初始打包檔案。它透過一個聊天應用程式範例展示靜態匯入的元件,並指出靜態匯入的模組會在引擎執行到匯入那一行時立即執行。它也說明打包大小的取捨,以及何時應改用動態匯入。
適用情境
當你需要匯入在載入時立即需要、且能受益於靜態分析與 tree shaking 的模組時使用。它也適合用來理解預設匯入機制,以便判斷何時應改用動態匯入。
執行需求
不需要任何工具、套件或憑證;僅為說明性指示,不附帶指令碼。

Static Import

The import keyword allows us to import code that has been exported by another module. By default, all modules we're statically importing get added to the initial bundle. A module that is imported by using the default ES2015 import syntax, import module from 'module', is statically imported.

Let's look at an example! A simple chat app contains a Chat component, in which we're statically importing and rendering three components: UserProfile, a ChatList, and a ChatInput to type and send messages! Within the ChatInput module, we're statically importing an EmojiPicker component to be able to show the user the emoji picker when the user toggles the emoji.

When to Use

  • Use this for importing modules that are needed immediately on page load
  • This is the default import mechanism — understand it to know when to switch to dynamic imports

Instructions

  • Use static imports for modules critical to the initial render
  • Be aware that all statically imported modules are bundled into the initial bundle
  • Consider switching to dynamic imports for modules not needed on initial render

Details

The modules get executed as soon as the engine reaches the line on which we import them.

Since the components were statically imported, Webpack bundled the modules into the initial bundle:

bash
Asset           Size      Chunks            Chunk Namesmain.bundle.js  1.5 MiB    main  [emitted]  main

Our chat application's source code gets bundled into one bundle: main.bundle.js. A large bundle size can affect the loading time of our application significantly depending on the user's device and network connection. Before the App component is able to render its contents to the user's screen, it first has to load and parse all modules.

Luckily, there are many ways to speed up the loading time! We don't always have to import all modules at once: maybe there are some modules that should only get rendered based on user interaction, like the EmojiPicker in this case, or rendered further down the page. Instead of importing all component statically, we can dynamically import the modules after the App component has rendered its contents and the user is able to interact with our application.

Source

來源與署名

來源:patternsdev/skills位於javascript/static-import提交48bf58a

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架