Js dynamic import

From Intense-Visions/harness-engineering · View interactive version

Js dynamic import is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Load ES modules on demand with import() to reduce initial bundle size and enable code splitting

How to use this skill

Skill file

JS Dynamic Import

Load ES modules on demand with import() to reduce initial bundle size and enable code splitting

When to Use

Instructions

  1. Use import('./module.js') to load a module at runtime — it returns a Promise.
  2. Use dynamic import for routes, heavy libraries, or features behind flags.
  3. Combine with await in async functions or .then() for cleaner syntax.
  4. Bundlers (webpack, Vite, Rollup) automatically split dynamic imports into separate chunks.
// Route-based code splitting
async function loadPage(route) {
  const module = await import(`./pages/${route}.js`);
  module.render();
}

// Feature-flag gating
if (user.hasFeature('charts')) {
  const { renderChart } = await import('./charts.js');
  renderChart(data);
}
  1. Add error handling — import() can fail (network error, missing module).
  2. 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:

When NOT to use:

Source

https://patterns.dev/javascript/dynamic-import

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.