将 requirejs 与节点一起使用
using requirejs with node
我目前正在处理服务器端 Node 项目。虽然节点有它自己的使用 CommonJS 的模块加载器,但我正在评估是否将 RequireJS 与它一起使用。虽然如果应用程序具有某些客户端方面的应用程序,则将 RequireJS 与 Node 一起使用有优势,但对于完全是服务器端的项目,我找不到任何好处。
是否普遍认为对于 100% 服务器端的 Node 项目,合并 RequireJS 并没有真正的优势?
我从来没有找到在节点中使用 RequireJS
的好理由。然而,RequireJS can be run on node,并且文档有一个简短的解释为什么你可能想要 运行 RequireJS
在节点服务器上:
By using RequireJS on the server, you can use one format for all your
modules, whether they are running server side or in the browser. That
way you can preserve the speed benefits and easy debugging you get
with RequireJS in the browser, and not have to worry about extra
translation costs for moving between two formats.
其中大部分仅在也具有客户端元素的项目中有用。如果是我,我会坚持 Node's internal module loader。
在服务器端使用 RequireJS 是有原因的,但很少。除非你能说明原因,例如:
I must use RequireJS because X
其中 "X" 是证明使用 RequireJS 的理由,那么你不应该这样做。
请注意,仅仅想以 AMD 格式编写模块是不够的,因为存在允许在 Node.js 中加载 AMD 模块的加载器(如 node-amdl-loader)。当我想测试不依赖于浏览器的代码时,我实际上会使用它。我以 AMD 格式编写模块,并指定如果在 Node 中使用,则应使用像 node-amd-loader 这样的 AMD 加载器。这样库在 Node 和浏览器中都可以工作,但我在 Node 中测试它。
使用 RequireJS 服务器端的一个原因是,例如,如果您需要 运行 需要 DOM 实现的代码。如果你使用jsdom之类的东西来提供DOM并且你要加载的代码是一系列AMD模块,那么使用RequireJS将代码加载到jsdom创建的DOM环境中有道理。
我目前正在处理服务器端 Node 项目。虽然节点有它自己的使用 CommonJS 的模块加载器,但我正在评估是否将 RequireJS 与它一起使用。虽然如果应用程序具有某些客户端方面的应用程序,则将 RequireJS 与 Node 一起使用有优势,但对于完全是服务器端的项目,我找不到任何好处。
是否普遍认为对于 100% 服务器端的 Node 项目,合并 RequireJS 并没有真正的优势?
我从来没有找到在节点中使用 RequireJS
的好理由。然而,RequireJS can be run on node,并且文档有一个简短的解释为什么你可能想要 运行 RequireJS
在节点服务器上:
By using RequireJS on the server, you can use one format for all your modules, whether they are running server side or in the browser. That way you can preserve the speed benefits and easy debugging you get with RequireJS in the browser, and not have to worry about extra translation costs for moving between two formats.
其中大部分仅在也具有客户端元素的项目中有用。如果是我,我会坚持 Node's internal module loader。
在服务器端使用 RequireJS 是有原因的,但很少。除非你能说明原因,例如:
I must use RequireJS because X
其中 "X" 是证明使用 RequireJS 的理由,那么你不应该这样做。
请注意,仅仅想以 AMD 格式编写模块是不够的,因为存在允许在 Node.js 中加载 AMD 模块的加载器(如 node-amdl-loader)。当我想测试不依赖于浏览器的代码时,我实际上会使用它。我以 AMD 格式编写模块,并指定如果在 Node 中使用,则应使用像 node-amd-loader 这样的 AMD 加载器。这样库在 Node 和浏览器中都可以工作,但我在 Node 中测试它。
使用 RequireJS 服务器端的一个原因是,例如,如果您需要 运行 需要 DOM 实现的代码。如果你使用jsdom之类的东西来提供DOM并且你要加载的代码是一系列AMD模块,那么使用RequireJS将代码加载到jsdom创建的DOM环境中有道理。