Created
September 2, 2015 16:38
-
-
Save felquis/d770b7c5b7bcca6c2fbc to your computer and use it in GitHub Desktop.
I found a minified JavaScript file on the internet and I decided to re-format the whole code to a human-like code, but I got into the following doubt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I figured out this function looks like RequireJS's `define` function | |
// but I couldn't find a source or resource to help me understand this code | |
// I'll appreciate some help, links to clearly docs | |
// NOTE: I'm not interested in how RequireJS works, cause it's source code is huge, | |
// I just want to understand how the code bellow works. Maybe the source above documented | |
// | |
// | |
// I think it's a output file from browserify or some tool like that | |
function define(modules, n, r) { | |
// I'm not even 1% sure about these variable names | |
// but I chaged to help me understand the minified code | |
function define2(moduleName, a) { | |
if (!n[moduleName]) { | |
if (!modules[moduleName]) { | |
var u = "function" == typeof require && require; | |
if (!a && u) return u(moduleName, true); | |
if (hasRequire) return hasRequire(moduleName, true); | |
throw new Error("Cannot find module '" + moduleName + "'") | |
} | |
// Again I don't have any idea about these variable names | |
var dependencies = n[moduleName] = { | |
exports: {} | |
}; | |
modules[moduleName][0].call(dependencies.exports, function(propertyName) { | |
// This function block is simple, but the `.call` above is not. | |
var moduleIndex = modules[moduleName][1][propertyName]; | |
return define2(moduleIndex ? moduleIndex : propertyName); | |
}, dependencies, dependencies.exports, define, modules, n, r) | |
} | |
return n[moduleName].exports | |
} | |
// I still trying to figure it out | |
var hasRequire = (typeof require === 'function') && require; | |
for (hasRequire, moduleName = 0; moduleName < r.length; moduleName++) define2(r[moduleName]); | |
return define2 | |
} | |
// I could understand it a little | |
var allModulesMixed = { | |
'hello': [function (require, module) { | |
module.exports = function Hello() { | |
console.log('Hi!'); | |
} | |
}], | |
6: [function (require, module) { | |
var Hello = require('./hello'); | |
new Hello(); | |
}, { | |
"./hello": 'hello' | |
}] | |
}; | |
define(allModulesMixed, {}, [6]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment