node.js - 配置节点将函数加载到全局范围内?

发布于 2024-12-21 01:49:03 字数 735 浏览 2 评论 0原文

在现实生活中,我在某个地方看到我们可以配置 Node-js 在全局范围内执行加载的模块,但我现在找不到如何做到这一点。

我为什么要问?

我有一些遗留文件定义了我想在服务器和客户端上使用的语言实用程序,但是其中许多实用程序被定义为全局范围函数。 例如,我有诸如 closure(fClosure)module(fModule) 之类的函数,以及更多以可读明确的方式简单地组织代码的函数,以及诸如 之类的实用程序$sb(arg,arg,arg),这是一个字符串生成器,等等。

现在,这些实用程序在 core.js 这样的文件中定义,并且该文件作为第一个依赖项加载到浏览器中,一切顺利。

但是,在根目录中要求此文件有助于扩展 Array.prototype 的位置,但其中定义的函数在其他模块中不可见。 (请避免讨论污染或与其他库冲突)

我知道这不符合 CommonJS 规范...但现在我只是尝试利用这些遗留代码,而不以 CommonJS 方式重新组织所有代码。

我还发现了 RequireJS 和它提出的漂亮的 AMD 模型,但它只回答了如何在为 Node.js 编写的浏览器代码上运行,而不是反之亦然。

分配给全局变量将不起作用,因为这意味着我必须重写所有遗留库。 我正在寻找一种方法,使它们在全局范围内运行,并将它们声明的所有内容保留在那里,而不需要重写它们。

那么,有没有办法让节点需要一个文件并在全局范围内运行它?

in realier days I saw somewhere that we can configure node-js to execute a loaded module in the global scope, I can't find how to do that now.

Why I'm asking?

I have some legacy files that define language utilities that I want to use on both the server and on the client, however many of these utilities are defined as global scope functions.
For example, I have functions like closure(fClosure), module(fModule), and many more that simply organize your code in readable definitive way, and utilities like $sb(arg,arg,arg), that is a string builder, and so on.

Now these utilities are defined in a file like core.js, and this file is loaded into the browser as first dependency, and life are good.

But, requiring this file in the root helps in places where it extends Array.prototype, but it functions that are defined in it are not visible in other modules.
(and please avoid the discussion of polluting or colliding with other libs)

I know it's not according to the CommonJS specifications... but now I'm just trying to leverage this legacy codes without reorganizing all the codes in a CommonJS way.

I aslo found about RequireJS and the beautiful AMD model it proposes, but it answers only on how to run on the browser codes that are written for node.js, and not vice-versia.

Assigning to the global variable will not work, because it means I have to rewrite all the legacy libraries.
I am looking for a way to make them run on the global scope and leave all what they declare there, without rewriting them.

So, is there a way to ask node to require a file and run it on the global scope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓酥 2024-12-28 01:49:03

您可以为 global 对象分配一些内容,对于 Node 来说就像 window 对于浏览器来说,例如:

test.js

global.my_var = require('./my_module');

require('./display_my_var');

my_module

module.exports = "this is a string";

display_my_var.js

console.log(my_var); // this will work, as my_var is now global

You can assign something to the global object, which for Node is like window is for the browser, example:

test.js

global.my_var = require('./my_module');

require('./display_my_var');

my_module

module.exports = "this is a string";

display_my_var.js

console.log(my_var); // this will work, as my_var is now global
深居我梦 2024-12-28 01:49:03

我们做了什么

这个答案绝不是一个很好的例子 - 如上所述 - 这种情况首先就不健康。

下面列出了我们对代码进行的最小更改。从中可以学到什么。

1 - 重构所有旧版 Web 代码以声明不带 var 的全局变量。

before:

var MyUtils = { ... };

window.some = value;

after:

MyUtils = { ... };

some = "value";

2 - 将所有全局函数声明为分配的全局变量

before:

function foo() { ... }

after:

foo = function() { ... }

3 -解决遗留代码和新节点代码之间的冲突

好吧,这个是私有的,所以没有关于它的片段。但这里是有关其他内容的片段:
您应该考虑的事情:

  • 属性增强为全局类的原型。如果你在任何一边这样做——现在两者都应该与之共存。愚蠢的示例 - add() - 类似于推送,但返回数组

    Array.prorotype.add = function(s){ this.push.apply(this,arguments);返回此 }

  • 缩小 - 我们必须解决一个没有很好缩小的地方 - 只需找到一种方法以不同的方式将其放入代码中,然后就可以了。

  • 单元测试 - 每个这样的全局都应该存在并被排除在全局范围的污染检查之外。一旦有了这个完整的列表 - 我们就再次开始使用它(现在它也得到了更好的记录:))

也值得一提

在搜索过程中,我们遇到了一些很酷的沙箱实用程序。
沙箱意味着在沙箱上下文中运行代码。这应该可以防止它到达其他范围。
其实现各不相同。
最赢得我赞赏的是: https://github.com/hflw/node-sandbox< /a>

它的工作原理是在子进程中运行“脏”代码,并帮助您连接“纯”代码和“脏遗留”代码之间的任何通信。

结果是完全分离。例如 - 子进程的 Array.prototype 不是父进程的 Array.prototype - 每个子进程都可以根据自己的“犯罪”操作需要对其进行扩充。 ..

就像我说的 - 我们从来没有绝望到如此需要它,因为节点代码是纯粹的,并且不使用任何内置类型的“扩展” - 他们不介意旧客户端代码的扩展。
但说实话 - 一旦代码混合 - 团队中的每个人都开始使用来自客户端代码的扩展,它就会变得混乱。

what we did

This answer is by no mean a good example - as stated above - the case is not healthy in the first place.

The minimal changes we got to apply to our code is listed bellow. Learn from it what you may.

1 - refactor all legacy web codes to declare globals without var.

before:

var MyUtils = { ... };

window.some = value;

after:

MyUtils = { ... };

some = "value";

2 - declare all global functions as assigned global var

before:

function foo() { ... }

after:

foo = function() { ... }

3 - resolve conflicts between legacy codes and new node codes

Well, this one is private, so no snippets about that. But here's snippets about other stuff:
Things you should consider:

  • properties augmented to prototypes of global classes. If you do it on any of the sides - both now should co-exist with it. Silly example - add() - like push, but returns the array

    Array.prorotype.add = function(s){ this.push.apply(this, arguments); return this }

  • minifications - we had to solve one place that did not minify well - just had to find a way to put it in code differently, and off with that.

  • Unit-testing - every such global should be expected to exist and be excluded from global-scope pollution checks. Once having this full list - we're off with it again (and now it's better documented too :) )

Also worths metnioning

Along the searches we met some cool sandboxing utilities.
Sandboxing means run the code in a sandbox context. that should prevent it from getting to other scopes.
Implementations of this varry.
The one that won my appreciation the most is this: https://github.com/hflw/node-sandbox

It works by running the 'dirty' code in a child process, and help you wire any communication between the 'pure' code and the 'dirty-legacy'.

The result is a complete separation. e.g - Array.prototype of the child process is not Array.prototype of the parent - each of them can augment it as it needs to for his own 'criminal' operations...

Like I said - we never got desperate enough to need it so bad, Since the node codes were pure, and do not use any 'extensions' to built-in types - they did not mind the extensions from old client codes.
But to be honest - once the codes were mixed - everybody in the team started use the extensions that came from the client codes, it becomes messy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文