danez / pirates

Properly hijack require

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Initial Discussion

ariporad opened this issue · comments

Hi All,
Just continuing istanbuljs/nyc#70 and babel/babel#3062.

To recap: I will be taking ideas from istanbul's and nyc's require-hook implementations, and trying to make something that:

  1. Super easy require-hook injection, with a super simple API that Just Works(tm), feeding hooks into each other in the order they are added. API concept:

    var pirates = require('pirates');
    
    // ...
    
    pirates.addCompiler('.js', function (filename) {
       return filename.indexOf('foo') !== -1;
    }, function (module, filename) {
        return this.addFile(module, filename).content; // Just return the new source for the module.
    });
  2. Using getters/setters, automatic fixing of hooks that don't use pirates, and still passing all requires through the hooks in order. API concept:

    require('pirates/register');
    // For some weird reason, part of my app is in coffee script, and some is in ES6.
    require('coffee-script/register');
    require('babel-core/regsister');
    require('some-bad-module/register');
  3. ???

Discuss.

cc @bcoe, @gotwarlost, @stefanpenner, @jamestalmage, @sebmck

Ok everyone, I got pirates mostly done! It's now available on npm, and I've opened pull requests for babel (babel/babel#3139) and istanbul (istanbuljs-archived-repos/istanbul-lib-hook#5), and a PR for nyc is in the works. If you have any other modules you like that use require hooks, convince them to adopt pirates! It's super easy and reduces complexity!

After looking into having getters and setters for dealing with naughty require hooks, I ended up dropping that idea (for now), because basically any hook that misbehaves is using Module.prototype._compile instead of module._compile, which I haven't been able to figure out how to fix.

Since Pirates@1.0.0 has been released, I'm going to close this.

Enjoy!