mathigon / fermat.js

Mathematics and statistics library for TypeScript.
http://mathigon.io/fermat
MIT License
103 stars 15 forks source link

Feature request: improve bundle #3

Closed Torsten85 closed 4 years ago

Torsten85 commented 4 years ago

Hello,

currently the "module" field of the package.json points to your "index.ts" file. This break e.g. creat-react-app. It would be better if you would provide two bundled version, one for cjs and one for esm. Then you could set the "main" field to your index.cjs.js file and your "module" field to "index.esm.js" ... in addition to that a "types" field pointing to typescript declarations.

Would you accept a pull request for those changes?

Besides that, this is a fantastic library btw 😉

plegner commented 4 years ago

We already have a compiled CJS version as the "main" field (dist/fermat.js) which you can use using require('@mathigon/fermat'), and you can definitely send a PR to add types for this.

Can you explain why the "module" field doesn't work with creat-react-app? I'd prefer keeping this as the Typescript source file, so that IDE links remain intact (e.g. command-click goes to the original declaration, rather than a compiled JS file), and so that end-users can take advantage of TS minification (e.g. https://github.com/angular/ts-minify).

Happy to hear that you like this library!

Torsten85 commented 4 years ago

Thanks for your quick reply.

The "module" field of a package.json is intended for a esm js entry point (see https://rollupjs.org/guide/en/#publishing-es-modules) So your "main" field has to point to cjs (which it already does) the optional "module" field has to point to "esm".

Currently create-react-app (with typescript template) throws because it expects a js file at "module":

Failed to compile.

./node_modules/@mathigon/fermat/src/complex.ts 10:17
Module parse failed: Unexpected token (10:17)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 
| 
> const absStr = (n: number, suffix?: string) => {
|   const prefix = n < 0 ? '–' : '';
|   if (Math.abs(n) === 1 && suffix) return prefix + suffix;

You won't loose the ability to jump to source by cmd+clicking, because typescript will find the index.ts in the module root.

I'll make a pull request so you can test this approach.

plegner commented 4 years ago

Fixed in https://github.com/mathigon/fermat.js/commit/b20cc2d289bd3f2d134c0d4ca738bb8b1504df78