formatjs / js-module-formats

Detect different types of javascript modules formats
Other
13 stars 7 forks source link

why are you throwing if CJS is detected? #8

Closed jonathanong closed 10 years ago

jonathanong commented 10 years ago

https://github.com/yahoo/js-module-formats/blob/master/index.js#L81

not sure what's the point of throwing o.O

caridy commented 10 years ago

It is a controlled throw. As you can see in line 78-80 we are pushing the type of module into the result array, and successive, we throw an error that is controlled thru a try/catch.

The reason why I decided to throw instead of letting the modules to be executed is because we are executing the code in a new context with new globals to avoid polluting the current runtime (where you just really want to detect the type). In the case of the require statement, there are problems that I want to control:

jonathanong commented 10 years ago

Ah ok. This module might be too crazy for me haha. I just want to know if something is es6 without executing code

caridy commented 10 years ago

@jonathanong I have the exact same discussion with @ericf, I think for now I managed to convince him to stick to the current implementation. I think it is irrelevant if we execute the code or not since it happens in a sandbox.

jonathanong commented 10 years ago

You could split it up into two modules. One basically just does regexp checks, then this one executes the code as well.

caridy commented 10 years ago

yes, that was the center of the discussion between @ericf and myself, so it is on the table, let see if the time comes.