Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{ts,js,mjs}]
[*.{ts,mts,mjs,cjs,js}]
indent_style = tab

[*.json]
Expand Down
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ npm install rtp.js
- All RTP and RTCP classes, types and packet related helpers are exported by the `packets` module.

```ts
import { packets } from 'rtp.js';
import * as packets from 'rtp.js/packets';

const {
isRtp,
Expand Down Expand Up @@ -46,17 +46,32 @@ npm install rtp.js
- The `utils` module exports some generic helpers and utilities.

```ts
import { utils } from 'rtp.js';
import * as utils from 'rtp.js/packets';

const view = utils.stringToDataView('foo');
const view = utils.stringToDataView('fooœæ€ñ#¢∞Ω©bar');
```

- CommonJS is also supported:

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

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

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

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

## Note about TypeScript

**rtp.js** is written in TypeScript with `module: NodeNext`, meaning that TypeScript projects that have **rtp.js** as dependency must have `moduleResolution` with value "node16", 'NodeNext" or "bundler" in their `tsconfig.json` file.

## Authors

- Iñaki Baz Castillo [[website](https://inakibaz.me)|[github](https://github.com/ibc/)]
Expand Down
34 changes: 34 additions & 0 deletions babel.config-cjs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* This is Babel configuration for transpilation of TypeScript files into
* JavaScript CJS files (.cjs).
*/

export default {
presets: [
[
'@babel/preset-env',
{
// Convert ESM modules to CJS.
modules: 'commonjs',
targets: 'defaults',
},
],
['@babel/preset-typescript', { allowDeclareFields: true }],
],
plugins: [
// We need to replace the .mts extension of imports/exports with .cjs,
// otherwise Babel will remove the extension of the import and the parent
// application will fail to resolve its location.
[
'module-resolver',
{
extensions: ['.cjs'],
resolvePath(sourcePath /* , currentFile, opts */) {
return sourcePath.endsWith('.mts')
? sourcePath.replace(/\.mts$/, '.cjs')
: sourcePath;
},
},
],
],
};
34 changes: 34 additions & 0 deletions babel.config-esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* This is Babel configuration for transpilation of TypeScript files into
* JavaScript ESM files (.mjs).
*/

export default {
presets: [
[
'@babel/preset-env',
{
// Do not convert ESM modules to anything else (keep ESM).
modules: false,
targets: 'defaults',
},
],
['@babel/preset-typescript', { allowDeclareFields: true }],
],
plugins: [
// We need to replace the .mts extension of imports/exports with .mjs,
// otherwise Babel will remove the extension of the import and the parent
// application will fail to resolve its location.
[
'module-resolver',
{
extensions: ['.mjs'],
resolvePath(sourcePath /* , currentFile, opts */) {
return sourcePath.endsWith('.mts')
? sourcePath.replace(/\.mts$/, '.mjs')
: sourcePath;
},
},
],
],
};
24 changes: 24 additions & 0 deletions babel.config-jest.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* This is Babel configuration for transpilation of TypeScript files into
* JavaScript CJS files when running Jest with ts-jest.
*
* In this case we don't want to replace the .mts extension of imports/exports
* (otherwise Jest will fail to locate them).
*
* NOTE: This file is CJS rather than ESM because older version of Node.js
* cannot load Babel ESM configuration files synchronously.
*/

module.exports = {
presets: [
[
'@babel/preset-env',
{
// Convert ESM modules to CJS.
modules: 'commonjs',
targets: 'defaults',
},
],
['@babel/preset-typescript', { allowDeclareFields: true }],
],
};
10 changes: 0 additions & 10 deletions babel.config.cjs

This file was deleted.

20 changes: 11 additions & 9 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const config = tsEslint.config(
{
languageOptions: {
sourceType: 'module',
globals: { ...globals.browser },
globals: { ...globals.node },
},
linterOptions: {
noInlineConfig: false,
Expand Down Expand Up @@ -107,19 +107,21 @@ const config = tsEslint.config(
yoda: 2,
},
},
// NOTE: We need to apply this only to .ts files (and not to .mjs files).
// NOTE: We need to apply this only to .mts source files (and not to .mjs
// files).
...tsEslint.configs.recommendedTypeChecked.map(item => ({
...item,
files: ['src/**/*.ts'],
files: ['src/**/*.mts'],
})),
// NOTE: We need to apply this only to .ts files (and not to .mjs files).
// NOTE: We need to apply this only to .mts source files (and not to .mjs
// files).
...tsEslint.configs.stylisticTypeChecked.map(item => ({
...item,
files: ['src/**/*.ts'],
files: ['src/**/*.mts'],
})),
{
name: '.ts files',
files: ['src/**/*.ts'],
name: '.mts source files',
files: ['src/**/*.mts'],
languageOptions: {
parserOptions: {
projectService: true,
Expand Down Expand Up @@ -163,9 +165,9 @@ const config = tsEslint.config(
},
},
{
name: '.ts test files',
name: '.mts test files',
...jestEslint.configs['flat/recommended'],
files: ['src/test/**/*.ts'],
files: ['src/test/**/*.mts'],
rules: {
...jestEslint.configs['flat/recommended'].rules,
'jest/no-disabled-tests': 2,
Expand Down
22 changes: 22 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const config = {
verbose: true,
testEnvironment: 'node',
testRegex: 'src/test/.*.test.mts',
// Make Jest consider .mts files as if they were ES modules.
moduleFileExtensions: ['mts', 'mjs', 'js', 'ts'],
transform: {
'^.+\\.mts?$': [
'babel-jest',
{
// We need special Babel settings for Jest plust we need it to be in
// a CJS configuration file, otherwise old versions of Node will fail
// to run Jest.
configFile: './babel.config-jest.cjs',
},
],
},
coveragePathIgnorePatterns: ['src/Logger.mts', 'src/test'],
cacheDirectory: '.cache/jest',
};

export default config;
Loading