Skip to content

Commit 5d9406f

Browse files
authored
Merge pull request #39 from versatica/use-multiple-exports-and-expose-esm-and-cjs-files
2 parents deb81c4 + be16026 commit 5d9406f

File tree

77 files changed

+4464
-6894
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+4464
-6894
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ charset = utf-8
99
trim_trailing_whitespace = true
1010
insert_final_newline = true
1111

12-
[*.{ts,js,mjs}]
12+
[*.{ts,mts,mjs,cjs,js}]
1313
indent_style = tab
1414

1515
[*.json]

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ npm install rtp.js
1818
- All RTP and RTCP classes, types and packet related helpers are exported by the `packets` module.
1919

2020
```ts
21-
import { packets } from 'rtp.js';
21+
import * as packets from 'rtp.js/packets';
2222

2323
const {
2424
isRtp,
@@ -46,17 +46,32 @@ npm install rtp.js
4646
- The `utils` module exports some generic helpers and utilities.
4747

4848
```ts
49-
import { utils } from 'rtp.js';
49+
import * as utils from 'rtp.js/packets';
5050

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

5454
- CommonJS is also supported:
5555

5656
```ts
57-
const { packets, utils } = require('rtp.js');
57+
const packets = require('rtp.js/packets');
58+
const utils = require('rtp.js/utils');
5859
```
5960

61+
- Single entry point ("main" or "module" entries in `package.json`) is also possible for backwards compatibility:
62+
63+
```ts
64+
// ESM
65+
import { packets, utils } from 'rtp.js';
66+
67+
// CJS
68+
const { packets, utils } = require('rtp.js'=;
69+
```
70+
71+
## Note about TypeScript
72+
73+
**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.
74+
6075
## Authors
6176
6277
- Iñaki Baz Castillo [[website](https://inakibaz.me)|[github](https://github.com/ibc/)]

babel.config-cjs.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* This is Babel configuration for transpilation of TypeScript files into
3+
* JavaScript CJS files (.cjs).
4+
*/
5+
6+
export default {
7+
presets: [
8+
[
9+
'@babel/preset-env',
10+
{
11+
// Convert ESM modules to CJS.
12+
modules: 'commonjs',
13+
targets: 'defaults',
14+
},
15+
],
16+
['@babel/preset-typescript', { allowDeclareFields: true }],
17+
],
18+
plugins: [
19+
// We need to replace the .mts extension of imports/exports with .cjs,
20+
// otherwise Babel will remove the extension of the import and the parent
21+
// application will fail to resolve its location.
22+
[
23+
'module-resolver',
24+
{
25+
extensions: ['.cjs'],
26+
resolvePath(sourcePath /* , currentFile, opts */) {
27+
return sourcePath.endsWith('.mts')
28+
? sourcePath.replace(/\.mts$/, '.cjs')
29+
: sourcePath;
30+
},
31+
},
32+
],
33+
],
34+
};

babel.config-esm.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* This is Babel configuration for transpilation of TypeScript files into
3+
* JavaScript ESM files (.mjs).
4+
*/
5+
6+
export default {
7+
presets: [
8+
[
9+
'@babel/preset-env',
10+
{
11+
// Do not convert ESM modules to anything else (keep ESM).
12+
modules: false,
13+
targets: 'defaults',
14+
},
15+
],
16+
['@babel/preset-typescript', { allowDeclareFields: true }],
17+
],
18+
plugins: [
19+
// We need to replace the .mts extension of imports/exports with .mjs,
20+
// otherwise Babel will remove the extension of the import and the parent
21+
// application will fail to resolve its location.
22+
[
23+
'module-resolver',
24+
{
25+
extensions: ['.mjs'],
26+
resolvePath(sourcePath /* , currentFile, opts */) {
27+
return sourcePath.endsWith('.mts')
28+
? sourcePath.replace(/\.mts$/, '.mjs')
29+
: sourcePath;
30+
},
31+
},
32+
],
33+
],
34+
};

babel.config-jest.cjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* This is Babel configuration for transpilation of TypeScript files into
3+
* JavaScript CJS files when running Jest with ts-jest.
4+
*
5+
* In this case we don't want to replace the .mts extension of imports/exports
6+
* (otherwise Jest will fail to locate them).
7+
*
8+
* NOTE: This file is CJS rather than ESM because older version of Node.js
9+
* cannot load Babel ESM configuration files synchronously.
10+
*/
11+
12+
module.exports = {
13+
presets: [
14+
[
15+
'@babel/preset-env',
16+
{
17+
// Convert ESM modules to CJS.
18+
modules: 'commonjs',
19+
targets: 'defaults',
20+
},
21+
],
22+
['@babel/preset-typescript', { allowDeclareFields: true }],
23+
],
24+
};

babel.config.cjs

Lines changed: 0 additions & 10 deletions
This file was deleted.

eslint.config.mjs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const config = tsEslint.config(
88
{
99
languageOptions: {
1010
sourceType: 'module',
11-
globals: { ...globals.browser },
11+
globals: { ...globals.node },
1212
},
1313
linterOptions: {
1414
noInlineConfig: false,
@@ -107,19 +107,21 @@ const config = tsEslint.config(
107107
yoda: 2,
108108
},
109109
},
110-
// NOTE: We need to apply this only to .ts files (and not to .mjs files).
110+
// NOTE: We need to apply this only to .mts source files (and not to .mjs
111+
// files).
111112
...tsEslint.configs.recommendedTypeChecked.map(item => ({
112113
...item,
113-
files: ['src/**/*.ts'],
114+
files: ['src/**/*.mts'],
114115
})),
115-
// NOTE: We need to apply this only to .ts files (and not to .mjs files).
116+
// NOTE: We need to apply this only to .mts source files (and not to .mjs
117+
// files).
116118
...tsEslint.configs.stylisticTypeChecked.map(item => ({
117119
...item,
118-
files: ['src/**/*.ts'],
120+
files: ['src/**/*.mts'],
119121
})),
120122
{
121-
name: '.ts files',
122-
files: ['src/**/*.ts'],
123+
name: '.mts source files',
124+
files: ['src/**/*.mts'],
123125
languageOptions: {
124126
parserOptions: {
125127
projectService: true,
@@ -163,9 +165,9 @@ const config = tsEslint.config(
163165
},
164166
},
165167
{
166-
name: '.ts test files',
168+
name: '.mts test files',
167169
...jestEslint.configs['flat/recommended'],
168-
files: ['src/test/**/*.ts'],
170+
files: ['src/test/**/*.mts'],
169171
rules: {
170172
...jestEslint.configs['flat/recommended'].rules,
171173
'jest/no-disabled-tests': 2,

jest.config.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const config = {
2+
verbose: true,
3+
testEnvironment: 'node',
4+
testRegex: 'src/test/.*.test.mts',
5+
// Make Jest consider .mts files as if they were ES modules.
6+
moduleFileExtensions: ['mts', 'mjs', 'js', 'ts'],
7+
transform: {
8+
'^.+\\.mts?$': [
9+
'babel-jest',
10+
{
11+
// We need special Babel settings for Jest plust we need it to be in
12+
// a CJS configuration file, otherwise old versions of Node will fail
13+
// to run Jest.
14+
configFile: './babel.config-jest.cjs',
15+
},
16+
],
17+
},
18+
coveragePathIgnorePatterns: ['src/Logger.mts', 'src/test'],
19+
cacheDirectory: '.cache/jest',
20+
};
21+
22+
export default config;

0 commit comments

Comments
 (0)