更改nodejs require()获取文件的方式

时间:2013-05-31 23:24:42

标签: node.js

我正在寻找使用我自己的函数替换其文件加载的monkey-patch require()。我想内部需要(module_id)做类似的事情:

  1. 将module_id转换为文件路径
  2. 将文件路径加载为字符串
  3. 将字符串编译为模块对象并正确设置各种全局变量
  4. 我希望在不重新实现步骤1 + 3的情况下替换第2步。查看公共API,其中require()执行1 - 3,require.resolve()执行1.有没有办法隔离从第3步开始的第2步?

    我已经查看了需要模拟工具的来源,例如mockery - 他们似乎正在做的是用一个拦截某些调用并返回用户提供的对象的函数替换require(),并且传递对本机require()函数的其他调用。

    对于上下文,我正在尝试编写一个函数require_at_commit(module_id,git_commit_id),它会加载一个模块以及该模块在给定提交时所需的任何内容。

    我想要这个函数,因为我希望能够编写某些函数,a)依赖于我的代码库的各个部分,并且b)保证不会随着我的代码库的发展而改变。我想在不同的时间点“冻结”我的代码,所以认为这可能是一种避免必须打包我的代码库的20个副本的简单方法(另一种方法是使用“my_code_v1”:“git:// .. “在我的package.json中,但我觉得这样会有20个版本的臃肿和缓慢)。

    更新

    所以模块加载的源代码在这里:https://github.com/joyent/node/blob/master/lib/module.js。具体来说,要做这样的事情,你需要重新实现Module._load,这非常简单。但是,有一个更大的障碍,即第1步,将module_id转换为文件路径,实际上比我想象的更难,因为resolveFilename需要能够调用fs.exists()来知道在哪里终止其搜索...所以我不能只替换掉单个文件,我必须替换整个目录,这意味着将整个git修订版导出到目录并指向该目录下的require()可能更容易,而不是覆盖require()

    更新2:

    完全使用不同的方法......请参阅我在下面添加的答案

2 个答案:

答案 0 :(得分:1)

您可以使用require.extensions机制。这就是咖啡脚本coffee命令无需将.coffee文件写入磁盘即可加载.js文件的方式。

以下是它的工作原理:

https://github.com/jashkenas/coffee-script/blob/1.6.2/lib/coffee-script/coffee-script.js#L20

  loadFile = function(module, filename) {
    var raw, stripped;
    raw = fs.readFileSync(filename, 'utf8');
    stripped = raw.charCodeAt(0) === 0xFEFF ? raw.substring(1) : raw;
    return module._compile(compile(stripped, {
      filename: filename,
      literate: helpers.isLiterate(filename)
    }), filename);
  };

  if (require.extensions) {
    _ref = ['.coffee', '.litcoffee', '.md', '.coffee.md'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      ext = _ref[_i];
      require.extensions[ext] = loadFile;
    }
  }

基本上,假设你的模块有一组众所周知的扩展,你应该能够使用这个模式和文件名的函数模式,做你需要的任何加载/转换,然后返回一个对象,模块。

这可能或者可能不足以满足你的要求,但老实说从你的问题来看,你听起来像是远离其他编程世界的杂草(不要那么严厉,它只是我最初的反应。)

答案 1 :(得分:0)

所以我最终做的不是弄乱节点require()模块,而是将我需要的提交归档到一个文件夹。我的代码看起来像这样:

# commit_id is the commit we want
# (note that if we don't need the whole repository, 
# we can pass "commit_id path_to_folder_we_need")
#
# path is the path to the file you want to require starting from the repository root 
# (ie 'lib/module.coffee')
#
# cb is called with (err, loaded_module)
#
require_at_commit = (commit_id, path, cb) ->
    dir = 'old_versions' #make sure this is in .gitignore!
    dir += '/' + commit_id

    do_require = -> cb null, require dir + '/' + path

    if not fs.existsSync(dir)   
        fs.mkdirSync(dir)            
        cmd = 'git archive ' + commit_id  + ' | tar -x -C ' + dir
        child_process.exec cmd, (error) ->
            if error
                cb error
            else
                do_require()
    else
        do_require()
相关问题