Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript and ESM support #163

Merged
merged 11 commits into from
Sep 15, 2021
Merged

Add TypeScript and ESM support #163

merged 11 commits into from
Sep 15, 2021

Conversation

mrmckeb
Copy link
Contributor

@mrmckeb mrmckeb commented Aug 17, 2021

This PR:

  • Changes the underlying code to TypeScript.
  • Exports strict types.
  • Exports both ESM and CJS modules.

Resolves #159.

Todo:

  • Fix types by using export = instead of export default.
  • Modify the CJS file to use module.exports alongside exports.default.

@mrmckeb mrmckeb requested a review from styfle August 17, 2021 18:24
@mrmckeb mrmckeb changed the title Typescript Add TypeScript and ESM support Aug 17, 2021
readme.md Show resolved Hide resolved
},
"rules": {
"tsdoc/syntax": "warn",
// Aligns with our style-guide.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the actual style-guide here instead of copy/pasta?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we still doing this @mrmckeb?

@mrmckeb mrmckeb marked this pull request as ready for review August 20, 2021 15:42
switch (compilerOptions.module) {
case ts.ModuleKind.CommonJS: {
// Adds backwards-compatibilty for Node.js.
contents += `module.exports = exports.default;\nmodule.exports.default = exports.default;\n`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, why this is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed this earlier, @MatthiasKunnen!

Right now TypeScript doesn't really support "mixed outputs". Although, it looks like it will be fixed/improved in TypeScript 4.5 from what I can see.

The way this is now built, the package (in a CJS environment) would need to be imported with require('ms').default. That's fine, but previously you could import ms as require('ms') - so this change ensures backwards compatibility for those users.

As we can only really have one types file for the root import, this still means that a TS user using require would still be asked to change it to require('ms').default... but this at least ensures compatibility for JS-users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative, we could change the CJS export to be require('ms/cjs') (as we'd want ESM to be our default), but again that would make this a bigger breaking change for users.

Copy link
Member

@leerob leerob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but my approval likely doesn't mean much here 😄

@leerob
Copy link
Member

leerob commented Sep 8, 2021

Are we not running tests on each PR?

scripts/build.js Outdated Show resolved Hide resolved
rupurt added a commit to fremantle-industries/workbench that referenced this pull request Sep 8, 2021
TODO:

- Requires Typescript 4.5 for package exports microsoft/TypeScript#33079, vercel/ms#163
Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Let's ship this as canary dist-tag first so we can try it out 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESM version
5 participants