Skip to content

Instantly share code, notes, and snippets.

@hackerxian
Created September 30, 2018 02:16
Show Gist options
  • Save hackerxian/2bc8977b95112660b7f96df4522cd4d6 to your computer and use it in GitHub Desktop.
Save hackerxian/2bc8977b95112660b7f96df4522cd4d6 to your computer and use it in GitHub Desktop.
module require and define
(function(global) {
if (global.define) {
return;
}
/* eslint strict:0 */
var modules = {};
var inGuard = false;
function def(id, deps, factory) {
if (deps instanceof Function) {
factory = deps;
deps = [];
}
modules[id] = {
factory: factory,
deps: deps,
module: {exports: {}},
isInitialized: false,
hasError: false,
};
}
function req(id) {
// Weex built-in modules
if (id.indexOf('@weex-module') === 0) {
return {};
}
var originId = id;
var mod = modules[id];
// Node like require
if (!mod) {
id = id + '/index';
mod = modules[id];
}
if (mod && mod.isInitialized) {
return mod.module.exports;
}
return requireImpl(id, originId);
}
function requireImpl(id, originId) {
if (global.ErrorUtils && !inGuard) {
inGuard = true;
var returnValue;
try {
returnValue = requireImpl.apply(this, arguments);
} catch (e) {
global.ErrorUtils.reportFatalError(e);
}
inGuard = false;
return returnValue;
}
var mod = modules[id];
if (!mod) {
throw new Error(
'Requiring unknown module "' + originId + '"'
);
}
if (mod.hasError) {
throw new Error(
'Requiring module "' + originId + '" which threw an exception'
);
}
try {
// We must optimistically mark mod as initialized before running the factory to keep any
// require cycles inside the factory from causing an infinite require loop.
mod.isInitialized = true;
// keep args in sync with with defineModuleCode in
// rax/bundler/src/resolver.js
mod.factory(req, mod.module.exports, mod.module);
} catch (e) {
mod.hasError = true;
mod.isInitialized = false;
throw e;
}
return mod.module.exports;
}
global.define = def;
global.require = req;
})(this || typeof global === 'object' && global || typeof window === 'object' && window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment