source: frontend/node_modules/pirates/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.9 KB
Line 
1# Pirates [![Coverage][codecov-badge]][codecov-link]
2
3### Properly hijack require
4
5This library allows to add custom require hooks, which do not interfere with other require hooks.
6
7This 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
14Two reasons:
151. Babel and istanbul were breaking each other.
162. Everyone seemed to re-invent the wheel on this, and everyone wanted a solution that was DRY, simple, easy to use,
17and made everything Just Work™, while allowing multiple require hooks, in a fashion similar to calling `super`.
18
19For some context, see [the Babel issue thread][] which started this all, then [the nyc issue thread][], where
20discussion was moved (as we began to discuss just using the code nyc had developed), and finally to [#1][issue-1]
21where 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
33Using pirates is really easy:
34```javascript
35// my-module/register.js
36const addHook = require('pirates').addHook;
37// Or if you use ES modules
38// import { addHook } from 'pirates';
39
40function 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
49const 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:
55revert();
56```
57
58## API
59
60### pirates.addHook(hook, [opts={ [matcher: true], [exts: ['.js']], [ignoreNodeModules: true] }]);
61Add a require hook. `hook` must be a function that takes `(code, filename)`, and returns the modified code. `opts` is
62an optional options object. Available options are: `matcher`, which is a function that accepts a filename, and
63returns a truthy value if the file should be hooked (defaults to a function that always returns true), falsey if
64otherwise; `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),
66if false, then the matcher will be called for any files in `node_modules` (defaults to true).
67
68
69## Projects that use Pirates
70
71See 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
73add yourself to the wiki.
Note: See TracBrowser for help on using the repository browser.