Skip to content

feat: native-ESM migration of concerto-core/concerto-util for per-export tree-shaking #1304

Closed

Description

@mttrbrts

Summary

Follow-up to #1261 / PR #1263. Package-level tree-shaking now works (unused @accordproject/* packages are dropped from web bundles), but per-export tree-shaking inside concerto-core does not — importing one export vs. the whole library differs by ~0.14%.

This epic tracks migrating concerto-core (and concerto-util) source from TypeScript export = / import X = require() to native ESM so bundlers can drop unused exports.

Root cause

The ESM build is esbuild --bundle (bundle: true), which produces a CJS-in-ESM blob (__commonJS / __esm wrappers). Bundlers cannot tree-shake inside that blob. Bundling is currently forced because 58 of 59 concerto-core/src files use TS export = / import X = require(), which is illegal as native ESM (tsc TS1203).

Spike measurement (GO — conditional)

A throwaway spike migrated one leaf module (securityexception.ts) to native ESM with bundle: false:

Import Bundle size
1 export — current (bundle: true) 595,862 B (596 KB)
All exports — current 596,715 B (597 KB)
1 export — after native-ESM migration 22,908 B (22 KB)

96.2% reduction (573 KB saved) for a single leaf import. The win is real and the migration is mechanical.

Condition: full wins require concerto-util to co-migrate — it is also a CJS-in-ESM blob, so leaf imports still pull in ~all of concerto-util until it too is native ESM. With both migrated, a SecurityException import would be <2 KB.

Migration shape

Bottom-up topological traversal of the import graph. Per-file pattern:

  1. const X = require('./foo') → import X from './foo.js'
  2. const { X, Y } = require('@external') → import { X, Y } from '@external'
  3. export = ClassName → export { ClassName }; export default ClassName;
  4. Migrate globalize.ts before any file that uses it.

Build-path change:

  • Replace the build:esm esbuild bundle: true pass with an unbundled pass (tsc --module ES2020 --moduleResolution bundler, or esbuild bundle: false + splitting: true) emitting one .mjs per source file.
  • Update tsconfig.build.json: module → ES2020, moduleResolution → bundler. Removing export = resolves TS1203.

File inventory (from spike):

  • 4 pure leaf files (no internal ./ requires): securityexception.ts, metamodelexception.ts, dayjs-setup.ts, serializer/validationexception.ts
  • ~38 files with only const X = require('./internal') patterns
  • Complex hubs: basemodelmanager.ts (20 internal requires), serializer/jsonpopulator.ts (8+4), factory.ts (10)

Recommended scope

  1. Migrate concerto-util to native ESM (~1–2 days)
  2. Migrate concerto-core to native ESM, top-down (~2–3 days)
  3. Update tsconfig.build.json (module/moduleResolution) and replace the build:esm bundling step
  4. Optional: per-module exports map entries for deep imports
  5. Verify downstream packages (concerto-analysis, concerto-vocabulary, etc.) still build

Estimate: ~3–5 developer-days including test verification.

Constraints / risk

Tracked internally as beads epic concerto-334; spike concerto-08k.

Activity

  1. 3 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions