| [9af201e] | 1 | # Pirates [![Coverage][codecov-badge]][codecov-link]
|
|---|
| 2 |
|
|---|
| 3 | ### Properly hijack require
|
|---|
| 4 |
|
|---|
| 5 | This library allows to add custom require hooks, which do not interfere with other require hooks.
|
|---|
| 6 |
|
|---|
| 7 | This library only works with commonJS.
|
|---|
| 8 |
|
|---|
| 9 | [codecov-badge]: https://img.shields.io/codecov/c/github/danez/pirates/master.svg?style=flat "codecov"
|
|---|
| 10 | [codecov-link]: https://codecov.io/gh/danez/pirates "codecov"
|
|---|
| 11 |
|
|---|
| 12 | ## Why?
|
|---|
| 13 |
|
|---|
| 14 | Two reasons:
|
|---|
| 15 | 1. Babel and istanbul were breaking each other.
|
|---|
| 16 | 2. Everyone seemed to re-invent the wheel on this, and everyone wanted a solution that was DRY, simple, easy to use,
|
|---|
| 17 | and made everything Just Work™, while allowing multiple require hooks, in a fashion similar to calling `super`.
|
|---|
| 18 |
|
|---|
| 19 | For some context, see [the Babel issue thread][] which started this all, then [the nyc issue thread][], where
|
|---|
| 20 | discussion was moved (as we began to discuss just using the code nyc had developed), and finally to [#1][issue-1]
|
|---|
| 21 | where discussion was finally moved.
|
|---|
| 22 |
|
|---|
| 23 | [the Babel issue thread]: https://github.com/babel/babel/pull/3062 "Babel Issue Thread"
|
|---|
| 24 | [the nyc issue thread]: https://github.com/bcoe/nyc/issues/70 "NYC Issue Thread"
|
|---|
| 25 | [issue-1]: https://github.com/danez/pirates/issues/1 "Issue #1"
|
|---|
| 26 |
|
|---|
| 27 | ## Installation
|
|---|
| 28 |
|
|---|
| 29 | npm install --save pirates
|
|---|
| 30 |
|
|---|
| 31 | ## Usage
|
|---|
| 32 |
|
|---|
| 33 | Using pirates is really easy:
|
|---|
| 34 | ```javascript
|
|---|
| 35 | // my-module/register.js
|
|---|
| 36 | const addHook = require('pirates').addHook;
|
|---|
| 37 | // Or if you use ES modules
|
|---|
| 38 | // import { addHook } from 'pirates';
|
|---|
| 39 |
|
|---|
| 40 | function matcher(filename) {
|
|---|
| 41 | // Here, you can inspect the filename to determine if it should be hooked or
|
|---|
| 42 | // not. Just return a truthy/falsey. Files in node_modules are automatically ignored,
|
|---|
| 43 | // unless otherwise specified in options (see below).
|
|---|
| 44 |
|
|---|
| 45 | // TODO: Implement your logic here
|
|---|
| 46 | return true;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | const revert = addHook(
|
|---|
| 50 | (code, filename) => code.replace('@@foo', 'console.log(\'foo\');'),
|
|---|
| 51 | { exts: ['.js'], matcher }
|
|---|
| 52 | );
|
|---|
| 53 |
|
|---|
| 54 | // And later, if you want to un-hook require, you can just do:
|
|---|
| 55 | revert();
|
|---|
| 56 | ```
|
|---|
| 57 |
|
|---|
| 58 | ## API
|
|---|
| 59 |
|
|---|
| 60 | ### pirates.addHook(hook, [opts={ [matcher: true], [exts: ['.js']], [ignoreNodeModules: true] }]);
|
|---|
| 61 | Add a require hook. `hook` must be a function that takes `(code, filename)`, and returns the modified code. `opts` is
|
|---|
| 62 | an optional options object. Available options are: `matcher`, which is a function that accepts a filename, and
|
|---|
| 63 | returns a truthy value if the file should be hooked (defaults to a function that always returns true), falsey if
|
|---|
| 64 | otherwise; `exts`, which is an array of extensions to hook, they should begin with `.` (defaults to `['.js']`);
|
|---|
| 65 | `ignoreNodeModules`, if true, any file in a `node_modules` folder wont be hooked (the matcher also wont be called),
|
|---|
| 66 | if false, then the matcher will be called for any files in `node_modules` (defaults to true).
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | ## Projects that use Pirates
|
|---|
| 70 |
|
|---|
| 71 | See the [wiki page](https://github.com/danez/pirates/wiki/Projects-using-Pirates). If you add Pirates to your project,
|
|---|
| 72 | (And you should! It works best if everyone uses it. Then we can have a happy world full of happy require hooks!), please
|
|---|
| 73 | add yourself to the wiki.
|
|---|