Created
September 2, 2019 05:34
-
-
Save christianscott/dc899a95de97b62e3802ec8f7a73cd15 to your computer and use it in GitHub Desktop.
shitty module bundler
This file contains hidden or 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
const renderBootstrap = body => `const modules = {}; | |
function require(name) { | |
return modules[name] && modules[name].exports; | |
} | |
function define(name, definition) { | |
const module = {}; | |
definition(require, module); | |
modules[name] = module; | |
} | |
${body} | |
`; | |
const renderModule = module => `define('${module.name}', function(require, module) { | |
${module.body.split('\n').join(' \n')} | |
}); | |
`; | |
const pack = modules => { | |
const seen = new Set() | |
return renderBootstrap(modules.map(module => { | |
if (seen.has(module.name)) throw new Error('duplicate modules') | |
seen.add(module.name) | |
return renderModule(module) | |
}).join('\n')); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment