Skip to content

Instantly share code, notes, and snippets.

@souparno souparno/jsio.js
Last active Jan 10, 2019

Embed
What would you like to do?
jsio bundler
var jsio = (function() {
var util = {
slice: Array.prototype.slice,
bind: function bind(method, context) {
var args = util.slice.call(arguments, 2);
return function() {
return method.apply(context, args.concat(util.slice.call(arguments, 0)));
};
}
};
function require(fromPath, item) {
var moduleDef = loadModule(fromPath, item);
if (!moduleDef.exports) {
moduleDef.exports = {};
fromPath = moduleDef.path;
execModule(makeContext(fromPath), moduleDef);
}
return moduleDef.exports;
}
function setCache(cache) {
jsio.__srcCache = cache;
}
function ModuleDef(path, src, exports) {
this.path = path;
this.src = src;
this.exports = exports;
}
function loadModule(fromPath, path) {
var srcCache = jsio.__srcCache,
modules = jsio.__modules,
cachedVersion;
path = fromPath ? (srcCache[fromPath])[1][path] : path;
cachedVersion = srcCache[path][0];
if (cachedVersion) {
if (!modules[path]) {
modules[path] = new ModuleDef(path, cachedVersion);
}
return modules[path];
}
}
function execModule(jsio, moduleDef) {
var fn = moduleDef.src,
exports = moduleDef.exports;
fn.call(exports, jsio, moduleDef);
}
function makeContext(fromPath) {
var jsio = util.bind(require, null, fromPath);
jsio.setCache = setCache;
jsio.__srcCache = {};
jsio.__modules = {};
return jsio;
}
return makeContext();
}());
jsio.setCache({
"__tests__/test1/print.js": [(function(require, module) {
module.exports = function(res) {
console.log(res);
};
}), {
}],
"__tests__/test1/calculator.js": [(function(require, module) {
var print = require('./print');
module.exports = {
add: function(a, b) {
print(a + b);
}
};
}), {
"./print": "__tests__/test1/print.js"
}],
"__tests__/test1/app.js": [(function(require, module) {
var calculator = require('./calculator');
calculator.add(4, 5);
}), {
"./calculator": "__tests__/test1/calculator.js"
}]
});
jsio('__tests__/test1/app.js');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.