AgoraIO / Tools

384 stars 829 forks source link

feat: Support for ESM #328

Closed EkaanshArora closed 1 month ago

EkaanshArora commented 1 year ago

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 commented 1 year ago

Hey @plutoless what are your thoughts?

sunshinexcode commented 1 month ago

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