Monday, 17 April 2017

Conditionally require modules in javascript

I'm entering the conditional as expected (as proven by a console that is only hit in the if statement if process.env.BROWSER === true, but the require inside the if statement is still getting hit nomatter if process.env.BROWSER === true or process.env.BROWSER === false and this is causing and error when process.env.BROWSER === false. If I comment out the first require, then when process.env.BROWSER === false there is no error.

I suspect that all requires are somehow called when JS is compiling, but if that's the case how can I conditionally require a module? What's exactly happening here?

if (process.env.BROWSER) {
    language['ES'].push(require('promise?global,locale-es-ES!react-intl/locale-data/es'));
} else{
    language['ES'].push(function () {
      return Promise.resolve(require('react-intl/locale-data/es'));
    });
}



via Turnipdabeets

No comments:

Post a Comment