Skip to content

Instantly share code, notes, and snippets.

@christianscott
Created September 2, 2019 05:34
Show Gist options
  • Save christianscott/dc899a95de97b62e3802ec8f7a73cd15 to your computer and use it in GitHub Desktop.
Save christianscott/dc899a95de97b62e3802ec8f7a73cd15 to your computer and use it in GitHub Desktop.
shitty module bundler
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