An open API service indexing awesome lists of open source software.

https://github.com/thomd/javascript-module-wrapper

universal wrapper for AMD, CommonJS and oldschools-JS modules (with tests)
https://github.com/thomd/javascript-module-wrapper

Last synced: 11 months ago
JSON representation

universal wrapper for AMD, CommonJS and oldschools-JS modules (with tests)

Awesome Lists containing this project

README

          

## JavaScript Modules

This is a little research project about using javascript modules.

[![Build Status](https://travis-ci.org/thomd/javascript-module-wrapper.svg?branch=master)](https://travis-ci.org/thomd/javascript-module-wrapper)

A universal module wrapper is wrapping your javascript library so it can work as

1. **[CommonJS](http://www.commonjs.org/) module** (Implemented by [Node.js](http://wiki.thomd.net/index.php?title=Node.js),
[Rhino](http://wiki.thomd.net/index.php?title=Javascript_engines#Rhino),
[MongoDB](http://wiki.thomd.net/index.php?title=MongoDB) or
[CouchDB](http://wiki.thomd.net/index.php?title=CouchDB)
among others)
2. **[AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) module** (mplemented
by [Require.js](http://wiki.thomd.net/index.php?title=Javascript_Loader#RequireJS)
or [curl](https://github.com/cujojs/curl) among others)
3. **Plain JavaScript** (attached to the `global` object)

### Universal Module Wrapper

(function(global, factory){
if (typeof define === "function" && define.amd) define(factory); // AMD
else if (typeof module === "object") module.exports = factory(); // CommonJS
else global.myModule = factory(); // global context
}(this, function(){
"use strict";
return function(...) { ... } // the module
}));

### Load module using AMD



require(['module.js'], function(){
myModule();
})

### Load module using CommonJS

var myModule = require('./module');
myModule();

### Load module using the global context



myModule();

## Test

Tests are done with [Mocha](http://mochajs.org/) using
[Zombie](http://zombie.labnotes.org/).

Run all tests for AMD, CommonJS and Plain Javascript with

npm install
npm test