如何更改 Node.js 模块包装器? [英] How to change the Node.js module wrapper?

查看:59
本文介绍了如何更改 Node.js 模块包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于测试目的,我需要更改 Node.js Module 包装器.

(function (exports, require, module, __filename, __dirname, process, global) {调试器;});

玩弄Module我发现

var Module = require("module")模块包装器->["(function (exports, require, module, __filename, __dirname, process, global) { ", "});"]模块包装->功能(脚本){返回 NativeModule.wrapper[0] + 脚本 + NativeModule.wrapper[1];}

是否可以钩入 Module.wraper 或属性来更改脚本包装?

解决方案

通过找到 Module.wrap 函数,您做得很好.简单地覆盖它怎么样?

假设您有两个文件,main.jsmodule.js.在 main.js 中,您覆盖 Module.wrap 函数以便 console.log('debug'); 每次需要模块时.然后你需要 module.js,它包含一个 hello world 消息.

main.js:

var Module = require("module");(功能(模块包装复制){Module.wrap = 函数(脚本){script = "console.log('debug');"+ 脚本返回 moduleWrapCopy(脚本);//调用原始包装函数};}(Module.wrap));//将原始函数传递给 IIFE要求(./module.js");

module.js:

console.log("Hello world from module.js!");

执行 node main.js 结果:

<块引用>

调试来自 module.js 的你好世界!

<小时>

这也适用于嵌套的 require() 调用,例如,如果您在 module.js 中使用 require("./module2.js")代码>:

module.js:

console.log("Hello world from module.js!");要求(./module2.js");

module2.js:

console.log("Hello world from module2.js!");

在这种情况下 node main.js 产生:

<块引用>

调试来自 module.js 的你好世界!调试来自 module2.js 的世界你好!

在 Node.js 4.3.0 和 6.1.0 上测试.

For testing purposes I need to change the Node.js Module wrapper.

(function (exports, require, module, __filename, __dirname, process, global) {  
    debugger;
 });

Played around with Module I found

var Module = require("module")
Module.wrapper
-> ["(function (exports, require, module, __filename, __dirname, process, global) { ", "
});"]

Module.wrap
-> function(script) {
    return NativeModule.wrapper[0] + script + NativeModule.wrapper[1];
}

Is it possible to hook into Module.wraper or property to change the script wrapping?

解决方案

You've done a great job by finding the Module.wrap function. How about simply overwriting it?

Imagine that you have two files, main.js and module.js. In main.js you overwrite the Module.wrap function in order to console.log('debug'); every time a module is required. Then you require module.js, which contains a hello world message.

main.js:

var Module = require("module");

(function(moduleWrapCopy) {
  Module.wrap = function(script) {
    script = "console.log('debug');" + script
    return moduleWrapCopy(script); // Call original wrapper function
  };
}(Module.wrap)); // Pass original function to IIFE

require("./module.js");

module.js:

console.log("Hello world from module.js!");

Executing node main.js results in:

debug
Hello world from module.js!


This also works with nested require() calls, for example if you require("./module2.js") in module.js:

module.js:

console.log("Hello world from module.js!");

require("./module2.js");

module2.js:

console.log("Hello world from module2.js!");

In this case node main.js produces:

debug
Hello world from module.js!
debug
Hello world from module2.js!

Tested on Node.js 4.3.0 and 6.1.0.

这篇关于如何更改 Node.js 模块包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