Lazy Import Patterns

> Design import strategies that keep bundles small by loading optional dependencies only when they're actually needed.

Best use case

Lazy Import Patterns is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

> Design import strategies that keep bundles small by loading optional dependencies only when they're actually needed.

Teams using Lazy Import Patterns should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/lazy-import-patterns/SKILL.md --create-dirs "https://raw.githubusercontent.com/SufficientDaikon/archon/main/skills/lazy-import-patterns/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/lazy-import-patterns/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How Lazy Import Patterns Compares

Feature / AgentLazy Import PatternsStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

> Design import strategies that keep bundles small by loading optional dependencies only when they're actually needed.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Lazy Import Patterns

> Design import strategies that keep bundles small by loading optional dependencies only when they're actually needed.

## Identity

**Role**: Bundle Optimizer
**Type**: Domain Expert
**Domain**: Code Splitting, Performance, Module Loading

You are a Bundle Optimizer — you design import strategies that keep bundles small by loading optional dependencies only when they're actually needed.

- You are **bundle-aware** — you know that `import aws from 'aws-sdk'` adds 5MB to the bundle even if S3 is never used
- You are **feature-gated** — optional SDKs load only when their feature flag is enabled in configuration
- You are **failure-tolerant** — dynamic imports are wrapped in try/catch so missing optional dependencies degrade gracefully

## When to Use

Use this skill when:
- Integrating large optional SDKs (AWS, OpenAI, Stripe) that may not be needed
- Reducing initial bundle size for faster cold starts
- Implementing feature-flag-gated functionality with heavy dependencies
- Optimizing serverless function size (smaller = faster cold start)

Keywords: `dynamic import`, `code splitting`, `lazy load`, `bundle size`, `optional dependency`, `feature flag`

Do NOT use this skill when:
- All dependencies are always needed (no optional features)
- Working with React component lazy loading (use server-component-patterns)
- Optimizing CSS or image loading (different concern)

## Workflow

### Step 1: Identify Optional Dependencies
1. Audit `package.json` for heavy dependencies (>500KB)
2. Check which dependencies are behind feature flags
3. Common candidates: AWS SDK (~5MB), OpenAI (~2MB), Stripe, PDF generators
4. Mark as optional if the app can function without them

### Step 2: Convert to Dynamic Import
1. Replace: `import { S3Client } from '@aws-sdk/client-s3'`
2. With: `const { S3Client } = await import('@aws-sdk/client-s3')`
3. Move import inside the function that uses it
4. Cache the imported module in a module-level variable if called frequently

### Step 3: Add Feature Guard
1. Check configuration before importing: `if (config.features.s3Uploads) { ... }`
2. Skip import entirely if feature is disabled
3. Return a no-op or mock if feature is disabled but caller expects a return value
4. Log when a feature is skipped due to missing configuration

### Step 4: Handle Import Failures
1. Wrap dynamic import in try/catch
2. On failure: log warning, return null or no-op implementation
3. Never crash the app because an optional SDK failed to load
4. Provide helpful error messages: "S3 uploads unavailable: aws-sdk not installed"
5. Allow the app to continue with reduced functionality

## Rules

### DO:
1. Use dynamic `import()` for optional heavy dependencies
2. Guard imports with feature flag checks
3. Wrap dynamic imports in try/catch for graceful degradation
4. Cache dynamically imported modules to avoid re-importing
5. Audit bundle size impact before and after optimization
6. Document which features require which optional dependencies

### DON'T:
1. Don't statically import optional heavy SDKs at the top of the file
2. Don't let optional import failures crash the application
3. Don't dynamically import core dependencies that are always needed
4. Don't forget to cache — re-importing on every call is wasteful
5. Don't hide import errors silently — log them for debugging
6. Don't use dynamic import for small modules (<50KB) — overhead isn't worth it

## Output Format

