Skip to content

Instantly share code, notes, and snippets.

@tpai
Created January 9, 2018 05:03
Show Gist options
  • Save tpai/5fb66b7b37066316e2863a211d4a4a3e to your computer and use it in GitHub Desktop.
Save tpai/5fb66b7b37066316e2863a211d4a4a3e to your computer and use it in GitHub Desktop.
parcel_bundle
// modules are defined as an array
// [ module function, map of requires ]
//
// map of requires is short require name -> numeric require
//
// anything defined in a previous bundle is accessed via the
// orig method which is the require for previous bundles
// eslint-disable-next-line no-global-assign
require = (function (modules, cache, entry) {
// Save the require from previous bundle to this closure if any
var previousRequire = typeof require === "function" && require;
function newRequire(name, jumped) {
if (!cache[name]) {
if (!modules[name]) {
// if we cannot find the module within our internal map or
// cache jump to the current global require ie. the last bundle
// that was added to the page.
var currentRequire = typeof require === "function" && require;
if (!jumped && currentRequire) {
return currentRequire(name, true);
}
// If there are other bundles on this page the require from the
// previous one is saved to 'previousRequire'. Repeat this as
// many times as there are bundles until the module is found or
// we exhaust the require chain.
if (previousRequire) {
return previousRequire(name, true);
}
var err = new Error('Cannot find module \'' + name + '\'');
err.code = 'MODULE_NOT_FOUND';
throw err;
}
localRequire.resolve = resolve;
var module = cache[name] = new newRequire.Module;
modules[name][0].call(module.exports, localRequire, module, module.exports);
}
return cache[name].exports;
function localRequire(x){
return newRequire(localRequire.resolve(x));
}
function resolve(x){
return modules[name][1][x] || x;
}
}
function Module() {
this.bundle = newRequire;
this.exports = {};
}
newRequire.Module = Module;
newRequire.modules = modules;
newRequire.cache = cache;
newRequire.parent = previousRequire;
for (var i = 0; i < entry.length; i++) {
newRequire(entry[i]);
}
// Override the current require with this new one
return newRequire;
})({4:[function(require,module,exports) {
// module a
},{}],8:[function(require,module,exports) {
var bundleURL = null;
function getBundleURLCached() {
if (!bundleURL) {
bundleURL = getBundleURL();
}
return bundleURL;
}
function getBundleURL() {
// Attempt to find the URL of the current script and use that as the base URL
try {
throw new Error;
} catch (err) {
var matches = ('' + err.stack).match(/(https?|file|ftp):\/\/[^)\n]+/g);
if (matches) {
return getBaseURL(matches[0]);
}
}
return '/';
}
function getBaseURL(url) {
return ('' + url).replace(/^((?:https?|file|ftp):\/\/.+)\/[^/]+$/, '$1') + '/';
}
exports.getBundleURL = getBundleURLCached;
exports.getBaseURL = getBaseURL;
},{}],3:[function(require,module,exports) {
var getBundleURL = require('./bundle-url').getBundleURL;
function loadBundles(bundles) {
var id = Array.isArray(bundles) ? bundles[bundles.length - 1] : bundles;
try {
return Promise.resolve(require(id));
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
return new LazyPromise(function (resolve, reject) {
Promise.all(bundles.slice(0, -1).map(loadBundle)).then(function () {
return require(id);
}).then(resolve, reject);
});
}
throw err;
}
}
module.exports = exports = loadBundles;
var bundles = {};
var bundleLoaders = {
js: loadJSBundle,
css: loadCSSBundle
};
function loadBundle(bundle) {
if (bundles[bundle]) {
return bundles[bundle];
}
var type = bundle.match(/\.(.+)$/)[1].toLowerCase();
var bundleLoader = bundleLoaders[type];
if (bundleLoader) {
return bundles[bundle] = bundleLoader(getBundleURL() + bundle);
}
}
function loadJSBundle(bundle) {
return new Promise(function (resolve, reject) {
var script = document.createElement('script');
script.async = true;
script.type = 'text/javascript';
script.charset = 'utf-8';
script.src = bundle;
script.onerror = function (e) {
script.onerror = script.onload = null;
reject(e);
};
script.onload = function () {
script.onerror = script.onload = null;
resolve();
};
document.getElementsByTagName('head')[0].appendChild(script);
});
}
function loadCSSBundle(bundle) {
return new Promise(function (resolve, reject) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = bundle;
link.onerror = function (e) {
link.onerror = link.onload = null;
reject(e);
};
link.onload = function () {
link.onerror = link.onload = null;
resolve();
};
document.getElementsByTagName('head')[0].appendChild(link);
});
}
function LazyPromise(executor) {
this.executor = executor;
this.promise = null;
}
LazyPromise.prototype.then = function (onSuccess, onError) {
return this.promise || (this.promise = new Promise(this.executor).then(onSuccess, onError));
};
LazyPromise.prototype.catch = function (onError) {
return this.promise || (this.promise = new Promise(this.executor).catch(onError));
};
},{"./bundle-url":8}],2:[function(require,module,exports) {
"use strict";
var _a = require("a");
var _a2 = _interopRequireDefault(_a);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
require("_bundle_loader")(require.resolve("b")).then(function (b) {
console.log("b loaded", b);
});
Promise.all([require("_bundle_loader")(require.resolve("c/1")), require("_bundle_loader")(require.resolve("c/2"))]).then(function (arr) {
console.log("c/1 and c/2 loaded", arr);
});
},{"a":4,"_bundle_loader":3,"b":["e20fcd0f9252da8d48bdaa590bb7100b.js",5],"c/1":["f6b6431ddfb12a27791ca9a6d32b446d.js",6],"c/2":["ebddf78172163650f2945d11d1dc57dc.js",7]}]},{},[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment