STATUS: SUPERSEDED BY 1.1
This specification addresses how modules should be written in order to be interoperable among a class of module systems that can be both client and server side, secure or insecure, implemented today or supported by future systems with syntax extensions. These modules are offered privacy of their top scope, facility for importing singleton objects from other modules, and exporting their own API. By implication, this specification defines the minimum features that a module system must provide in order to support interoperable modules.
Contents[hide] |
Contract
Module Context
- In a module, there is a free variable "require", that is a function.
- The "require" function accepts a module identifier.
- "require" returns the exported API of the foreign module.
- If there is a dependency cycle, the foreign module may not have finished executing at the time it is required by one of its transitive dependencies; in this case, the object returned by "require" must contain at least the exports that the foreign module has prepared before the call to require that led to the current module's execution.
- If the requested module cannot be returned, "require" must throw an error.
- In a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.
- modules must use the "exports" object as the only means of exporting.
Module Identifiers
- A module identifier is a String of "terms" delimited by forward slashes.
- A term must be a camelCase identifier, ".", or "..".
- Module identifiers may not have file-name extensions like ".js".
- Module identifiers may be "relative" or "top-level". A module identifier is "relative" if the first term is "." or "..".
- Top-level identifiers are resolved off the conceptual module name space root.
- Relative identifiers are resolved relative to the identifier of the module in which "require" is written and called.
Unspecified
This specification leaves the following important points of interoperability unspecified:
- Whether modules are stored with a database, file system, or factory functions, or are interchangeable with link libraries.
- Whether a PATH is supported by the module loader for resolving module identifiers.
Unit Tests
- Unit Tests at Google Code by Kris Kowal
- Unit Tests Git Mirror by Ash Berlin
Sample Code
exports.add = function() { var sum = 0, i = 0, args = arguments, l = args.length; while (i < l) { sum += args[i++]; } return sum; };
var add = require('math').add; exports.increment = function(val) { return add(val, 1); };
var inc = require('increment').increment; var a = 1; inc(a); // 2
相关推荐
扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类
扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类
扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类
扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类
扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类
rollup-plugin-commonjs, 将CommonJS模块转换为 ES2015 rollup-plugin-commonjs 将CommonJS模块转换为 ES6,以便它们可以包含在Rollup包中安装npm install --save-dev rollup-plugin-commonjs用法
commonjs-everywhere, 具有别名,可扩展性和源映射的CommonJS浏览器 bundler 到处都是 web浏览器使用从缩小的JS包到原始源的源代码,浏览器替代的别名和任意语言支持的可扩展性。安装npm install -g commonjs-...
CommonJS时代的JS模块和编译工具.pdf。扫描清晰可见,是学习jS的必备数据,
nashorn-commonjs-modules, CommonJS的模块支持 Nashorn CommonJS模块支持 Nashorn这个库增加了对CommonJS模块( aka require ) inside的支持( Nashorn脚本引擎) 。 它基于 NodeJS模块的规范,它支持从文件夹加载模块...
扩展的EXTJS公共类.扩展的EXTJS公共类.扩展的EXTJS公共类
一个Babel 7转换插件用于将CommonJS转换为ESM
CommonJS和ES6模块的区别
浏览器-commonjs 在浏览器中使用 CommonJS 模块。 安装 $ bower install browser-commonjs 用法 < script src =" contentloaded.js " > </ script > < script src =" browser-common.js " > &...
关于前端中nodejs使用CommonJS规范实现模块化开发的两个小例子。第一个是基本使用方法,模块的导入与模块的导出;第二个是模拟扑克牌发牌的小练习
commonjs1.part04.rar
commonjs1.part02.rar
commonjs1.part03.rar
commonjs.part2.rarcommonjs.part2.rar