perf: load the network from a single shared asset instead of inlining it in both builds - #18
Merged
Merged
Conversation
… it in both builds The Eurostat marnet was inlined as a ~1.1 MB JSON string in src/lib/marnet.ts and compiled into both the CJS and ESM builds, so the published tarball carried the network twice (~2.2 MB of identical data). Extract the network to a single data/marnet.cjs module. Both builds import it via '../../data/marnet.cjs', which resolves to one shared dist/data/marnet.cjs at runtime (CJS require and ESM import of a .cjs both load it). The build and test scripts copy the asset into dist/data and build/data respectively. Roughly halves the published size (~329 KB -> ~184 KB packed, ~2.5 MB -> ~1.3 MB unpacked). No public API change.
mayurrawte
force-pushed
the
feat/issue-10-shared-network-asset
branch
from
July 3, 2026 07:54
a2937be to
53173ff
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Eurostat marnet was inlined as a ~1.1 MB JSON string in
src/lib/marnet.tsand compiled into both the CJS and ESM builds, so the published tarball shipped the network twice (~2.2 MB of identical data). This was already flagged as a follow-up inCHANGELOG.md.This PR extracts the network to a single shared asset that both builds load at runtime, rather than a source module compiled into each.
What changed
data/marnet.cjs— the network now lives in one committed CommonJS data module (module.exports = JSON.parse(...)), with a companiondata/marnet.d.ctstype declaration.src/lib/marnet.ts— slimmed toimport marnetData from '../../data/marnet.cjs'and re-export. The specifier resolves to a singledist/data/marnet.cjsat runtime from bothdist/cjs/libanddist/esm/lib(../../data/marnet.cjs→dist/data/marnet.cjsin both).scripts/build-marnet.cjs— updated to generatedata/marnet.cjsinstead ofsrc/lib/marnet.ts.scripts/copy-marnet.cjs(new) — copies the asset intodist/data(build) andbuild/data(tests).package.json—buildandbuild:testcopy the asset into place;filesalready shipsdist.Why a
.cjsdata module (not fs or JSON import).cjsfile is always CommonJS regardless of the surroundingpackage.jsontype, so CJSrequireand ESMimportof it both resolve the single asset across Node 18/20/22 — noimport.metavs__dirnamesplit, no version-fragile JSON import attributes (assertvswith), and nofs(so it stays bundler-friendly).Impact
npm pack --dry-run).dist/data/marnet.cjs); the per-buildmarnet.jsfiles are now a few hundred bytes each.Validation
All steps CI runs, from a clean tree:
npm cinpm run lint✅npm run format:check✅npm run build✅npm test✅ (45 tests; addedDEFAULT_MARNET loads the shared network asset)Additionally verified both published entrypoints load the single asset and route identically:
require('./dist/cjs/index.js')→ 9847 features, Shanghai→Rotterdam 10666 nmimport('./dist/esm/index.js')→ 9847 features, Shanghai→Rotterdam 10666 nmCloses #10