swc-project/swc · error · anyhow::Error

unimplemented: dynamic code splitting

Error message

unimplemented: dynamic code splitting

What it means

The Node bundle API maps every chunk the SWC bundler produces onto a named output, but when the entry graph yields a dynamic chunk (BundleKind::Dynamic, created by dynamic import() or similar splitting), the binding has no code path for it and bails. This is a hard, explicit feature gap in the binding ('unimplemented'), not a configuration problem. The Rust bundler itself produced the chunk fine; only the Node-side mapping refuses it.

Source

Thrown at bindings/binding_core_node/src/bundle.rs:114

                                    .cloned(),
                            )
                            .collect(),
                        ..Default::default()
                    },
                    Box::new(Hook),
                );

                let result = bundler
                    .bundle(self.config.static_items.config.entry.clone().into())
                    .convert_err()?;

                let result = result
                    .into_iter()
                    .map(|bundle| match bundle.kind {
                        BundleKind::Named { name } | BundleKind::Lib { name } => {
                            Ok((name, bundle.module))
                        }
                        BundleKind::Dynamic => bail!("unimplemented: dynamic code splitting"),
                    })
                    .map(|res| {
                        res.and_then(|(k, m)| {
                            // TODO: Source map
                            let minify = self
                                .config
                                .static_items
                                .config
                                .options
                                .as_ref()
                                .map(|v| v.config.minify.into_bool())
                                .unwrap_or(false);

                            let output = self.swc.print(
                                &m,
                                PrintArgs {
                                    inline_sources_content: true,
                                    source_map: SourceMapsConfig::Bool(true),

View on GitHub (pinned to 5176682b65)

Solutions

  1. Replace dynamic import() with static imports for the graph you hand to this API, or split the app into multiple static entries
  2. Use a bundler with code-splitting support (webpack, rollup, esbuild, or spack driven via swc_core directly) for graphs that need dynamic chunks
  3. Track the SWC repo for dynamic-splitting support in the Node bundle binding before re-enabling import()

Example fix

// before - entry graph contains dynamic import, binding bails with
// 'unimplemented: dynamic code splitting'
import('./lazy-module.js').then(m => m.run());

// after - static import so the bundler emits only named chunks
import * as lazy from './lazy-module.js';
lazy.run();
Defensive patterns

Strategy: try-catch

Validate before calling

// reject graphs with dynamic imports before calling the bundle binding
const hasDynamicImport = (src) => /\bimport\s*\(/.test(src);
const sources = collectEntrySources(entry);
if (sources.some(hasDynamicImport)) {
  throw new Error('entry graph uses dynamic import(); Node bundle API cannot split chunks');
}

Try / catch

try {
  const out = bundle(config);
} catch (e) {
  if (String(e?.message).includes('dynamic code splitting')) {
    // fall back to per-entry static bundles or another bundler
    return bundleEachEntryStatically(config);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling the bundle binding with an entry whose module graph contains import('./lazy.js') (or another dynamic entry form), so bundler.bundle(entry) emits at least one BundleKind::Dynamic chunk alongside the named/lib ones.

Common situations: Migrating an app with lazy-loaded routes to SWC bundling; pointing the experimental bundle API at a playground or fixture that uses dynamic imports; adding a single import() during prototyping and suddenly every bundle call fails.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/79a1285c19e85863. Report an issue: GitHub.