UNPKG

fuse-box

Version:

Fuse-Box a bundler that does it right

385 lines (384 loc) • 11.5 kB
(function(__root__){ if (__root__["FuseBox"]) return __root__["FuseBox"]; var $isBrowser = typeof window !== "undefined" && window.navigator; var g = $isBrowser ? window : global; if ($isBrowser) { g["global"] = window; } __root__ = !$isBrowser || typeof __fbx__dnm__ !== "undefined" ? module.exports : __root__; var $fsbx = $isBrowser ? (window["__fsbx__"] = window["__fsbx__"] || {}) : g["$fsbx"] = g["$fsbx"] || {}; if (!$isBrowser) { g["require"] = require; } var $packages = $fsbx.p = $fsbx.p || {}; var $events = $fsbx.e = $fsbx.e || {}; function $getNodeModuleName(name) { var n = name.charCodeAt(0); var s = name.charCodeAt(1); if (!$isBrowser && s === 58) { return; } if (n >= 97 && n <= 122 || n === 64) { if (n === 64) { var s_1 = name.split("/"); var target = s_1.splice(2, s_1.length).join("/"); return [s_1[0] + "/" + s_1[1], target || undefined]; } var index = name.indexOf("/"); if (index === -1) { return [name]; } var first = name.substring(0, index); var second = name.substring(index + 1); return [first, second]; } } ; function $getDir(filePath) { return filePath.substring(0, filePath.lastIndexOf("/")) || "./"; } ; function $pathJoin() { var string = []; for (var _i = 0; _i < arguments.length; _i++) { string[_i] = arguments[_i]; } var parts = []; for (var i = 0, l = arguments.length; i < l; i++) { parts = parts.concat(arguments[i].split("/")); } var newParts = []; for (var i = 0, l = parts.length; i < l; i++) { var part = parts[i]; if (!part || part === ".") continue; if (part === "..") { newParts.pop(); } else { newParts.push(part); } } if (parts[0] === "") newParts.unshift(""); return newParts.join("/") || (newParts.length ? "/" : "."); } ; function $ensureExtension(name) { var matched = name.match(/\.(\w{1,})$/); if (matched) { if (!matched[1]) { return name + ".js"; } return name; } return name + ".js"; } ; function $loadURL(url) { if ($isBrowser) { var d = document; var head = d.getElementsByTagName("head")[0]; var target; if (/\.css$/.test(url)) { target = d.createElement("link"); target.rel = "stylesheet"; target.type = "text/css"; target.href = url; } else { target = d.createElement("script"); target.type = "text/javascript"; target.src = url; target.async = true; } head.insertBefore(target, head.firstChild); } } ; function $loopObjKey(obj, func) { for (var key in obj) { if (obj.hasOwnProperty(key)) { func(key, obj[key]); } } } ; function $serverRequire(path) { return { server: require(path) }; } ; function $getRef(name, o) { var basePath = o.path || "./"; var pkgName = o.pkg || "default"; var nodeModule = $getNodeModuleName(name); if (nodeModule) { basePath = "./"; pkgName = nodeModule[0]; if (o.v && o.v[pkgName]) { pkgName = pkgName + "@" + o.v[pkgName]; } name = nodeModule[1]; } if (name) { if (name.charCodeAt(0) === 126) { name = name.slice(2, name.length); basePath = "./"; } else { if (!$isBrowser && (name.charCodeAt(0) === 47 || name.charCodeAt(1) === 58)) { return $serverRequire(name); } } } var pkg = $packages[pkgName]; if (!pkg) { if ($isBrowser && FuseBox.target !== "electron") { throw "Package not found " + pkgName; } else { return $serverRequire(pkgName + (name ? "/" + name : "")); } } name = name ? name : "./" + pkg.s.entry; var filePath = $pathJoin(basePath, name); var validPath = $ensureExtension(filePath); var file = pkg.f[validPath]; var wildcard; if (!file && validPath.indexOf("*") > -1) { wildcard = validPath; } if (!file && !wildcard) { validPath = $pathJoin(filePath, "/", "index.js"); file = pkg.f[validPath]; if (!file) { validPath = filePath + ".js"; file = pkg.f[validPath]; } if (!file) { file = pkg.f[filePath + ".jsx"]; } if (!file) { validPath = filePath + "/index.jsx"; file = pkg.f[validPath]; } } return { file: file, wildcard: wildcard, pkgName: pkgName, versions: pkg.v, filePath: filePath, validPath: validPath, }; } ; function $async(file, cb, o) { if (o === void 0) { o = {}; } if ($isBrowser) { if (o && o.ajaxed === file) { return console.error(file, 'does not provide a module'); } var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { var contentType = xmlhttp.getResponseHeader("Content-Type"); var content = xmlhttp.responseText; if (/json/.test(contentType)) { content = "module.exports = " + content; } else { if (!/javascript/.test(contentType)) { content = "module.exports = " + JSON.stringify(content); } } var normalized = $pathJoin("./", file); FuseBox.dynamic(normalized, content); cb(FuseBox.import(file, { ajaxed: file })); } else { console.error(file, 'not found on request'); cb(undefined); } } }; xmlhttp.open("GET", file, true); xmlhttp.send(); } else { if (/\.(js|json)$/.test(file)) return cb(g["require"](file)); return cb(""); } } ; function $trigger(name, args) { var e = $events[name]; if (e) { for (var i in e) { var res = e[i].apply(null, args); if (res === false) { return false; } } ; } } ; function $import(name, o) { if (o === void 0) { o = {}; } if (name.charCodeAt(4) === 58 || name.charCodeAt(5) === 58) { return $loadURL(name); } var ref = $getRef(name, o); if (ref.server) { return ref.server; } var file = ref.file; if (ref.wildcard) { var safeRegEx = new RegExp(ref.wildcard .replace(/\*/g, "@") .replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") .replace(/@@/g, ".*") .replace(/@/g, "[a-z0-9$_-]+"), "i"); var pkg_1 = $packages[ref.pkgName]; if (pkg_1) { var batch = {}; for (var n in pkg_1.f) { if (safeRegEx.test(n)) { batch[n] = $import(ref.pkgName + "/" + n); } } return batch; } } if (!file) { var asyncMode_1 = typeof o === "function"; var processStopped = $trigger("async", [name, o]); if (processStopped === false) { return; } return $async(name, function (result) { return asyncMode_1 ? o(result) : null; }, o); } var pkg = ref.pkgName; if (file.locals && file.locals.module) return file.locals.module.exports; var locals = file.locals = {}; var path = $getDir(ref.validPath); locals.exports = {}; locals.module = { exports: locals.exports }; locals.require = function (name, optionalCallback) { return $import(name, { pkg: pkg, path: path, v: ref.versions, }); }; if ($isBrowser || !g["require"].main) { locals.require.main = { filename: "./", paths: [] }; } else { locals.require.main = g["require"].main; } var args = [locals.module.exports, locals.require, locals.module, ref.validPath, path, pkg]; $trigger("before-import", args); file.fn.apply(0, args); $trigger("after-import", args); return locals.module.exports; } ; var FuseBox = (function () { function FuseBox() { } FuseBox.global = function (key, obj) { if (obj === undefined) return g[key]; g[key] = obj; }; FuseBox.import = function (name, o) { return $import(name, o); }; FuseBox.on = function (n, fn) { $events[n] = $events[n] || []; $events[n].push(fn); }; FuseBox.exists = function (path) { try { var ref = $getRef(path, {}); return ref.file !== undefined; } catch (err) { return false; } }; FuseBox.remove = function (path) { var ref = $getRef(path, {}); var pkg = $packages[ref.pkgName]; if (pkg && pkg.f[ref.validPath]) { delete pkg.f[ref.validPath]; } }; FuseBox.main = function (name) { this.mainFile = name; return FuseBox.import(name, {}); }; FuseBox.expose = function (obj) { var _loop_1 = function (k) { var alias = obj[k].alias; var xp = $import(obj[k].pkg); if (alias === "*") { $loopObjKey(xp, function (exportKey, value) { return __root__[exportKey] = value; }); } else if (typeof alias === "object") { $loopObjKey(alias, function (exportKey, value) { return __root__[value] = xp[exportKey]; }); } else { __root__[alias] = xp; } }; for (var k in obj) { _loop_1(k); } }; FuseBox.dynamic = function (path, str, opts) { this.pkg(opts && opts.pkg || "default", {}, function (___scope___) { ___scope___.file(path, function (exports, require, module, __filename, __dirname) { var res = new Function("__fbx__dnm__", "exports", "require", "module", "__filename", "__dirname", "__root__", str); res(true, exports, require, module, __filename, __dirname, __root__); }); }); }; FuseBox.flush = function (shouldFlush) { var def = $packages["default"]; for (var fileName in def.f) { if (!shouldFlush || shouldFlush(fileName)) { delete def.f[fileName].locals; } } }; FuseBox.pkg = function (name, v, fn) { if ($packages[name]) return fn($packages[name].s); var pkg = $packages[name] = {}; pkg.f = {}; pkg.v = v; pkg.s = { file: function (name, fn) { return pkg.f[name] = { fn: fn }; }, }; return fn(pkg.s); }; FuseBox.addPlugin = function (plugin) { this.plugins.push(plugin); }; FuseBox.packages = $packages; FuseBox.isBrowser = $isBrowser; FuseBox.isServer = !$isBrowser; FuseBox.plugins = []; return FuseBox; }()); if (!$isBrowser) { g["FuseBox"] = FuseBox; } return __root__["FuseBox"] = FuseBox; } )(this)