mediasoup-client icon indicating copy to clipboard operation
mediasoup-client copied to clipboard

Export TS types in a separate entry point via 'mediasoup-client/types'

Open ibc opened this issue 1 year ago • 2 comments

Details

The idea is that, instead of exporting types like this:

import { types as mediasoupClientTypes } from 'mediasoup-client';

We import them this way:

import types as mediasoupClientTypes from 'mediasoup-client/types';

I've also removed "main" and "types" fields in package.json since they are replaced by the new "exports" field.

Related documentation

  • "TypeScript and NPM package.json exports the 2024 way": https://www.kravchyk.com/typescript-npm-package-json-exports/

TODO 1

I've briefly tested this and it looks like for this to work, the parent application must have in its tsconfig.json:

"module": "nodenext",
"moduleResolution": "nodenext",

Is this ok? Any other implications?

TODO 2

Should we stop exporting types from 'mediasoup-client' and instead force applications to import them from 'mediasoup-client/types'?

TODO 3

We should export HandlerInterface type so applications do not need to do this ugly thing:

import { HandlerFactory as MediasoupClientHandlerFactory } from 'mediasoup-client/lib/handlers/HandlerInterface';

ibc avatar Oct 10 '24 12:10 ibc

I've briefly tested this and it looks like for this to work, the parent application must have in its tsconfig.json:

"module": "nodenext", "moduleResolution": "nodenext",

If the application does not set such values (for any reason they may have), can it still use the old way of importing the types?

jmillan avatar Oct 11 '24 12:10 jmillan

I am not sure. I've tried to test using mediasoup-demo client app but it's SUPER OLD and doesn't even use TypeScript so we are lost here.

ibc avatar Oct 11 '24 12:10 ibc

This is ready for review.

ibc avatar Dec 18 '24 09:12 ibc

NOTE: These changes have been reverted in 3.8.1. The world is not ready for these things yet.

ibc avatar Dec 18 '24 17:12 ibc

NOTE: These changes have been reverted in 3.8.1. The world is not ready for these things yet.

What's the reason?

Fabioni avatar Dec 19 '24 09:12 Fabioni

NOTE: These changes have been reverted in 3.8.1. The world is not ready for these things yet.

What's the reason?

The reason is that the syntax of the "exports" field in package.json is not universally defined. NPM docs refer to the documentation of "exports" in package.json of Node projects where the value of each item in "exports" is basically a path to the corresponding file (it doesn't cover TypeScript .d.ts files at all), while in webpack the value can be an object with "import", "require", "types" (pointing to the .d.ts file with TypeScript definitions). Then ts-jest doesn't read the "types" field so it throws an error because "types not found", etc. Then ts-jest also fails if an entry in "exports" points to a file that exports a const, etc etc. I investigated and indeed it's a know ts-jest issue.

It's a complete nightmare and I already spent too much time trying to deal with it.

ibc avatar Dec 19 '24 09:12 ibc