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

BigInt polyfill for ES2018 #732

Closed
hazae41 opened this issue Jan 31, 2021 · 2 comments
Closed

BigInt polyfill for ES2018 #732

hazae41 opened this issue Jan 31, 2021 · 2 comments

Comments

@hazae41
Copy link

hazae41 commented Jan 31, 2021

When compiling ipfs with es2018 target on https://esm.sh, I get the following error

https://esm.sh/ipfs?target=es2018&dev

/* esm.sh - error */
throw new Error("[esm.sh] " + "esbuild: Big integer literals are not available in the configured target environment");
export default null;

Maybe we could polyfill BigIntegers for ES2018?

@evanw
Copy link
Owner

evanw commented Feb 1, 2021

BigInts are not lowered to older syntax in esbuild because there is no direct translation. The BigInt specification decided to use normal JavaScript operators for BigInts and the JavaScript language does not have user-pluggable operator overloading. Code wanting to use BigInts on older browsers will have to be rewritten to use a library that uses function calls instead of native JavaScript operators. That way the function calls can be substituted with older functionality using a polyfill. Here is a quote from https://github.com/GoogleChromeLabs/jsbi:

To use BigInts in your code today, you need a library. But there’s a difficulty: the BigInt proposal changes the behavior of operators (like +, >=, etc.) to work on BigInts. These changes are impossible to polyfill directly; and they are also making it infeasible (in most cases) to transpile BigInt code to fallback code using Babel or similar tools. The reason is that such a transpilation would have to replace every single operator in the program with a call to some function that performs type checks on its inputs, which would incur an unacceptable performance penalty.

The solution is to do it the other way round: write code using a library’s syntax, and transpile it to native BigInt code when available. JSBI is designed for exactly this purpose: it provides a BigInt “polyfill” implementation that behaves exactly like the upcoming native BigInts, but with a syntax that you can ship on all browsers, today.

I haven't tried the JSBI library myself but the approach sounds solid from their description. And I'm not familiar with IPFS's JavaScript tooling. Do they already have a transpilation strategy for BigInts? Maybe it's worth asking them?

@evanw
Copy link
Owner

evanw commented Feb 3, 2021

I'm going to close this since this is not something I plan on supporting in esbuild itself, for reasons stated above.

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

No branches or pull requests

2 participants