加载中...

CommonJS


CommonJS

为服务器提供的一种模块形式的优化

CommonJS模块建议指定一个简单的用于声明模块服务器端的API,并且不像AMD那样尝试去广泛的操心诸如io,文件系统,约定以及更多的一揽子问题。

这种形式为CommonJS所建议--它是一个把目标定在设计,原型化和标准化Javascript API的自愿者工作组。迄今为止,他们已经在模块和包方面做出了批复标准的尝试。

入门

从架构的角度来看,CommonJS模块是一个可以复用的Javascript块,它出口对任何独立代码都起作用的特定对象。不同于AMD,通常没有针对此模块的功能封装(因此打个比方我们并没有在这里找到定义的相关语句)。

CommonJS模块基本上包括两个基础的部分:一个取名为exports的自由变量,它包含模块希望提供给其他模块的对象,以及模块所需要的可以用来引入和导出其它模块的函数。

理解CommonJS:require()和exports

  1. // package/lib is a dependency we require
  2. var lib = require( "package/lib" );
  3. // behaviour for our module
  4. function foo(){
  5. lib.log( "hello world!" );
  6. }
  7. // export (expose) foo to other modules
  8. exports.foo = foo;

exports的基础使用

  1. // define more behaviour we would like to expose
  2. function foobar(){
  3. this.foo = function(){
  4. console.log( "Hello foo" );
  5. }
  6. this.bar = function(){
  7. console.log( "Hello bar" );
  8. }
  9. }
  10. // expose foobar to other modules
  11. exports.foobar = foobar;
  12. // an application consuming "foobar"
  13. // access the module relative to the path
  14. // where both usage and module files exist
  15. // in the same directory
  16. var foobar = require("./foobar").foobar,
  17. test = new foobar();
  18. // Outputs: "Hello bar"
  19. test.bar();

等同于AMD的第一个CommonJS示例

  1. define(function(require){
  2. var lib = require( "package/lib" );
  3. // some behaviour for our module
  4. function foo(){
  5. lib.log( "hello world!" );
  6. }
  7. // export (expose) foo for other modules
  8. return {
  9. foobar: foo
  10. };
  11. });

这也可以用AMD支持的简化了的CommonJS特定做到。

消耗多重依赖

app.js

  1. var modA = require( "./foo" );
  2. var modB = require( "./bar" );
  3. exports.app = function(){
  4. console.log( "Im an application!" );
  5. }
  6. exports.foo = function(){
  7. return modA.helloWorld();
  8. }

bar.js

  1. exports.name = "bar";

foo.js

  1. require( "./bar" );
  2. exports.helloWorld = function(){
  3. return "Hello World!!"
  4. }

加载器和框架对CommonJS提供了什么支持?

在浏览器端:

服务器端:

CommonJS适合浏览器么?

有开发者感觉CommonJS更适合于服务器端的开发,这是如今应该用哪种形式和将要来作为面向未来的备选事实标准,在这一问题上存在一定程度分歧的原因之一。一些争论指摘CommonJS包括许多面向服务器的特性,这些特性很容易可以看出并不能够用Javascript在浏览器级别中实现--例如,io,系统,而且js会被认为是借助于它们功能的性质无法实现的。

那就是说,无论如何了解如何构建CommonJS模块是有用的,那样我们就可以更好的理解它们如何适合于定义可以在任何地方使用的模块了。模块在客户端和服务器端都有包括验证,约定和模板引擎的应用程序。一些开发者趋向于选择那种形式的方式是当一个模块能够在服务器端环境使用时,就选择CommonJS,而如果不是这种场景,就使用AMD。

由于AMD模块具有使用插件的能力,并且能够定义更加精细的像构造器和函数之类的东西,如此是有道理的。

CommonJS模块只能够去定义使用起来会非常繁琐的对象,如果我们尝试从它们那里获取构造器的话。

尽管这超出了本节的讨论范畴,也要注意当论及AMD和CommonJS时,不同类型的“require”方法会被提到。带有类似命名空间的问题理所当然是令人迷惑的,而社区当前对全局的require功能的优点正存在着分歧。这里John Hann的建议是不去叫它“require”,它很可能在告知用户关于全局的和内部的require之间的差别,这一目标上取得失败,将全局加载器方法重新命名为其它什么东西(例如,库的名字)可能更加起作用。正式由于这个原因,像curl.js这样的加载器反对使用require,而使用curl()。

AMD 与 CommonJS 存在竞争,但都是同样有效的标准

AMD 和 CommonJS 都是有效的模块形式,它们带有不同的最终目标。

AMD采用浏览器先行的方针,它选择了异步的行为方式,并且简化了向后兼容性,但是它并没有任何文件I/O的概念。它支持对象,函数,构造器,字符串,JSON以及许多其它类型的模块,在浏览器进行本地运行。这是令人难以置信的灵活性。

CommonJS 则在另一个方面采用了服务器端先行的方针,承载着同步行为,没有全局的负担并且尝试去迎合(在服务器上的)未来。我们的意思是CommonJS支持无封装的模块,可以感觉到它跟ES.next/Harmony更接近一点,将我们从AMD强制使用的define()封装中解放出来。然而CommonJS仅支持对象作为模块。

UMD:AMD和兼容CommonJS模块的插件

对于希望创建在浏览器和服务器端环境都能够运作的模块的开发者而言,现有的解决方案感觉可能少了点。为了有助于缓解这个问题,James Burke , 我以及许许多多其他的开发者创造了UMD(通用模块定义)

UMD是一种是实验性质的模块形式,允许在编写代码的时候,所有或者大多数流行的实用脚本加载技术对模块的定义在客户端和服务器环境下都能够起作用。另外一种模块格式的想法尽管可能是艰巨的,出于仔细彻底的考虑,我们将简要的概括一下UMD。最开始,我们通过简要的看一看AMD规范中所支持的对于CommonJS的简单封装,来定义UMD。对于希望把模块当做CommonJS模块来编写的开发者,可以应用下面的兼容CommonJS的形式:

基础的AMD混合格式:

  1. define( function ( require, exports, module ){
  2. var shuffler = require( "lib/shuffle" );
  3. exports.randomize = function( input ){
  4. return shuffler.shuffle( input );
  5. }
  6. });

然而,注意到如果一个模块并没有包含一个依赖数组,并且定义的函数只包含最少的一个参数,那么它就真的仅仅只是被当做CommonJS模块来对待,这一点是很重要的。这在某些设备(例如PS3)上面也不会正确的工作。如需进一步了解上述的封装,请看看:http://requirejs.org/docs/api.html#cjsmodule

进一步的考虑,我们想要提供许多不同的模式,那不仅仅只是在AMD和CommonJS上起作用,同样也能解决开发者希望使用其它环境开发这样的模块时普遍遇到的问题。

下面我们可以看到这样的变化允许我们使用CommonJS,AMD或者浏览全局的对象创建一个模块。

使用 CommonJS,AMD或者浏览器全局对象创建模块

定义一个模块 commonJsStrict,它依赖于另外一个叫做B的模块。模块的名称暗示了文件的名称(,就是说一样的),而让文件名和导出的全局对象的名字一样则是一种最佳实践。

如果模块同时也在浏览器中使用了相同类型的样板,它就会创建一个global.b备用。如果我们不希望对浏览器全局补丁进行支持, 我们可以将root移除,并且把this传递到顶层函数作为其第一个参数。

  1. (function ( root, factory ) {
  2. if ( typeof exports === 'object' ) {
  3. // CommonJS
  4. factory( exports, require('b') );
  5. } else if ( typeof define === 'function' && define.amd ) {
  6. // AMD. Register as an anonymous module.
  7. define( ['exports', 'b'], factory);
  8. } else {
  9. // Browser globals
  10. factory( (root.commonJsStrict = {}), root.b );
  11. }
  12. }(this, function ( exports, b ) {
  13. //use b in some fashion.
  14. // attach properties to the exports object to define
  15. // the exported module properties.
  16. exports.action = function () {};
  17. }));

UMD资源库包含了在浏览器中能够最优化运作的涵盖不同的模块,那些对于提供导出非常不错的,那些对于CommonJS的优化还有那些对于定义jQuery插件作用良好的,我们会在接下里看得到。

可以在所有环境下面起作用的jQuery插件

UMD提供了两种同jQuery一起工作的模式--一种模式定义了能够同AMD和浏览器全局对象一起工作得很好的插件,而另外一种模式也能够在CommonJS环境中起作用。jQuery并不像是能够运行在大多数CommonJS环境中的,因此除非我们工作在一个能够良好同jQuery一起运作的环境中,那就把这一点牢记于心。

现在我们将定义一个包含一个核心,以及对此核心的一个扩展的插件。核心插件被加载到一个$.core命名空间中,它可以简单的使用借助于命名空间模式的插件扩展进行扩展。通过脚本标签加载的插件会自动填充core下面的一个插件命名空间(比如,$core.plugin.methodName())。

这种模式操作起来相当的棒,因为插件扩展可以访问到底层定义的属性和方法,或者,做一些小小的调整就可以重写行为以便它能够被扩展来做更多的事情。加载器同样也不在需要面面俱到了。

