Skip to content

Instantly share code, notes, and snippets.

@souparno
Last active January 10, 2019 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save souparno/d9d638b03bd6f9df0430cdd97cf46e5a to your computer and use it in GitHub Desktop.
Save souparno/d9d638b03bd6f9df0430cdd97cf46e5a to your computer and use it in GitHub Desktop.
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