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

feat: calculator-bn.js #701

Closed
wants to merge 19 commits into from
Closed

Conversation

mvayngrib
Copy link

@mvayngrib mvayngrib commented Dec 13, 2022

Summary

implement calculator for https://github.com/indutny/bn.js/ (very commonly used in the world of crypto)

draft while i figure out the TS types for bn.js, both the inlined and the DefinitelyTyped one is making rollup unhappy 😢

lmk if you want me to add a section to the docs mirroring this one

@vercel
Copy link

vercel bot commented Dec 13, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
dinerojs ✅ Ready (Inspect) Visit Preview Dec 16, 2022 at 3:51PM (UTC)

@johnhooks
Copy link
Contributor

johnhooks commented Dec 15, 2022

Its Babel that you are having an issue with. The babel.config.js lines 14-23 would need to be replaced with this.

    presets: [
      [
        '@babel/preset-env',
        {
          modules,
          targets,
        },
      ],
      '@babel/preset-typescript',
    ],

It has to do with the error you were getting.

SyntaxError: TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:

I found this comment on an issue someone submitted on babel-loader.

Though I don't know much about Babel or what that changes about the config. It all compiled okay on my machine, but we'd need @sarahdayan to consider what this changes about the build process, or if it can be overridden for just this package.

},
"dependencies": {
"@dinero.js/core": "2.0.0-alpha.12",
"bn.js": "^5"
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be an exact version

Copy link
Author

Choose a reason for hiding this comment

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

maybe a peerDependency instead? There's two ways it'll work with dinero - by passing BN instances into amount, and by calculator-bn using it internally. Ideally those are both supported in a similar way

Copy link
Contributor

Choose a reason for hiding this comment

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

Adding it as a peerDependency could solve the issue with rollup too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Though, I think it then needs to be added to devDependencies in the root package.json

Copy link
Author

Choose a reason for hiding this comment

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

yep, already added

Copy link
Contributor

@johnhooks johnhooks left a comment

Choose a reason for hiding this comment

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

I don't believe this file belongs in the library packages/calculator-bn.js/src/bn.js.d.ts. It would probably be best if it was a custom types packages and used as a dependency.

@mvayngrib
Copy link
Author

I don't believe this file belongs in the library packages/calculator-bn.js/src/bn.js.d.ts. It would probably be best if it was a custom types packages and used as a dependency.

@types/bn.js from DefinitelyTyped wasn't working for me because of their export = BN style instead of export default BN. I'll switch to that one though and maybe we can debug it

@mvayngrib
Copy link
Author

@johnhooks i wasn't getting this error 🤔

SyntaxError: TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 15, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ac195a9:

Sandbox Source
@dinero.js/example-cart-react Configuration
@dinero.js/example-cart-vue Configuration
@dinero.js/example-pricing-react Configuration
@dinero.js/example-starter Configuration

@mvayngrib
Copy link
Author

mvayngrib commented Dec 15, 2022

@johnhooks here's the error i get on build (regardless of order in babel.config.js)

$ rimraf ./{dist,temp}
$ rollup --config
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
lerna ERR! yarn run build stderr:

src/index.ts → dist/umd/index.development.js...
(!) `this` has been rewritten to `undefined`
https://rollupjs.org/guide/en/#error-this-is-undefined
../../node_modules/bn.js/lib/bn.js
3545:     return res._forceRed(this);
3546:   };
3547: })(typeof module === 'undefined' || module, this);
                                                  ^
[!] Error: 'default' is not exported by ../../node_modules/bn.js/lib/bn.js, imported by src/api/constants.ts
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
src/api/constants.ts (1:7)
1: import BN from 'bn.js';
          ^
2: 
3: export const ZERO = new BN(0);
Error: 'default' is not exported by ../../node_modules/bn.js/lib/bn.js, imported by src/api/constants.ts
    at error (/Users/mv/code/dinero.js/node_modules/rollup/dist/shared/rollup.js:198:30)

@mvayngrib
Copy link
Author

adding the rollup commonjs plugin fixed the issue. Is that ok? Would you like it in a separate PR? 34ede23 2e75d3f

package.json Outdated
@@ -42,10 +42,12 @@
"@babel/preset-env": "7.20.2",
"@babel/preset-typescript": "7.18.6",
"@microsoft/api-extractor": "7.33.5",
"@rollup/plugin-commonjs": "^23.0.5",
Copy link
Author

Choose a reason for hiding this comment

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

lmk if u want this + adding it to rollup config in a separate PR

Copy link
Contributor

@johnhooks johnhooks Dec 15, 2022

Choose a reason for hiding this comment

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

@sarahdayan would need to take a look at this. Perhaps not rolling up bn.js and having it a peer dependency of @dinero.js/calculator-bn.js would work, it could be configured as external.

EDIT: Though I don't know how that would affect the umd builds.

Copy link
Author

Choose a reason for hiding this comment

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

added as peerDep, removed this d102791. Thanks for the tips, i'm a rollup noob!

@mvayngrib mvayngrib marked this pull request as ready for review December 15, 2022 15:49
@johnhooks
Copy link
Contributor

johnhooks commented Dec 15, 2022

@johnhooks i wasn't getting this error 🤔

It was in the Circle CI log, it was one of the errors on my system.

.eslintignore Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
mvayngrib and others added 3 commits December 15, 2022 12:25
Co-authored-by: John Hooks <bitmachina@outlook.com>
Co-authored-by: John Hooks <bitmachina@outlook.com>
@sarahdayan
Copy link
Collaborator

sarahdayan commented Dec 19, 2022

Hey @mvayngrib and sorry for the late response.

Thanks for all your work so far! I'm trying to keep Dinero.js lean at the moment though, so anything that's tied to a specific library may not be a good fit yet.

What do you think about making this a third-party Dinero.js calculator available as an npm package on your end? Based on how much traction it gets, we could reconsider bringing it to the monorepo. How does it sound?

@mvayngrib
Copy link
Author

@sarahdayan sure, let me close this for now

@mvayngrib mvayngrib closed this Dec 19, 2022
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.

None yet

3 participants