Switch build from tsc to tsup (ESM + CJS + IIFE)#1896
Open
mikedotexe wants to merge 9 commits intonear:masterfrom
Open
Switch build from tsc to tsup (ESM + CJS + IIFE)#1896mikedotexe wants to merge 9 commits intonear:masterfrom
mikedotexe wants to merge 9 commits intonear:masterfrom
Conversation
Replace the borsh npm dependency with a lean 289-line zero-dep serializer/deserializer tailored to NEAR's schema needs. Move is-my-json-valid to optionalDependencies and dynamically import it with a try/catch fallback, so environments without it still work. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: e70716e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
8291e1b to
e121c0a
Compare
This allows downstream consumers (e.g. near-connect) to import serialize, deserialize, and the Schema type directly from near-api-js instead of depending on the borsh package separately. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
New public API surface for downstream consumers (near-connect wallets) to import crypto helpers (sha256, signHash, privateKeyFromRandom, etc.) and plain JSON transaction types (PlainTransaction, mapTransaction, mapAction) directly from near-api-js. Also adds parseRpcError re-export and actionCreators backwards-compat alias. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace the tsc-only build with tsup producing three output formats: ESM (unbundled), CJS (unbundled with .cjs extension fix), and IIFE (fully bundled browser build at ~252KB). Adds lightweight browser shims for error parsing, JSON validation, and node:util to keep the IIFE bundle small. Updates package.json exports to dist/ with proper import/require/types conditions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Wrap IIFE globalThis.defineProperty in try/catch to match the js-monorepo pattern and prevent silent failures in strict environments. Fix biome import ordering in crypto/index.ts and index.ts, and normalize package.json array formatting. Build verified: 346 tests pass, all 7 attw export checks green, IIFE 252KB (unchanged effective size). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Drop nested types conditions from package.json exports -- TypeScript
resolves co-located .d.ts/.d.cts files automatically. Matches the
js-monorepo pattern. Verified with attw (all 7 subpath exports green)
and full test suite (346 pass).
Document root cause in fix-cjs-imports.js: tsup/esbuild with
bundle:false preserves source import specifiers verbatim, so .js
extensions in ESM source become broken require("./foo.js") in .cjs
output. The js-monorepo has the same latent bug.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
e121c0a to
e70716e
Compare
4 tasks
Contributor
|
I like the idea of bringing ESM + CJS back, we used to have it and some users reported that they need it back, would be nice if we could have a PR that only does that (i.e. it only changes |
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.
the big kahuna. closes #1876
first, I know it's kind of rude or perhaps a bigger deal to suggestion replacing
tscwithtsup, but it came after long and grueling hours. Originally with FastNearJS I had a combo oftscusingno-emitandesbuild, but foundtsupis a superset ofesbuildand just did everything in what felt like a bulletproof way.What's not as obvious from the summary below, is that the IIFE bundle means
near-api-jscan and will work on a static HTML site. Noyarn startit can just use a<script>tag and be pulled from a CDN if desired. Quite portable while still allowing for regular NodeJS apps to use the EcmaScript build.This change allows these three target ouputs EcmaScript, CommonJS, and browser bundle IIFE to come from the same code, and leverage ESM's safety/efficiencies. So someone can build a React app like normal and another person can (literally) run
near-api-json a tiny IoT sensor talking to the blockchain.I say this not to sway, but to celebrate as we look into this: we'll have the most efficient web3 library in the world, I'm willing to debate. the most efficient web3 lib for the most efficient blockchain in the world 💪🏻
Summary
Replace the tsc-only build with tsup producing three output formats, matching the FastNear JS Monorepo build pattern:
.cjsextensions<script>tags, with lightweight shimsAdds browser shims to keep the IIFE bundle small (~116KB saved):
error-parse-lite.ts— lightweight error parser replacing ~62KB error class hierarchyjson-validator-noop.ts— no-op ABI validator replacing ~44KB is-my-json-validutil.ts— browser shim for node:utilDepends on: #1894 (inline borsh) and #1895 (crypto utils). Merge those first, then rebase this.
Changes
tsup.config.tsscripts/fix-cjs-imports.jssrc/shims/error-parse-lite.tssrc/shims/json-validator-noop.tssrc/shims/util.tspackage.jsonexports(lib/→dist/),main/module/browser/types,buildscript (tsup),tsupdevDep,files(dist),keywords, updatedclean/autoclave/check-exportsBuild output
Test plan
pnpm build(tsup) — no errors, all 3 formats producedpnpm test— all 27 test files, 346 tests passpnpm check-exports(attw) — all 7 subpath exports green across node10/node16/bundler🤖 Generated with Claude Code