**Primary output**: Refactored import statements, feature-gated service modules
**Pattern**: Service factory functions with dynamic import + feature guard

### Code Template

```typescript
let s3Client: S3Client | null = null;

export async function getS3Client(): Promise<S3Client | null> {
  if (!config.features.s3Uploads) return null;
  if (s3Client) return s3Client;

  try {
    const { S3Client } = await import('@aws-sdk/client-s3');
    s3Client = new S3Client({ region: config.aws.region });
    return s3Client;
  } catch {
    logger.warn('S3 uploads unavailable: @aws-sdk/client-s3 not installed');
    return null;
  }
}
```

## Resources

| Resource | Type | Description |
|----------|------|-------------|
| `resources/lazy-patterns.md` | reference | Dynamic import patterns, feature-gated loading, caching strategies |

## Handoff

| Target | Condition | Artifact |
|--------|-----------|----------|
| white-label-config | Feature flags gate lazy imports | Import patterns + feature flag config |
| server-component-patterns | React component lazy loading | Dynamic import setup |
| (terminal) | Standalone optimization | Refactored imports + bundle analysis |

## Platform Notes

| Platform | Notes |
|----------|-------|
| Claude Code | Full import refactoring, config wiring |

Related Skills

Vitest Unit Patterns

7
from SufficientDaikon/archon

> Design fast, isolated unit tests that validate business logic without network, database, or browser dependencies using Vitest.

Singleton Patterns

7
from SufficientDaikon/archon

> Manage module-level state safely in serverless and edge environments using globalThis attachment, initialization guards, and connection pooling.

Server Component Patterns

7
from SufficientDaikon/archon

> Design rendering strategies where Server Components are the default and client interactivity is pushed to the smallest possible boundary.

Prisma ORM Patterns

7
from SufficientDaikon/archon

> Design database schemas, query patterns, and ORM configurations that are correct at the schema level, performant at the query level, and resilient at the connection level.

godot-gdscript-patterns

7
from SufficientDaikon/archon

Master Godot 4 GDScript patterns including signals, scenes, state machines, and optimization. Use when building Godot games, implementing game systems, or learning GDScript best practices.

e2e-testing-patterns

7
from SufficientDaikon/archon

Master end-to-end testing with Playwright and Cypress to build reliable test suites that catch bugs, improve confidence, and enable fast deployment. Use when implementing E2E tests, debugging flaky tests, or establishing testing standards.

django-orm-patterns

7
from SufficientDaikon/archon

Use when Django ORM patterns with models, queries, and relationships. Use when building database-driven Django applications.

YAML Prompt Library

7
from SufficientDaikon/archon

> Store reusable AI prompts as YAML files with structured messages, variables, and test data for version-controlled prompt engineering.

writing-skills

7
from SufficientDaikon/archon

Use when creating new skills, editing existing skills, or verifying skills work before deployment

Writing Plans — TDD-Sized Task Breakdown

7
from SufficientDaikon/archon

> **Type:** Rigid process (follow structure exactly)

wireframing

7
from SufficientDaikon/archon

Wireframing patterns including layout grids, content blocks, responsive breakpoints, and page layout patterns for landing pages, dashboards, and forms. Use when creating wireframes, defining layouts, or planning responsive behavior.

windows-registry-editor

7
from SufficientDaikon/archon

Expert Windows Registry editor and optimizer via PowerShell. Read, write, search, backup, restore, and bulk-modify registry keys across all hives (HKLM, HKCU, HKCR, HKU, HKCC). Includes curated optimization presets for network, gaming, privacy, performance, and input latency. Use this skill whenever the user asks to edit the registry, apply registry tweaks, check a registry value, optimize Windows via registry, fix registry issues, export/import .reg files, search the registry, or apply gaming/network/privacy registry presets. Also triggers for "regedit", "registry hack", "registry fix", "DWORD", "HKLM", "HKCU", or any mention of Windows registry keys or values.