sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript
https://sanctuary.js.org
MIT License
3.03k stars 94 forks source link

Need a way to work with react-native #580

Closed Nomeasmo closed 5 years ago

Nomeasmo commented 5 years ago

I tried to work with react native. React native is kind of node but limited in some way.

[18:49:23] The package at "node_modules/sanctuary-def/index.js" attempted to import the Node standard library module "util". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq.html#can-i-use-nodejs-packages-with-expo

The require('util') in that file decides to import utils if module.exports is present.

    if (typeof module === 'object' && typeof module.exports === 'object') {
      var util = require ('util');

Could you imagine to have an option to opt out this require or an idea for work around that problem?

davidchambers commented 5 years ago

Thanks for raising this issue, @Nomeasmo. We should be able to work around this issue. :)

https://github.com/sanctuary-js/sanctuary-def/blob/v0.18.1/index.js#L214-L222:

var inspect = (function() {
  /* istanbul ignore else */
  if (typeof module === 'object' && typeof module.exports === 'object') {
    var util = require ('util');
    /* istanbul ignore else */
    if (typeof util.inspect.custom === 'symbol') return util.inspect.custom;
  }
  return 'inspect';
} ());

We could do something like this instead:

var inspect = (function() {
  var util;
  try {
    util = require ('util');
  } catch (err) {
    return 'inspect';
  }
  return util.inspect.custom === 'symbol' ? util.inspect.custom : 'inspect';
} ());

Are you able to test this change? I can't easily test it myself as I've never used React Native.

Nomeasmo commented 5 years ago

Thanks for your efforts, David. I tried the fix but it doesn't help, but brought me rethinking. No change is needed I just installed node-util which solved it.