Last active
August 29, 2015 13:57
-
-
Save dfkaye/9680865 to your computer and use it in GitHub Desktop.
3/20/2014 ~ api for common-inject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// shell style commonjs for node.js | |
var a = require('a'); | |
module.exports = example; | |
function example() { | |
var r = a.apply(this, arguments); | |
return r; | |
} | |
// AMD for browser - arguments may be incorrect here... | |
define(function(module, require, exports){ | |
var a = require('a'); | |
module.exports = example; | |
function example() { | |
var r = a.apply(this, arguments); | |
return r; | |
} | |
}); | |
// don't need a new global named define | |
// just overwrite require() and be done with it | |
// insane injection style fir node.js | |
require('a') | |
(function() { | |
module.exports = example; | |
function example() { | |
// yes, a is available as a symbol to this module | |
var r = a.apply(this, arguments); | |
return r; | |
} | |
}); | |
// insane injection style with multi requires for node.js | |
require('a', 'b', { | |
alias: 'path/to/source' | |
}) | |
(function() { | |
// a, b, alias available as symbols to this module | |
module.exports = example; | |
function example() { | |
var r = a.apply(this, arguments); | |
return r; | |
} | |
}); | |
// insane injection style with multi requires | |
// using namespace for concatenated browser builds | |
require.name('path/to/example') | |
('a', 'b', { | |
alias: 'path/to/source' | |
}) | |
(function() { | |
// a, b, alias available as symbols to this module | |
module.exports = example; | |
function example() { | |
var r = a.apply(this, arguments); | |
return r; | |
} | |
}); | |
// one function, require, with a #name method | |
// name returns require() | |
// require accepts string for paths, config for path aliasing | |
// and function for injecting path aliases into module scope | |
// config is local | |
// name is optional (build tool inserts it) | |
// symbols injected into iife | |
// module, exports, global, __filename, __dirname | |
// this is essentially an annotation block that beats the minifiers | |
('a', 'b', { | |
alias: 'path/to/source' | |
}) | |
// aesthetically the bulk require doesn't thrill me - maybe use single arg version: | |
require.name('path/to/me') | |
require('a') | |
require('b') | |
require({ | |
alias: 'path/to/source' | |
}) | |
(function () { | |
module.exports //, etc. | |
}); | |
// which could be built down to | |
require.name('path/to/me') | |
('a', 'b', { | |
alias: 'path/to/source' | |
}) | |
(function () { | |
module.exports //, etc. | |
}); | |
// support both the expanded version for readability and the chaining version for minification????? | |
////// wow pretty close | |
///// 3/21/2014- looked at amark/theory | |
//// see https://github.com/amark/theory#require | |
/// source: https://github.com/amark/theory/blob/master/theory.js#L726-L758 | |
// see also his imports.js gist: https://gist.github.com/amark/6291429 | |
module.exports = require('theory') | |
('hello', function(a){ | |
// your initialization code here. | |
return { world: $('input').val() }; // the module you export. | |
},['http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js']); | |
/* | |
<script src="/theory.js"> | |
require('./hello') | |
</script> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment