michaelficarra / commonjs-everywhere

:rainbow: minimal CommonJS browser bundler with aliasing, extensibility, and source maps
BSD 3-Clause "New" or "Revised" License
158 stars 21 forks source link

CLI Option for Extension Handlers #64

Closed EndangeredMassa closed 11 years ago

EndangeredMassa commented 11 years ago

It looks like the file watcher is only available on the CLI. The problem is that I need custom extension handler for my project.

Is there a way to add this to the CLI? I can't think of a great interface for it, but something like -h extension:path-to-file might be useful enough. That file would be a module that exports a function that acts just like the handlers object callbacks.

module.exports = (body, filename) ->
  body = body.toString()
  compiled = body.replace(/\"/g, "\\\"").replace(/\n/g, '\\\n')                                                                                                                 
  require('esprima').parse("module.exports = \"#{compiled}\";")

Then, if you allow lists in some way, I could use the same transform method for multiple extensions.

It seems like a complex option to pass on a command line, but it would be very useful to me.

michaelficarra commented 11 years ago

This sounds good. We can rip off mocha here. They use an option with extension:module, where the module adds require.extensions support for that extension. Then, since it's a global mutation, we can just pull that function out of our environment. Like #28, this will rely on michaelficarra/jedediah#1.

michaelficarra commented 11 years ago

Unfortunately, we can't just use require.extensions here, since the function you put on require.extensions just calls the .compile method of its first argument, and doesn't have a guaranteed return value. Since we have a sync interface, there's nothing we can do with this. Going with your original suggestion.