requireify icon indicating copy to clipboard operation
requireify copied to clipboard

throw exceptions while requireify a json file

Open huang47 opened this issue 10 years ago • 4 comments

Not quite sure if it's because my misunderstanding, but seems like requrieify doesn't like requireing json

/private/tmp/t/a.json:1
module.exports=  var global = (function(){ return this; }).call(null);  if(!global.require){    global.require = function require(key){        return global.require[key.replace(/\\/g, '/')];    };    (function(){    var require = global.require;    var ret = global.require;    Object.defineProperty(global, 'require', {        get: function(){          return ret;        },        set: function(newRequire){            ret = function(key){                key = key.replace(/\\/g, '/');                if(require[key]){                  return require[key];                }else if(require[key + '/index']){                  return require[key + '/index'];                }else{                  var temp = ret;                  var module;                  ret = newRequire;                  try {                    module = newRequire(key);                  }                  catch(e){                    ret = temp;                    throw e;                  }                  ret = temp;                  return module;                }            };            for(var key in require){              ret[key] = require[key];            }        }    });    })();  }{
                 ^
ParseError: Unexpected token var

huang47 avatar Oct 12 '14 04:10 huang47

@huang47 I haven't actually tried this myself with requireing a json file directly. Can you give me an example of the browserified code that you're trying to use?

Thanks.

johnkpaul avatar Oct 12 '14 14:10 johnkpaul

it's something like below

// in dep.json
{ "hello: "world" }

// in mod.json
var dep = require('./dep.json');
module.exports = function () {
    return dep.hello;
};

I have a workaround which prepend "module.exports = " if it's a JSON. From your perspective do you think it's a proper way to address this problem?

huang47 avatar Oct 12 '14 21:10 huang47

@huang47 yes, I do think that this is the correct solution. Any chance you have this fix easily PR-able?

johnkpaul avatar Dec 26 '14 22:12 johnkpaul

PR #13

huang47 avatar Dec 28 '14 08:12 huang47