algorand / js-algorand-sdk

The official JavaScript SDK for Algorand.
https://algorand.github.io/js-algorand-sdk/
MIT License
287 stars 205 forks source link

chore: build a valid esm and cjs output #861

Open neilcampbell opened 8 months ago

neilcampbell commented 8 months ago

Produce a valid ESM and CJS output, so importing into an ESM project doesn't fallback to using the CJS version.

This fixes the following error, when using algosdk in an ESM project.

SyntaxError: Named export 'Indexer' not found. The requested module 'algosdk' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'algosdk';
const { Indexer } = pkg;

I have tested that the following imports, which were previously resolvable still work correctly.

import { tealSign, modelsv2, Algodv2, getApplicationAddress } from 'algosdk'
import type { BaseHTTPClientResponse, Query } from 'algosdk/dist/types/client/baseHTTPClient'
import { BaseHTTPClientResponse, Query } from 'algosdk/src/client/baseHTTPClient'
import { sign, PUBLIC_KEY_LENGTH } from 'algosdk/dist/types/nacl/naclWrappers'
import english from 'algosdk/dist/types/mnemonic/wordlists/english'
import { AppClearStateTxn, MultisigMetadata } from 'algosdk/dist/types'

Additionally I have tested the update in AlgoKit utils-ts and all tests pass.

robdmoore commented 8 months ago

Worth also testing the imports in subscriber-ts too if possible, there's a couple of weird ones in there.

neilcampbell commented 8 months ago

@robdmoore Can confirm the AlgoKit subscriber-ts imports work correctly with this change and all tests pass.

neilcampbell commented 8 months ago

Just pushed an update to the PR to include a couple of additional index imports I missed. This update handles the below imports:

import { ABIContractNetworkInfo } from 'algosdk/dist/types/abi'
import { AssetFreezeTxn } from 'algosdk/dist/types/types/transactions'
import { AssetFreezeTransaction } from 'algosdk/dist/types/types/transactions/asset'