node.js-配置节点以将函数加载到全局作用域? [英] node.js - configure node to load functions into the global scope?

查看:236
本文介绍了node.js-配置节点以将函数加载到全局作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更现实的日子里,我看到某个地方可以配置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.

我为什么要问?

我有一些旧文件,这些文件定义了我想在服务器和客户端上都使用的语言实用程序,但是其中许多实用程序都定义为全局范围函数. 例如,我有closure(fClosure)module(fModule)之类的功能,还有许多其他功能,它们以可读的确定性方式简单地组织代码,还有诸如$sb(arg,arg,arg)的实用程序(它是字符串生成器)之类的.

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.

现在,这些实用程序在类似core.js的文件中定义,并且该文件作为第一个依赖项加载到浏览器中,使用寿命长.

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.

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

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)

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

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.

我也发现了RequireJS及其提出的漂亮的AMD模型,但它仅回答了如何在为node.js编写的浏览器代码上运行,反之亦然.

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?

推荐答案

我们做了什么

这个答案绝不是一个好例子-如上所述-这种情况一开始就不健康.

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-重构所有旧版Web代码以声明不带var的全局变量.

之前:

var MyUtils = { ... };

window.some = value;

之后:

MyUtils = { ... };

some = "value";

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

之前:

function foo() { ... }

之后:

foo = function() { ... }

3-解决旧代码与新节点代码之间的冲突

好吧,这个是私人的,所以没有关于它的摘录.但是以下是有关其他内容的摘录: 您应该考虑的事情:

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

  • 属性扩充为全局类的原型.如果您从任何方面进行操作-现在两者都应该与之共存.愚蠢的例子-add()-类似于推,但返回数组

  • 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.

在搜索中,我们遇到了一些很棒的沙盒工具. 沙箱意味着在沙箱上下文中运行代码.那应该阻止它进入其他范围. 此版本的实现. 最令我赞赏的是: https://github.com/hflw/node-sandbox

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'.

结果是 完整 分离.例如-子进程的Array.prototype并非父进程的Array.prototype-每个子进程都可以根据需要对其自身的犯罪"操作进行扩充...

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.

这篇关于node.js-配置节点以将函数加载到全局作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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