Tools icon indicating copy to clipboard operation
Tools copied to clipboard

feat: Support for ESM

Open EkaanshArora opened this issue 1 year ago • 2 comments

Can we add support for ESM for agora-token?

ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and export statements.

Node.js fully supports ECMAScript modules as they are currently specified and provides interoperability between them and its original module format, CommonJS.

Newer build tools like Vite expect libraries to support ESM exports. Not having ESM and only exporting to CJS, needs a workaround to use the library like so:

import TokenServerImport from 'agora-token';
const { RtcRole, RtcTokenBuilder } = TokenServerImport; // CJS module import

Using a named import from the package might work in dev, but would break in production builds: image

EkaanshArora avatar Mar 31 '23 12:03 EkaanshArora

Hey @plutoless what are your thoughts?

EkaanshArora avatar Apr 04 '23 13:04 EkaanshArora

@EkaanshArora Set the type of package.json to module, and try again

sunshinexcode avatar Jul 22 '24 07:07 sunshinexcode