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 Import4metadata:5 source: https://skillmd.com/skills/intense-visions/js-dynamic-import6---7# JS Dynamic Import89> Load ES modules on demand with import() to reduce initial bundle size and enable code splitting1011## When to Use1213- You want to load code only when it is needed (route-based splitting, feature flags, heavy libraries)14- Reducing the initial JavaScript bundle size for faster page loads15- Loading polyfills or locale data conditionally based on the runtime environment1617## Instructions18191. Use `import('./module.js')` to load a module at runtime — it returns a Promise.202. Use dynamic import for routes, heavy libraries, or features behind flags.213. Combine with `await` in async functions or `.then()` for cleaner syntax.224. Bundlers (webpack, Vite, Rollup) automatically split dynamic imports into separate chunks.2324```javascript25// Route-based code splitting26async function loadPage(route) {27 const module = await import(`./pages/${route}.js`);28 module.render();29}3031// Feature-flag gating32if (user.hasFeature('charts')) {33 const { renderChart } = await import('./charts.js');34 renderChart(data);35}36```37385. Add error handling — `import()` can fail (network error, missing module).396. Use `/* webpackChunkName: "name" */` comments for readable chunk names in webpack.4041## Details4243Dynamic `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.4445**Trade-offs:**4647- Asynchronous — you must handle the loading state (spinner, skeleton, Suspense)48- Each dynamic import creates a separate network request (chunk) — too many small chunks can hurt performance49- Harder to analyze statically — bundlers may not tree-shake dynamically imported modules as effectively50- Module is not available synchronously — cannot be used where a synchronous value is required5152**When NOT to use:**5354- For modules used on every page load — static import is simpler and avoids the loading delay55- When the module is small — the overhead of a separate chunk outweighs the savings56- In performance-critical synchronous paths — the async nature adds latency5758## Source5960https://patterns.dev/javascript/dynamic-import6162## Process63641. Read the instructions and examples in this document.652. Apply the patterns to your implementation, adapting to your specific context.663. Verify your implementation against the details and edge cases listed above.6768## Harness Integration6970- **Type:** knowledge — this skill is a reference document, not a procedural workflow.71- **No tools or state** — consumed as context by other skills and agents.7273## Success Criteria7475- The patterns described in this document are applied correctly in the implementation.76- Edge cases and anti-patterns listed in this document are avoided.
Run npx get-skill 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: docs only. 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.