简体   繁体   English

如何在node.js中并排使用CommonJS和AMD

[英]How to use CommonJS and AMD side by side in node.js

I've been researching CommonJs, AMD, module loading, and related issues for over a week. 我已经研究了CommonJs,AMD,模块加载和相关问题超过一周。 I feel like nothing out there does what I need. 我觉得什么都没有我需要的东西。 My basic need is to share code seamlessly between frontend and backend. 我的基本需求是在前端和后端之间无缝共享代码。 There are various issues around this including module formats for the client side, script loading, and module format conversions/wrapping. 围绕此问题存在各种问题,包括客户端的模块格式,脚本加载和模块格式转换/包装。 The piece I've been struggling with recently is how to use both CommonJS and AMD (or something AMD-like) in node.js. 我最近一直在努力的部分是如何在node.js中使用CommonJS和AMD(或类似AMD的东西)。

You can't get away from commonJs in node.js, so my thinking is that if I want to use AMD, it has to work alongside commonJs. 你无法摆脱node.js中的commonJs,所以我的想法是,如果我想使用AMD,它必须与commonJs一起工作。 What tools, libraries, or techniques can I use to get something AMD-like working? 我可以使用哪些工具,库或技术来获得类似AMD的工作?

For example, I would like to be able to write a module like this: 例如,我希望能够编写这样的模块:

var x = require('x')

modules.exports = function(a, callback) {
  if(a) {
     require(['y','z'], function(y,z) {
       callback(x, y.o + z.k)
     }
  } else {
    callback(x, "ok")
  }
}

Ideally: 理想的情况是:

  • Both node.js and the amd-like modules will have paths interpreted in the node.js way (paying attention to node_modules unless the module path starts with "/", "./", or "../") node.js和类似amd的模块都将以node.js方式解释路径(除非模块路径以“/”,“./”或“../”开头,否则请注意node_modules)
  • doesn't require source conversion for the server side in a build step (ie modules will run in node.js without each one being programmatically converted) 在构建步骤中不需要服务器端的源转换(即模块将在node.js中运行,而不是每个都以编程方式转换)
  • module or require don't need to be explicitly passed into the amd-like require function modulerequire不需要显式传递给类似amd的require函数

uRequire is the perfect tool for this requirement, it's all about interoperability between the module formats and their incompatibilities. uRequire是满足此要求的完美工具,它与模块格式之间的互操作性及其不兼容性有关。

Essentially uRequire converts or translates modules from nodejs to AMD and vise versa, plus the UMD format that runs on both nodejs and the browser or a combined .is that requires no AMD loader on browser. 本质上,uRequire将模块从nodejs转换或转换为AMD,反之亦然,加上在nodejs和浏览器上运行的UMD格式,或者在浏览器上不需要AMD加载器的组合。

It will require a build step though, but that is a minor concern in contrast to the offering. 虽然这需要一个构建步骤,但与产品相比,这是一个小问题。

You could check out, http://dojotoolkit.org/documentation/tutorials/1.9/node/ I've only played with it a little, but has worked with what I've tried. 你可以查看, http://dojotoolkit.org/documentation/tutorials/1.9/node/我只玩了一点,但是我已经尝试过了。 I got it working with node-orm and remember that being a pain to get going, but might of just been me making a mess while playing with it. 我得到它与node-orm一起工作,并记住这是一个痛苦的开始,但可能是我在玩它时弄得一团糟。

Essentially you end up with AMD on the server, like: 基本上你最终在服务器上使用AMD,例如:

require(["dojo/node!orm","other/amd/module"], function(orm){
    //use third party commonjs module and your own amd modules here
}

It looks like you've already investigated Requirejs's suggestion to wrap commonjs modules in an AMD require (automatically during build most likely using r.js). 看起来你已经调查过Requirejs建议将commonjs模块包装在AMD需求中(在构建过程中最有可能使用r.js)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM