Skip to content

Conversation

@ibc
Copy link
Member

@ibc ibc commented Mar 17, 2025

Details

  • This PR replaces PR Use multiple exports #38.
  • Expose rtp.js/packets and rtp.js/utils paths in exports field in package.json. It works no matter the consumer app uses ESM or CommonJS.
  • Still expose a single index based entry point for backwards compatibility.
  • Expose ESM and CJS files in exports object instead of transpiling TypeScript code only to CommonJS.
  • Use module: NodeNext in tsconfig.json, meaning that TypeScript consumers with moduleResolution "node16", 'NodeNext" or "bundler" can import rtp.js/packets and rtp.js/utils paths, while TypeScript projects with older configuration using moduleResolution "commonjs" can still import rtp.js single entry point.
  • Internally, rename all .ts files to .mts to make it clear that those contain TypeScript code in ESM syntax rather than TypeScript code in CJS syntax.

Before

Using ESM:

import { packets, utils } from 'rtp.js';

const rtpPacket = new packets.RtpPacket();
const stringLen = utils.getStringByteLength('œæ€');

Using CJS:

const { packets, utils } = require('rtp.js');

const rtpPacket = new packets.RtpPacket();
const stringLen = utils.getStringByteLength('œæ€');

After

Using ESM:

import { RtpPacket } from 'rtp.js/packets';
import { getStringByteLength } from 'rtp.js/utils';

const rtpPacket = new RtpPacket();
const stringLen = getStringByteLength('œæ€');

Using CJS:

const { RtpPacket } = require('rtp.js/packets');
const { getStringByteLength } = require('rtp.js/utils');

const rtpPacket = new RtpPacket();
const stringLen = getStringByteLength('œæ€');

Note: single entry point still possible

Single entry point ("main" or "module" entries in package.json) is also possible for backwards compatibility:

// ESM
import { packets, utils } from 'rtp.js';

// CJS
const { packets, utils } = require('rtp.js'=;

@ibc ibc mentioned this pull request Mar 17, 2025
@ibc ibc merged commit 5d9406f into master Mar 18, 2025
8 checks passed
@ibc ibc deleted the use-multiple-exports-and-expose-esm-and-cjs-files branch March 18, 2025 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants