Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial

  • Loading branch information...
commit e5468ddf5dd41aa9a51cd282c6acc77fdf8fcb2f 0 parents
@dominictarr authored
83 bundle.js
@@ -0,0 +1,83 @@
+require('mad')
+
+module.define(['fs','vm'],function (fs,vm){
+ var _fn = module.filename
+
+ return function (){
+ var _fn = module.filename
+ , path = [].shift.call(arguments)
+ , done = [].pop.call(arguments)
+ , minify = [].pop.call(arguments)
+ , filename = resolve(path), refs = {}, rCount = 1, funx = [], modules = {}
+
+ function resolve (req,filename){
+ module.filename = filename || path
+ var r = require.resolve(req)
+ module.filename = _fn
+ return r
+ }
+
+
+ function load (filename){
+ var src = fs.readFileSync(filename,'utf-8')
+
+ var script = vm.createScript(src)
+ script.runInNewContext({
+ require:function (){},
+ module: {
+ define: function (depends,init){
+
+ var m = {
+ fn: filename,
+ init: init,
+ resolves: {},
+ }
+
+ depends.forEach(function (e){
+ var depFN = resolve(e,filename)
+ if(!modules[depFN])
+ load(depFN)
+ m.resolves[e] = resolve(e,filename)
+ })
+ if(!modules[filename])
+ modules[filename] = m
+ }
+ }
+ })
+ }
+
+ load(filename)
+
+ function makeRef(item){
+ return refs[item] = rCount ++
+ }
+
+ function ref (item){
+ if(minify)
+ return 'M[' + ( refs[item] ? refs[item] : makeRef(item) ) +']'
+ return 'M[' + JSON.stringify(item) + ']'
+ }
+
+ function indent(src){
+ return src.split('\n').map(function (x){return' ' + x}).join('\n')
+ }
+
+ function argsFor(fn){
+ var resolves = modules[fn].resolves
+ , args = []
+ for(var key in resolves)
+ args.push(ref(resolves[key]))
+ return args
+ }
+
+ for(var fn in modules){
+ //if it is the main module, do not register it.
+ funx.push((fn === filename ? 'return' : ref (fn) + ' =\n' )
+ + indent('(' + modules[fn].init + ')(' + argsFor(fn) + ');\n')
+ )
+ }
+
+ done(null,'(function (M){\n' + funx.join('\n') + '\n})({});')
+ }
+
+})
4 go.js
@@ -0,0 +1,4 @@
+require('./bundle')(process.env.PWD + '/' + process.argv[2], process.argv[3]
+ , function (e,s){
+ console.log(s)
+ })
13 mad.js
@@ -0,0 +1,13 @@
+
+/*
+ adds support for using ADM style moduels in node.
+
+*/
+
+module.constructor.prototype.define = function (depends,initializer){
+ var self = this
+ var _id = module.filename
+ module.filename = self.filename
+ this.exports = initializer.apply(this.exports,depends.map(require)) || this.exports
+ module.filename = _id
+}
19 package.json
@@ -0,0 +1,19 @@
+{
+ "name": "amd",
+ "description": "Async Module Definition - module loader/bunder for node + browser",
+ "version": "0.0.0",
+ "repository": {
+ "url": ""
+ },
+ "author": "Dominic Tarr <dominic.tarr@gmail.com> (bit.ly/dominictarr)",
+ "main": "amd.js",
+ "directories": {
+ "lib": "."
+ },
+ "scripts": {
+ "test": "meta-test test/*.js"
+ },
+ "engines": {
+ "node": "*"
+ }
+}
1  readme.markdown
@@ -0,0 +1 @@
+#a superlightweight module loader for node and the browser.
30 test/bundle.asynct.js
@@ -0,0 +1,30 @@
+require('amd')
+var bundle = require('mad/bundle')
+
+exports ['test'] = function (test){
+
+ bundle(__dirname + '/examples/b.js', true,function (err,src){
+ console.log(src)
+
+ var x = eval(src)
+
+ test.deepEqual(x,{a:'aardvark', b: 'banana'})
+
+ test.done()
+ })
+
+}
+
+exports ['test repeated'] = function (test){
+
+ bundle(__dirname + '/examples/c.js', true, function (err,src){
+ console.log(src)
+
+ var x = eval(src)
+
+ test.deepEqual(x,{a:'aardvark', b: 'banana', c:'coco'})
+
+ test.done()
+ })
+
+}
4 test/examples/a.js
@@ -0,0 +1,4 @@
+
+module.define([],function (){
+ return {a: 'aardvark'}
+})
4 test/examples/b.js
@@ -0,0 +1,4 @@
+
+module.define(['./a'],function (a){
+ return {a: a.a, b: 'banana'}
+})
4 test/examples/c.js
@@ -0,0 +1,4 @@
+
+module.define(['./a','./b'],function (a,b){
+ return {a: a.a, b: b.b, c: 'coco'}
+})
9 test/mad.synct.js
@@ -0,0 +1,9 @@
+require('../amd')
+module.define(['assert','./examples/a','./examples/b'], function (assert,a,b){
+
+ this ['loads AMD style modules'] = function (){
+ assert.equal(b.a,'aardvark')
+ assert.equal(b.b,'banana')
+ }
+
+})
Please sign in to comment.
Something went wrong with that request. Please try again.