monosux/ethereum-block-by-date

Library breaks on production

Closed this issue · 3 comments

The library breaks in the following line when the app is built & deployed into production:

this.web3 = web3.constructor.name === 'Web3' ? web3 : { eth: web3 };

The reason is that after the codebase is minified the value returned by web3.constructor.name is never 'Web3' even when using web3js as function names are usually changed (e.g. u(...)).

Below you can find a potential solution I added to my fork however, the main drawback is that apps will break if using a provider with a minor version that is below the one used by this library (e.g. issue persists if using web3js@1.6.1 instead of 1.7.1 used by the library)

const Web3 = require('web3');

module.exports = class {
    constructor(web3) {
        this.web3 = web3 instanceof Web3 ? web3 : { eth: web3 };
       ....
       ....

Hi @santteegt,

What plugin for minification do you use? I tested a few and it worked for me. Anyway, I could just check if web3 has eth object instead:

this.web3 = typeof web3.eth != 'undefined' ? web3 : { eth: web3 };

Will it work for you?

We're using craco. Yeah, your solution should work!

Ok, published version 1.4.5, please update, and let me know if you still have the issue. Or any other issues!