Created
July 6, 2016 12:29
-
-
Save vdanchenkov/4da79e3645afefc63a5d1fffed0d87ba to your computer and use it in GitHub Desktop.
simple amd loader
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
import zipObject from 'lodash/zipObject' | |
import memoize from 'lodash/memoize' | |
const loadModules = memoize((modules) => { | |
// prevent node style export | |
const exports = undefined | |
const module = undefined | |
const resultModules = [] | |
const resultNames = [] | |
const define = (...args) => { | |
const factory = args[args.length - 1] | |
resultModules.push(typeof factory == 'function' ? factory() : factory) | |
} | |
define.amd = {} | |
for (const moduleName in modules) { | |
eval(modules[moduleName]) | |
// iteration order is not guarantied, so we have to push names in the same order as modules | |
resultNames.push(moduleName) | |
} | |
return zipObject(resultNames, resultModules) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment