fix: bundle openapi-fetch into sdk-analytics dist to avoid broken .cjs resolution - #1399
Open
ZayanKhan-12 wants to merge 2 commits into
Open
Conversation
…s resolution
The published @metamask/sdk-analytics CJS bundle contains
`require("openapi-fetch")`. openapi-fetch's `require` export condition
resolves to `dist/cjs/index.cjs`, and webpack configs without a rule for
`.cjs` files (notably create-react-app / react-scripts 5, where the
final `asset/resource` fallback only excludes js/mjs/jsx/ts/tsx) emit
that file as a static asset URL instead of parsing it as JavaScript. At
runtime `import_openapi_fetch.default` is then a string, and
instantiating the module-level Analytics singleton throws
"import_openapi_fetch.default is not a function".
Bundle openapi-fetch into the dist output via tsup's `noExternal` so
consumer bundlers never resolve the problematic `.cjs` file, and move
the dependency to devDependencies since it is no longer required at
runtime. The emitted d.ts is already self-contained.
Fixes MetaMask#1340
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Description
Fixes #1340
Root cause
@metamask/sdk-analyticsships a CJS bundle (dist/index.js) that contains a runtimerequire("openapi-fetch"). openapi-fetch'srequireexport condition resolves todist/cjs/index.cjs— a.cjsfile. Webpack configs that have no module rule for.cjs(notably create-react-app / react-scripts 5, whose finalasset/resourcefallback only excludesjs|mjs|jsx|ts|tsx) emit that file as a static asset URL instead of parsing it as JavaScript:So
import_openapi_fetch.defaultis a URL string, and the module-levelnew Analytics(...)singleton throwsimport_openapi_fetch.default is not a functionthe moment the package is loaded. This matches the reports here and in Web3Auth/web3auth-web#2224 (same error via@web3auth/modal→@metamask/sdk→@metamask/sdk-analytics), and explains why Vite-based apps are unaffected (Vite handles.cjs).Note this is not an openapi-fetch version regression — 0.13.5 and 0.13.8 ship byte-equivalent packaging (verified by diffing the npm tarballs), and an import-style/interop tweak in our source cannot help, because in the failing toolchain the resolved module contains no function at all.
Fix
Bundle openapi-fetch into the dist output via tsup
noExternal(config moved to atsup.config.ts, following the existingsdk-multichainprecedent), so consumer bundlers never resolve the problematic.cjsfile. openapi-fetch is tiny (~6 kB min, no runtime deps) and the emittedd.tswas already self-contained, soopenapi-fetchmoves fromdependenciestodevDependencies.yarn.lockis unchanged.Testing
Reproduction (before fix), minimal CRA app whose entry does
import { analytics } from '@metamask/sdk-analytics', with published@metamask/sdk-analytics@0.0.5+openapi-fetch@0.13.8(the reporter's versions):After replacing the installed package with this branch's build and rebuilding the same CRA app:
Also verified plain webpack 5 (default config) bundles both before and after without error, so this changes nothing for toolchains that already worked.
Commands run in the monorepo:
yarn workspace @metamask/sdk-analytics build— succeeds;grep openapi_fetch dist/index.jshas no matches (dependency fully inlined, dist 4 kB → 17 kB)node -e "require('.../dist/index.js')"and ESMimportofdist/index.mjs— both load;analytics.trackis a functionyarn workspace @metamask/sdk-analytics test:ci— 2 files, 6 tests passedyarn workspace @metamask/sdk-communication-layer build— succeeds (downstream consumer)🤖 Generated with Claude Code
Note
Low Risk
Packaging-only change for a deprecated analytics package; increases dist size but avoids changing runtime API or security-sensitive logic.
Overview
Fixes runtime failures in create-react-app and similar Webpack setups where
@metamask/sdk-analyticsloadedopenapi-fetchas an externalrequire, which resolved to a.cjsfile that Webpack treated as a static asset—socreateClientwas a URL string and analytics threw "default is not a function" on import.openapi-fetchis now inlined into the published ESM/CJS dist via a newtsup.config.tswithnoExternal: ['openapi-fetch'], and the build script delegates totsupinstead of inline CLI flags. The dependency moves fromdependenciestodevDependenciessince consumers no longer resolve it at install time.Reviewed by Cursor Bugbot for commit b4fbdbb. Bugbot is set up for automated code reviews on this repo. Configure here.