Skip to content

feat: add tree-shakeable ESM build (minor) - #218

Open
Roberto286 wants to merge 2 commits into
fireblocks:masterfrom
Roberto286:feat/esm-tree-shaking
Open

Roberto286 wants to merge 2 commits into
fireblocks:masterfrom
Roberto286:feat/esm-tree-shaking

Conversation

@Roberto286

Copy link
Copy Markdown

Pull Request Description

Makes the package tree-shakeable by publishing an additional ESM build, and trims the published tarball. No exports field, CJS output unchanged.

The generated code is already tree-shake friendly (no module-level side effects, api/* only import model types). The only missing piece was packaging: until now only a CommonJS ES5 build was shipped, with no module or sideEffects.

Changes:

  • tsconfig.esm.json (new): extends tsconfig.json, module: esnext, outDir: dist/esm, declaration: false. target stays ES5 (same syntax as the current CJS build), types still come from dist/*.d.ts via typings.
  • package.json: module: ./dist/esm/index.js, sideEffects: false, build runs tsc -p tsconfig.esm.json, copy:package also copies package.json into dist/esm/ (needed for import { version } from "../package.json" and for bundlers resolving sideEffects). New files: ["dist"]: docs/ (~7 MB) and the .ts sources are no longer published next to dist/.
  • index.ts: additive root exports of AxiosManager, Configuration, ConfigurationParameters, so a single API class can be used standalone (new UsersApi(undefined, basePath, axiosManager.axios)) without deep imports into the CJS build.

client/client.ts is intentionally untouched: it statically imports all 58 API classes, so new Fireblocks() still bundles them all. The gain is for consumers who instantiate individual API classes.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Chore / Documentation
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Compatibility

  • No exports field: deep imports such as @fireblocks/ts-sdk/dist/network/axiosManager keep working.
  • CJS build is unchanged: diff -r -x esm -x package.json of dist/ against master shows only index.js, index.js.map, index.d.ts (the two new re-exports).
  • Node, Jest and ts-node keep using main.
  • files: ["dist"]: imports of paths outside dist/ (e.g. @fireblocks/ts-sdk/models/..., /docs/...) will no longer resolve. These were never documented entry points, but flagging it.
  • Known limit: a bundle mixing root imports (ESM) and dist/... deep imports (CJS) contains two copies of the classes, so instanceof FireblocksError across copies is false. There is no shared or global state.
  • Bundlers may warn about the named import version from JSON; it is only a warning.

Porting to the generator templates

All touched files are listed in .openapi-generator/FILES, so the next generator PR will overwrite them unless the private templates are updated:

  1. package.mustache: add module and sideEffects after typings, add files, change build to tsc && tsc -p tsconfig.esm.json && npm run copy:package and copy:package to cp package.json dist/ && cp package.json dist/esm/.
  2. Add tsconfig.esm.json as a supporting file (content as in this PR). It also needs "moduleResolution": "node", otherwise resolveJsonModule fails with TS5070 under module: esnext.
  3. index.ts template: add the two re-export lines.

Note: the stock supportsES6 option of typescript-axios does (1)+(2) but also raises the CJS build to ES6, changing today's CJS output. It should be replicated in the custom template rather than enabled.

How Has This Been Tested?

  • npm test: 8 suites, 168 tests pass.
  • Installed the packed tarball in a clean project: require('@fireblocks/ts-sdk') exposes Fireblocks, TransactionsApi, AxiosManager, Configuration; ESM import { Fireblocks, BasePath } works under Node; deep import of dist/network/axiosManager works. Installed package contains only dist/, package.json, README.md, LICENSE.
  • Bundle size with esbuild (--main-fields=module,main), entry importing only UsersApi + AxiosManager:
bundle size TransactionsApiAxiosParamCreator occurrences dist/models/ inputs
master (36.1.0) 3,972,488 B 4 yes
this PR 26,739 B (0.67%) 0 none
  • Locally tested against Fireblocks API

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • Any dependent changes have been merged and published in downstream modules
  • I have added corresponding labels to the PR

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant