Use /* webpackChunkName: "name" */ comments for readable chunk names in webpack.
Details
Dynamic import() is a function-like syntax (not a function — it is a language feature) that returns a Promise resolving to the module's namespace object. Unlike static import, it can be used anywhere: inside functions, conditionals, loops, and event handlers.
Trade-offs:
Asynchronous — you must handle the loading state (spinner, skeleton, Suspense)
Each dynamic import creates a separate network request (chunk) — too many small chunks can hurt performance
Harder to analyze statically — bundlers may not tree-shake dynamically imported modules as effectively
Module is not available synchronously — cannot be used where a synchronous value is required
When NOT to use:
For modules used on every page load — static import is simpler and avoids the loading delay
When the module is small — the overhead of a separate chunk outweighs the savings
In performance-critical synchronous paths — the async nature adds latency
Read the instructions and examples in this document.
Apply the patterns to your implementation, adapting to your specific context.
Verify your implementation against the details and edge cases listed above.
Harness Integration
Type: knowledge — this skill is a reference document, not a procedural workflow.
No tools or state — consumed as context by other skills and agents.
Success Criteria
The patterns described in this document are applied correctly in the implementation.
Edge cases and anti-patterns listed in this document are avoided.
1---2name: js-dynamic-import3description: JS Dynamic Import4---5# JS Dynamic Import67> Load ES modules on demand with import() to reduce initial bundle size and enable code splitting89## When to Use1011- You want to load code only when it is needed (route-based splitting, feature flags, heavy libraries)12- Reducing the initial JavaScript bundle size for faster page loads13- Loading polyfills or locale data conditionally based on the runtime environment1415## Instructions16171. Use `import('./module.js')` to load a module at runtime — it returns a Promise.182. Use dynamic import for routes, heavy libraries, or features behind flags.193. Combine with `await` in async functions or `.then()` for cleaner syntax.204. Bundlers (webpack, Vite, Rollup) automatically split dynamic imports into separate chunks.2122```javascript23// Route-based code splitting24async function loadPage(route) {25 const module = await import(`./pages/${route}.js`);26 module.render();27}2829// Feature-flag gating30if (user.hasFeature('charts')) {31 const { renderChart } = await import('./charts.js');32 renderChart(data);33}34```35365. Add error handling — `import()` can fail (network error, missing module).376. Use `/* webpackChunkName: "name" */` comments for readable chunk names in webpack.3839## Details4041Dynamic `import()` is a function-like syntax (not a function — it is a language feature) that returns a Promise resolving to the module's namespace object. Unlike static `import`, it can be used anywhere: inside functions, conditionals, loops, and event handlers.4243**Trade-offs:**4445- Asynchronous — you must handle the loading state (spinner, skeleton, Suspense)46- Each dynamic import creates a separate network request (chunk) — too many small chunks can hurt performance47- Harder to analyze statically — bundlers may not tree-shake dynamically imported modules as effectively48- Module is not available synchronously — cannot be used where a synchronous value is required4950**When NOT to use:**5152- For modules used on every page load — static import is simpler and avoids the loading delay53- When the module is small — the overhead of a separate chunk outweighs the savings54- In performance-critical synchronous paths — the async nature adds latency5556## Source5758https://patterns.dev/javascript/dynamic-import5960## Process61621. Read the instructions and examples in this document.632. Apply the patterns to your implementation, adapting to your specific context.643. Verify your implementation against the details and edge cases listed above.6566## Harness Integration6768- **Type:** knowledge — this skill is a reference document, not a procedural workflow.69- **No tools or state** — consumed as context by other skills and agents.7071## Success Criteria7273- The patterns described in this document are applied correctly in the implementation.74- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/js-dynamic-import in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
JS Dynamic Import It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. Capability flags: makes network calls. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Intense-Visions (@intense-visions) published this skill. Their other Agent Skills are listed on their SkillMD profile.