why are you throwing if CJS is detected?
jonathanong opened this issue · 5 comments
https://github.com/yahoo/js-module-formats/blob/master/index.js#L81
not sure what's the point of throwing o.O
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:
- it might require relative path modules, and since we are executing a blob, we don't know about filesystem, therefore if will fail.
- it requires modules that are suppose to be installed and available in the path to the actual file (which we don't know, same as above), even if I use
process.cwd()there is not guarantee that it will work. - it might actually do things to modify the environment or modify a submodule member.
Ah ok. This module might be too crazy for me haha. I just want to know if something is es6 without executing code
@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.
You could split it up into two modules. One basically just does regexp checks, then this one executes the code as well.