想要了解更多需要做的详细信息,那就请看看下面代码示例中内嵌的注释吧:

usage.html

  1. <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
  2. <script type="text/javascript" src="pluginCore.js"></script>
  3. <script type="text/javascript" src="pluginExtension.js"></script>
  4. <script type="text/javascript">
  5. $(function(){
  6. // Our plugin "core" is exposed under a core namespace in
  7. // this example, which we first cache
  8. var core = $.core;
  9. // Then use use some of the built-in core functionality to
  10. // highlight all divs in the page yellow
  11. core.highlightAll();
  12. // Access the plugins (extensions) loaded into the "plugin"
  13. // namespace of our core module:
  14. // Set the first div in the page to have a green background.
  15. core.plugin.setGreen( "div:first");
  16. // Here we're making use of the core's "highlight" method
  17. // under the hood from a plugin loaded in after it
  18. // Set the last div to the "errorColor" property defined in
  19. // our core module/plugin. If we review the code further down,
  20. // we can see how easy it is to consume properties and methods
  21. // between the core and other plugins
  22. core.plugin.setRed("div:last");
  23. });
  24. </script>

pluginCore.js

  1. // Module/Plugin core
  2. // Note: the wrapper code we see around the module is what enables
  3. // us to support multiple module formats and specifications by
  4. // mapping the arguments defined to what a specific format expects
  5. // to be present. Our actual module functionality is defined lower
  6. // down, where a named module and exports are demonstrated.
  7. //
  8. // Note that dependencies can just as easily be declared if required
  9. // and should work as demonstrated earlier with the AMD module examples.
  10. (function ( name, definition ){
  11. var theModule = definition(),
  12. // this is considered "safe":
  13. hasDefine = typeof define === "function" && define.amd,
  14. // hasDefine = typeof define === "function",
  15. hasExports = typeof module !== "undefined" && module.exports;
  16. if ( hasDefine ){ // AMD Module
  17. define(theModule);
  18. } else if ( hasExports ) { // Node.js Module
  19. module.exports = theModule;
  20. } else { // Assign to common namespaces or simply the global object (window)
  21. ( this.jQuery || this.ender || this.$ || this)[name] = theModule;
  22. }
  23. })( "core", function () {
  24. var module = this;
  25. module.plugins = [];
  26. module.highlightColor = "yellow";
  27. module.errorColor = "red";
  28. // define the core module here and return the public API
  29. // This is the highlight method used by the core highlightAll()
  30. // method and all of the plugins highlighting elements different
  31. // colors
  32. module.highlight = function( el,strColor ){
  33. if( this.jQuery ){
  34. jQuery(el).css( "background", strColor );
  35. }
  36. }
  37. return {
  38. highlightAll:function(){
  39. module.highlight("div", module.highlightColor);
  40. }
  41. };
  42. });

pluginExtension.js

  1. // Extension to module core
  2. (function ( name, definition ) {
  3. var theModule = definition(),
  4. hasDefine = typeof define === "function",
  5. hasExports = typeof module !== "undefined" && module.exports;
  6. if ( hasDefine ) { // AMD Module
  7. define(theModule);
  8. } else if ( hasExports ) { // Node.js Module
  9. module.exports = theModule;
  10. } else {
  11. // Assign to common namespaces or simply the global object (window)
  12. // account for for flat-file/global module extensions
  13. var obj = null,
  14. namespaces,
  15. scope;
  16. obj = null;
  17. namespaces = name.split(".");
  18. scope = ( this.jQuery || this.ender || this.$ || this );
  19. for ( var i = 0; i < namespaces.length; i++ ) {
  20. var packageName = namespaces[i];
  21. if ( obj && i == namespaces.length - 1 ) {
  22. obj[packageName] = theModule;
  23. } else if ( typeof scope[packageName] === "undefined" ) {
  24. scope[packageName] = {};
  25. }
  26. obj = scope[packageName];
  27. }
  28. }
  29. })( "core.plugin" , function () {
  30. // Define our module here and return the public API.
  31. // This code could be easily adapted with the core to
  32. // allow for methods that overwrite and extend core functionality
  33. // in order to expand the highlight method to do more if we wish.
  34. return {
  35. setGreen: function ( el ) {
  36. highlight(el, "green");
  37. },
  38. setRed: function ( el ) {
  39. highlight(el, errorColor);
  40. }
  41. };
  42. });

UMD并不企图取代AMD或者CommonJS,而仅仅只是为如今希望让其代码运行在更多的环境下的开发者提供一些补充的援助。想要关于这种实验性质的形式的更多信息或者想要贡献建议,见:https://github.com/umdjs/umd


还没有评论.