Created
November 22, 2018 04:10
-
-
Save k10526/cf69f383e2aba450dd5d7b642ebd4a1d to your computer and use it in GitHub Desktop.
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
{ | |
function makeBody(body, ex, im){ | |
return `function (${im}) { | |
${body} | |
return ${ex}; | |
}` | |
} | |
const im_re = /(?:^|\s|;)import +(\S[\S\s]*?) +from +"(\S+)" *;/; | |
const ex_re = /(?:^|\s|;)export +default +(\S+);$/; | |
function loadModule(modules) { | |
//file을 모듈로 만드는건 그냥 나중에 구현하도록 하자. | |
} | |
const modules = { | |
a: { | |
dep: ['b','c'], | |
body: makeBody('console.log(b(), c());', '', 'b,c'), | |
c: false | |
}, | |
b: { | |
dep: [], | |
body: makeBody('var f = function () {return 2}', 'f', ''), | |
c: false | |
}, | |
c: { | |
dep: [], | |
body: makeBody('var f = function () {return 3}', 'f', ''), | |
c: false | |
}, | |
d: { | |
dep: ['b'], | |
body: makeBody('console.log(b())', '', 'b'), | |
c: false | |
} | |
} | |
function makeModule(ms) { | |
return Object.entries(ms).reduce((res, [name, m]) => { | |
res +=`__mo['${name}'] = ${m.body};` | |
return res; | |
}, ''); | |
} | |
function makeDep(name, m, dep) { | |
if (dep.includes(name)) { | |
throw new Error('circle dependency.') | |
} | |
const d = [...dep, name] | |
if (m.c) { | |
return `__ca['${name}']`; | |
} | |
const res = `__ca['${name}']=__mo['${name}'](${m.dep.map((dm)=>makeDep(dm, modules[dm], d)).join(',')})` | |
m.c = true; | |
return res; | |
} | |
function makeAllDep(ms) { | |
return Object.entries(ms).reduce((res, [name, m]) => { | |
return m.c ? res : `${res};${makeDep(name, m, [])}` | |
}, ''); | |
} | |
function build(ms) { | |
return `(function(){var __mo={},__ca={};${makeModule(ms)};${makeAllDep(ms)}})()` | |
} | |
build(modules); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment