dynamic-import — Claude Code skill: what it does, how to install
Teaches coding agents on-demand module loading with dynamic import(). It helps cut initial bundle sizes by deferring non-critical components.
npx skills add PatternsDev/skills --skill dynamic-import -a claude-codeWhat dynamic-import does
The skill configures coding agents to apply import() in **/*.js and **/*.ts files. It guides the model to split pickers, modals, or heavy packages into dedicated chunks instead of including them in the initial bundle. For React applications, it enforces React.lazy alongside React.Suspense with an explicit fallback component.
In the Patterns.dev chat reference, deferring EmojiPicker shaved bundle size down from 1.5MiB to 1.33 MiB. The skill clearly flags an SSR limitation: because React Suspense does not support server-side rendering, you must reach for loadable-components instead. Only modules non-essential to the initial render should be split off.
What it helps with
- reduce initial bundle size by splitting off non-critical code
- configure React.lazy and Suspense for interactive UI pickers and modals
- set up loadable-components for dynamic imports in SSR environments
- provide fallback UI states while deferred modules download
Example prompts for the agent
- Refactor EmojiPicker import to use React.lazy and Suspense to lighten the main bundle
- Help me set up dynamic imports in an SSR React app using loadable-components
- Split this modal into a separate chunk using dynamic import()
How to install dynamic-import
1Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill dynamic-import -a claude-code2Manually: put the files in place
~/.claude/skills/dynamic-import/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/dynamic-import/
1Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill dynamic-import -a codex2Manually: put the files in place
.agents/skills/dynamic-import/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill dynamic-import -a cursor2Manually: put the files in place
.cursor/skills/dynamic-import/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install PatternsDev/skills dynamic-import2Manually: put the files in place
.github/skills/dynamic-import/SKILL.md1Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill dynamic-import -a gemini-cli2Platform CLI
gemini skills install https://github.com/PatternsDev/skills --path javascript/dynamic-importManually: put the files in place
.devin/skills/dynamic-import/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add PatternsDev/skills --skill dynamic-import -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about dynamic-import
How to install dynamic-import in Claude Code?
Run npx skills add PatternsDev/skills --skill dynamic-import -a claude-code in your terminal.
What does dynamic-import teach the agent?
It provides instructions for on-demand loading, ensuring the agent uses React.lazy, meaningful fallbacks, and avoids blocking the initial page render.
How does dynamic-import handle SSR projects?
Since React Suspense lacks SSR support, the skill instructs the agent to use the loadable-components library instead.
Source text
SKILL.md · MIT
--- name: dynamic-import description: Teaches dynamic import() for on-demand code loading. Use when you need to reduce initial bundle size by lazily loading modules that aren't required at startup. paths: - "**/*.js" - "**/*.ts" license: MIT metadata: author: patterns.dev version: "1.1" related_skills: - "module-pattern" - "singleton-pattern" --- # Dynamic Import In a chat application, we have four key components: `UserInfo`, `ChatList`, `ChatInput` and `EmojiPicker`. However, only _three_ of these components are used instantly on the initial page load: `UserInfo`, `ChatList` and `ChatInput`. The `EmojiPicker` isn't directly visible, and may not even be rendered at all if the user won't even click on the `Emoji` in order to toggle the `EmojiPicker`. This would mean that we unnecessarily added the `EmojiPicker` module to our initial bundle, which potentially increased the loading time! In order to solve this, we can _dynamically import_ the `EmojiPicker` component. Instead of statically importing it, we'll only import it when we want to show the `EmojiPicker`. An easy way to dynamically import components in React is by using **React Suspense**. The `React.Suspense` component receives the component that should be dynamically loaded, which makes it possible for the `App` component to render its contents faster by suspending the import of the `EmojiPicker` module! ## When to Use - Use this when certain modules are only needed based on user interaction or conditions - This is helpful when you want to reduce the initial bundle size for faster page loads - Use this when components like modals, pickers, or heavy libraries aren't needed on initial render ## Instructions - Use `React.lazy` with `Suspense` for dynamic component imports in React - Provide meaningful fallback UI while dynamically imported modules are loading - Consider using `loadable-components` for SSR applications where React Suspense isn't supported - Only dynamically import modules that aren't critical to the initial render ## Details Instead of unnecessarily adding `EmojiPicker` to the initial bundle, we can split it up into its own bundle and reduce the size of the initial bundle! A smaller initial bundle size means a faster initial load: the user doesn't have to stare at a blank loading screen for as long. The `fallback` component lets the user know that our application hasn't frozen: they simply need to wait a little while for the module to be processed and executed. ``` Asset Size Chunks Chunk Names emoji-picker.bundle.js 1.48 KiB 1 [emitted] emoji-picker main.bundle.js 1.33 MiB main [emitted] main vendors~emoji-picker.bundle.js 171 KiB 2 [emitted] vendors~emoji-picker ``` Whereas previously the initial bundle was `1.5MiB`, we've been able to reduce it to `1.33 MiB` by suspending the import of the `EmojiPicker`! By dynamically importing the `EmojiPicker` component, we managed to reduce the initial bundle size from `1.5MiB` to `1.33 MiB`! Although the user may still have to wait a while until the `EmojiPicker` has been fully loaded, we have improved the user experience by making sure the application is rendered and interactive while the user waits for the component to load. ### Loadable Components Server-side rendering doesn't support React Suspense (yet). A good alternative to React Suspense is the [`loadable-components`](https://loadable-components.com/docs/getting-started/) library, which can be used in SSR applications. Similar to React Suspense, we can pass the lazily imported module to the `loadable`, which will only import the module once the `EmojiPicker` module is being requested! While the module is being loaded, we can render a `fallback` component. Although loadable components are a great alternative to React Suspense for SSR applications, they're also useful in CSR applications in order to suspend the import of modules. ## Source - [patterns.dev/vanilla/dynamic-import](https://patterns.dev/vanilla/dynamic-import)
© patterns.dev · MIT · PatternsDev/skills