lukeed/clsxPublic

A tiny (239B) utility for constructing `className` strings conditionally.

fix: fix esm support and types #57

Mergedremcohaszingmerged 1 commit intomasterfromrefs/pull/57/head
  1. RE

    The non-standard `package.json` field `module` was used. This pointed to a faux ESM file (Uses ESM syntax, but has a `.js` file extension in a package which doesn’t specify `"type": "module"`.

    ESM support was fixed by using the `.mjs` file extension for the ESM export and defining a proper `exports` field in `package.json.

    The types reflected a package that has the `module.exports.default` field. This was incorrect. The CommonJS types have now been fixed to use `export =`, which is the correct way to type modules that use `module.exports` assignments.

    Additional types were added for ESM support.

    This fixes the following issues that have previously been closed without a solution.

    Closes #27 Closes #43 Closes #50

    To try this locally, try running the following in the Node.js repl from the package root:

    ``` $ node Welcome to Node.js v18.12.0. Type ".help" for more information. > require('clsx') <ref *1> [Function: r] { clsx: [Circular *1] } > await import('clsx') [Module: null prototype] { clsx: [Function: clsx], default: [Function: clsx] }

    ```

  2. LU

    clsx already works with `require` and `import` and `import()` statements:

    <img width="591" alt="Screen Shot 2022-11-01 at 10 50 44 AM" src="https://user-images.githubusercontent.com/5855893/199303822-bf8571c1-3c29-478b-a419-db9549b8ebe7.png">

    The rest of this PR is adding breaking changes:

    1. Adding `exports` map (always is), and is unnecessary since there's only 1 file/entry point 2. The `module` change to a `.mjs` extension breaks most older bundler versions (rollup/webpack) since it changes how they will handle module resolution for the rest of the tree. (Dark, early ESM days) 3. Using `.d.mts` is strictly unnecessary & binds you to a minimum TS version for no reason at all, especially given the `export = clsx` contents.

  3. lukeedclosed4 years ago on GitHub

  4. remcohaszingreferenced this in fix(types): fix TypeScript types #584 years ago on GitHub

  5. RE
    Author

    > clsx already works with `require` and `import` and `import()` statements: > > <img alt="Screen Shot 2022-11-01 at 10 50 44 AM" width="591" src="https://user-images.githubusercontent.com/5855893/199303822-bf8571c1-3c29-478b-a419-db9549b8ebe7.png">

    Yes, commonjs files can be imported from ESM. The fact that there’s a faux ESM file and TypeScript type definitions describe a default export instead of `export =`, gave the impression the intended behaviour was to support actual ESM. A lot of packages have there type definitions wrong, which has become more apparent since the release of TypeScript 4.7.

    > The rest of this PR is adding breaking changes: > > 1. Adding `exports` map (always is), and is unnecessary since there's only 1 file/entry point

    Whether or not this is breaking is subjective. This disallows people to import subpaths from the package. I would consider that unintemded behaviour and therefor not consider this to be a breaking change. However, since you’re the maintainer, your view on this counts.

    Adding an `exports` map isn’t unnecessary when adding native ESM support. However, if you’re not interested, then indeed the `exports` map is unnecessary.

    > 2. The `module` change to a `.mjs` extension breaks most older bundler versions (rollup/webpack) since it changes how they will handle module resolution for the rest of the tree. (Dark, early ESM days)

    I wasn’t aware of this. It is possible to support both though (but not without `exports`).

    > 3. Using `.d.mts` is strictly unnecessary & binds you to a minimum TS version for no reason at all, especially given the `export = clsx` contents.

    This isn’t true. This file is only used by the `node16` / `nodenext` module resolution to support native ESM. The `index.d.ts` is still used by other module resolutions as it always was.

  6. LU

    > Whether or not this is breaking is subjective.

    Node 13-13.7 had a different exports map specification. The finalized spec throws an error in these versions and there’s no way to support both. Thus adding sny exports map is breaking as it has to be done with a minimum version bump.

    > This isn’t true.

    Yes it is. You can define “types” for exports to a standard .d.ts file, even for nodenext/node16 mode. The mts extension is just to force/guarantee that TS treats the file as ESM, similar to .mjs even tho .js can also contain ESM. In fact, mts is a front/stand-in for would-be-emitted .mjs files.

  7. remcohaszingreferenced this in add conditional exports #633 years ago on GitHub

  8. wojtekmajreferenced this in "This expression is not callable." error when using TypeScript with moduleResolution "node16" or "nodenext" #753 years ago on GitHub

  9. wojtekmajreferenced this in Module format error on import with vitest #693 years ago on GitHub

  10. WO

    @lukeed I kindly ask you to reconsider this or equivalent PR. [Incorrect types](https://arethetypeswrong.github.io/?p=clsx%401.2.1) are causing us a ton of problems which may force us to replace clsx with [correctly typed](https://arethetypeswrong.github.io/?p=classnames%402.3.2) classnames in many repositories and that would be a huge bummer.

  11. lukeedmentioned3 years ago on GitHub

  12. lukeedsubscribed3 years ago on GitHub

  13. LU

    I'll cut a new major version that includes an "exports" map

  14. lukeedreopened3 years ago on GitHub

  15. CC

    ## [Codecov](https://app.codecov.io/gh/lukeed/clsx/pull/57?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Luke+Edwards) Report Patch and project coverage have no change. > Comparison is base [(`6da37d6`)](https://app.codecov.io/gh/lukeed/clsx/commit/6da37d67472ea9eb177a4ea9a9fa2f3f4a3c445e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Luke+Edwards) 100.00% compared to head [(`40a074e`)](https://app.codecov.io/gh/lukeed/clsx/pull/57?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Luke+Edwards) 100.00%.

    :exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please [install the Github App Integration](https://github.com/apps/codecov) for your organization. [Read more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Luke+Edwards).

    <details><summary>Additional details and impacted files</summary>

    ```diff @@ Coverage Diff @@ ## master #57 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 1 1 Lines 22 22 ========================================= Hits 22 22 ```

    </details>

    [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/lukeed/clsx/pull/57?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Luke+Edwards). :loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Luke+Edwards).

  16. lukeedmerged 3ec8e9fa2f6e3 years ago on GitHub

  17. lukeedclosed3 years ago on GitHub

  18. lukeedreferenced this in Fix module format error with vitest (and possibly other esm usage) #703 years ago on GitHub

  19. RE
    Author

    Thanks for merging and releasing this! ❤️

  20. remcohaszinghead-ref-deleted3 years ago on GitHub

  21. LU

    Thank you for the PR & patience!

  22. sync-by-unito[bot]botreferenced this in build(deps): bump clsx from 1.2.1 to 2.0.0 #3123 years ago on GitHub

  23. tgelureferenced this in Runtime error with Webpack Module Federation because of old "clsx" dependency #54832 years ago on GitHub

  24. serprexreferenced this in Update clsx to 2.1.0 #10312 years ago on GitHub

  25. carlblockreferenced 632093d97c2e2 years ago on GitHub

  26. carlblockreferenced this in fix/esm-rollup-clsx #7542 years ago on GitHub

  27. rsazreferenced this in [Snyk] Upgrade clsx from 2.0.0 to 2.1.1 #512 years ago on GitHub

  28. salgadevreferenced this in [Snyk] Upgrade clsx from 2.0.0 to 2.1.1 #172 years ago on GitHub

  29. wierzareferenced this in [Snyk] Upgrade clsx from 2.0.0 to 2.1.1 #1472 years ago on GitHub

  30. STRMLreferenced 18e525fdc268last year on GitHub