I can't transform underscore.min with Babel. I need it because, with gulp, I merge all files and then process them with babel, but with underscore it returns an error.
You can try it out here: https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=es2015
At the bottom of the right panel an error appears: "u is undefined".
I'm getting "Cannot read property '_' of undefined", which is different but also broken.
Which version of Underscore are you using?
I'm suspicious that this is related to https://github.com/jashkenas/underscore/pull/2153 which has been fixed in master, but we have not yet shipped a release that includes it.
You could try it with the version of Underscore on master (https://github.com/jashkenas/underscore/blob/master/underscore.js) and see if that resolves the issue?
Let me know what you find.
I'm using the version 1.8.3, the latest. Unfortunately, I cannot use the unminified version, because of the "require" methods not supported.
exports is not supported in the browser, you need a commonjs environment such as node.js/io.js, browserify/webpack etc
Where are you seeing the error with exports
? I see that message when using the Babel website to transpile it, but the code looks fine. While Underscore references the exports
variable, it first detects which environment it's in a only uses exports
if it finds itself running in a node environment.
Basically, that "error" might just be Babel trying too hard to be helpful.
Edited after more research
I just noticed... The actual last line of Underscore is to invoke itself with this
:
}.call(this));
However after built by Babel, the last line is changed to pass undefined
:
}.call(undefined));
It seems that in ES6 modules, this
is supposed to be undefined
(though I'm having a hard time processing the actual ECMA specification to determine if that is true) so Babel replaces it automatically.
StackOverflow: How to stop babel from transpiling 'this' to 'undefined'
Screenshot from Babel's online interpreter:
I think, this is fixed with this commit: https://github.com/jashkenas/underscore/commit/8a847ba69e521b3d7f4faaa6c7c039bcb8e6497b from @lexdene
however, there is no official release available containing this fix; the current version 1.8.3
does not contain it...
Most helpful comment
I'm getting "Cannot read property '_' of undefined", which is different but also broken.
Which version of Underscore are you using?
I'm suspicious that this is related to https://github.com/jashkenas/underscore/pull/2153 which has been fixed in master, but we have not yet shipped a release that includes it.
You could try it with the version of Underscore on master (https://github.com/jashkenas/underscore/blob/master/underscore.js) and see if that resolves the issue?
Let me know what you find.