diff --git a/lib/index.js b/lib/index.js index b176d768..7c2273a8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -22,26 +22,38 @@ function node_require(module) { return require(module); } -var realmConstructor; +function isNode() { + return typeof process == 'object' && (('' + process) == '[object process]' || typeof jest == 'object') +} -if (typeof Realm != 'undefined') { - // The global Realm constructor should be available on device (using JavaScriptCore). - realmConstructor = Realm; // eslint-disable-line no-undef -// eslint-disable-next-line -} else if (typeof window != 'undefined') { - // The userAgent will be defined when running in a browser (such as Chrome debugging mode). - realmConstructor = require('./browser').default; // (exported as ES6 module) -// eslint-disable-next-line -} else if (typeof process == 'object' && (('' + process) == '[object process]' || typeof jest == 'object')) { +// function isElectronRenderer() { +// return isNode() && process.type === 'renderer' +// } + +var realmConstructor; +if (isNode()) { // If process is defined, we're running in node. require('./submit-analytics')('Run'); // Prevent React Native packager from seeing this module. var binary = node_require('node-pre-gyp'); var path = node_require('path'); - var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json'))); + var pkg = path.resolve(path.join(__dirname,'../package.json')); + var binding_path = binary.find(pkg); realmConstructor = require(binding_path).Realm; } else { + if (typeof Realm != 'undefined') { + // The global Realm constructor should be available on device (using JavaScriptCore). + realmConstructor = Realm; // eslint-disable-line no-undef + // eslint-disable-next-line + } else if (typeof window != 'undefined') { + // The userAgent will be defined when running in a browser (such as Chrome debugging mode). + realmConstructor = require('./browser').default; // (exported as ES6 module) + // eslint-disable-next-line + } +} + +if (!realmConstructor) { throw new Error('Missing Realm constructor - please ensure RealmReact framework is included!'); }