海盗 
适当劫持需要
这个库允许添加自定义需要的钩子,不会干扰其他需要的钩子.
这个图书馆只与普通JS合作.
为什么?
两个原因:
- Babel和Istanbul在互相残杀.
- 每个人似乎都重新发明了这个车轮, 每个人都想有一个解决办法 是DRY,简单,容易使用,
和做一切 Just WorkTM, 同时允许多个需要钩, 类似呼叫的方式
super。 。 。 .
关于某些背景,见 babel 问题线索 开始这一切,然后 尼克问题线索时 我们开始讨论刚刚开发的nyc代码 1号 讨论终于被搬走了.
安装
npm install --save pirates
使用量
利用海盗很容易:
// my-module/register.js
const addHook = require('pirates').addHook;
// Or if you use ES modules
// import { addHook } from 'pirates';
function matcher(filename) {
// Here, you can inspect the filename to determine if it should be hooked or
// not. Just return a truthy/falsey. Files in node_modules are automatically ignored,
// unless otherwise specified in options (see below).
// TODO: Implement your logic here
return true;
}
const revert = addHook(
(code, filename) => code.replace('@@foo', 'console.log(\'foo\');'),
{ exts: ['.js'], matcher }
);
// And later, if you want to un-hook require, you can just do:
revert();
API 密码
海盗.addHook(hook, [opts={ [macher: true], [exts: ['.js']], [ignoreNodeModules: true]];
添加需要的钩. hook 必须是需要 (code, filename),并返回修改后的代码. opts 实值
选项对象。 可用的选项有: matcher,这是一个接受文件名的函数,以及
返回一个真实值,如果文件被锁定(默认为总是返回真实的函数),如果是虚假
否则; exts,这是钩线的扩展数组,它们应该从 . (违约) ['.js'](三);
ignoreNodeModules如果真实, 任何文件 node_modules 文件夹不会被锁定( 匹配者也不会被调用) ,
如果错误, 那么匹配者会被调用任何文件 node_modules (默认为真实).
使用海盗的项目
见 维基页面如果你把海盗加入你的计划, (你应该! ) 如果每个人都用它,它最好。 然后我们可以有一个充满快乐的世界 需要钩子,请 加入维基语录.