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公共类
rollup-plugin-commonjs, 将CommonJS模块转换为 ES2015 rollup-plugin-commonjs 将CommonJS模块转换为 ES6,以便它们可以包含在Rollup包中安装npm install --save-dev rollup-plugin-commonjs用法
扩展的EXTJS公共类,扩展的EXTJS公共类,扩展的EXTJS公共类
本篇文章将深入探讨Babel 7中的转换插件,特别是如何使用`babel-plugin-transform-commonjs`将CommonJS转换为ESM。 首先,让我们了解CommonJS和ESM的区别。CommonJS采用同步导入机制,允许在代码中使用`require()`...
CommonJS是一种在服务器端JavaScript中实现模块化的规范,它的出现主要是为了标准化服务器端JavaScript模块的加载机制。CommonJS规范最初于2009年提出,它的设计理念是基于同步加载机制,并且以文件为模块的载体。在...
commonjs-everywhere, 具有别名,可扩展性和源映射的CommonJS浏览器 bundler 到处都是 web浏览器使用从缩小的JS包到原始源的源代码,浏览器替代的别名和任意语言支持的可扩展性。安装npm install -g commonjs-...
nashorn-commonjs-modules, CommonJS的模块支持 Nashorn CommonJS模块支持 Nashorn这个库增加了对CommonJS模块( aka require ) inside的支持( Nashorn脚本引擎) 。 它基于 NodeJS模块的规范,它支持从文件夹加载模块...
扩展的EXTJS公共类.扩展的EXTJS公共类.扩展的EXTJS公共类
CommonJS是Node.js中实现模块化开发的重要规范,它允许我们在JavaScript中导入和导出模块,使得代码结构清晰、可复用性增强。本文将深入探讨CommonJS的基本概念、使用方法以及通过一个模拟扑克牌发牌的小例子来实践...
### CommonJS与ES6模块的区别详解 #### 一、引言 随着JavaScript的发展与应用领域的扩展,特别是Node.js的出现,让JavaScript不仅局限于浏览器环境,更拓展到了服务器端。为了更好地管理和组织代码,模块化成为了...
#### 一、CommonJS **CommonJS** 是为了解决JavaScript在实际应用中缺乏模块化支持的问题而诞生的一种规范。它最初是为了让JavaScript能够在服务器端运行,尤其是为了适应Node.js这样的环境。CommonJS的出现极大地...
汇总插件-commonjs 将CommonJS模块转换为ES6,以便可以将它们包含在汇总包中安装npm install --save-dev rollup-plugin-commonjs用法通常,您可以将此插件与一起使用,以便将CommonJS依赖项捆绑在node_modules 。...
内容概要:本文详细介绍了JavaScript的模块化编程及其在CommonJS中的实现方法。首先解释了模块化编程的重要性和在JavaScript中的应用,接着具体讲述了CommonJS的规范和实现细节,包括模块的定义、导出、导入及缓存...
commonjs1.part04.rar
commonjs1.part02.rar