Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Web3 uses some Node.js core modules, which are not available in a browser env (ex:
Buffer
).Web3 should work for both a Node.js env, as well as a browser env. That's why it provides 2 entry points, the second of which is polyfilled using webpack:
https://github.com/alephium/alephium-web3/blob/master/packages/web3/package.json#L6-L7
In theory, this means that when the package is imported in a Node.js env, the first path will be used, and when it's imported in a browser env, the second path will be used.
In practice, however, only the first path is being used, no matter the environment. We can confirm this with the desktop wallet and explorer, which are browser environments. Both are importing the
dist/src/index.js
path. This is problematic because we are forced to polyfill Node.js core modules on our side as well, which is redundant, since web3 already does it.The reason why this happens is that the web3 package.json configuration is problematic. The problem is in the
"exports"
key:https://github.com/alephium/alephium-web3/blob/master/packages/web3/package.json#L9-L12
Reading the docs of
exports
:https://nodejs.org/api/packages.html#package-entry-points
Further diving into the docs, I believe that the correct configuration should be this:
https://nodejs.org/api/packages.html#dual-commonjses-module-packages
I've tested the above with desktop wallet, mobile wallet, and explorer and it works. We no longer need to polyfill Node.js core modules. I've also created a very simple Node app to test that it works well in Node.js environments as well:
> node index.js { web3: { setCurrentNodeProvider: [Function: setCurrentNodeProvider], ...
Remarks
As the JS environment evolves, many of the Node.JS core modules start becoming available for the web. For example,
Buffer
can be replaced byUint8Array
, which is an extension ofBuffer
that is compatible for both node and web envs (in fact, it's already been used in@alephium/web3
). Another example is the Node.js core modulecrypto
which could potentially be replaced by the web crypto API https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API