tus / tus-js-client

A pure JavaScript client for the tus resumable upload protocol
https://tus.io/
MIT License
2.12k stars 316 forks source link

Move to TypeScript #683

Open Acconut opened 7 months ago

Acconut commented 7 months ago

This PR is an experiment to see how we can utilize TypeScript to transpile the source code into CommonJS/ESM code included in the package. The benefit is that we have type-checked source code and also don't have to manually maintain a type definition in index.d.ts.

Acconut commented 7 months ago

@Murderlon You mentioned that dual publishing a package containing CommonJS and ESM can lead to unexpected problems. Are there some links or resources digging more into this? Are you referring to what Node.js calls the dual package hazard? We have dual published this package for four years (since https://github.com/tus/tus-js-client/commit/bdb614438378c938444456941496da534414df6b) and didn't run into any problems and no major issues where reported to us.

By completely ditching CommonJS, I am worrying that we leave some application behind which are still using it and have a hard time upgrading to ESM (I think to recall that our website tus.io also still compiles to CommonJS, but I might get that wrong). Is this concern unjustified nowadays?

If so, it would be great if we could reduce the complexity of this package's setup.

Murderlon commented 7 months ago

You mentioned that dual publishing a package containing CommonJS and ESM can lead to unexpected problems. Are there some links or resources digging more into this?

This from my experience + some other prominent maintainers I talk with. All my issues in the past with a package were from dual publishing, not ESM. Examples include Preact itself and even the package dual-publish, which is supposed to solve it.

It is possible and since we already have it working it's not really a problem. But at some point we should make the jump, ESM is the future.

By completely ditching CommonJS, I am worrying that we leave some application behind which are still using it and have a hard time upgrading to ESM

Technically everyone can use it, even in a CommonJS setup:

const tusPromise = import('tus-js-client')

module.exports = tusPromise.then((tus) => tus.default)

It's not the ideal developer experience, but it's important to note that tus-js-client is close to done for tus protocol v1. It's stable and it's totally fine to stay on the previous major version if you must for a while. And even without ditching CommonJS, this would be a major version anyway, and I think it's better to have less major versions (rather than major version for TS then later again for ESM).


Some tools which are importent to check:

Murderlon commented 7 months ago

Some more warnings: https://publint.dev/tus-js-client@4.1.0

Acconut commented 6 months ago

I updated this PR to touch the export structure in package.json as little as possible and instead opened https://github.com/tus/tus-js-client/issues/693 to handle the restructuring of the exports in a separate PR.