5 ms·
A technical question: how do they get require()ing .sjs files to work, as opposed to compiling and require()ing the output? Is there some way to add hooks that
by gliese1337 14y ago
A technical question: how do they get require()ing .sjs files to work, as opposed to compiling and require()ing the output? Is there some way to add hooks that change the behavior of require() to insert a compilation step?
- romac 14y agoThere is indeed. Take a look at require.extensions: http://nodejs.org/api/globals.html#globals_require_extensions http://nodejs.org/api/globals.html#globals_require_extension...
- gliese1337 14y agoNeat! With a little bit of googling, it seems CoffeeScript can do the same thing. That lowers the barrier-to-usage of compile-to-js languages enormously! Now I'm looking at the sweet.js source where they register the extension, and there's some additional magic I don't recognize; is module._compile documented somewhere that I just can't find, or is that getting added somewhere in the sweet.js code?
- grncdr 14y agoI don't think it is. I seem to recall Isaac saying that he considers require.extensions to be a mistake, though I don't know if he meant the concept (extensibility) or implementation (register hooks under filename extensions). Either way, I think it's recognized that a lot of people enjoy and use the functionality so it's unlikely to go away. As an aside, here's the relevant bit of code that loads the (already compiled) source as a module: https://github.com/joyent/node/blob/master/lib/module.js#L404 https://github.com/joyent/node/blob/master/lib/module.js#L40...