Debian Codegraph @

pirates/README.md

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