Created
July 17, 2018 23:03
-
-
Save adamisntdead/40661e3e03690fa5b00630d7500cd445 to your computer and use it in GitHub Desktop.
Simple 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
module.exports = entry => pack(getModules(entry)) |
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
// A factory function | |
(require, module) => { | |
/* Module Source */ | |
} |
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
function getModules(entry) { | |
const rootModule = createModuleObject(entry) | |
const modules = [rootModule] | |
// Iterate over the modules, even when new | |
// ones are being added | |
for (const module of modules) { | |
module.map = {} // Where we will keep the module maps | |
module.requires.forEach(dependency => { | |
const basedir = path.dirname(module.filepath) | |
const dependencyPath = resolve(dependency, { basedir }) | |
const dependencyObject = createModuleObject(dependencyPath) | |
module.map[dependency] = dependencyObject.id | |
modules.push(dependencyObject) | |
}) | |
} | |
return modules | |
} |
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 detective = require('detective') | |
const resolve = require('resolve').sync | |
const fs = require('fs') | |
const path = require('path') |
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
let ID = 0 | |
function createModuleObject(filepath) { | |
const source = fs.readFileSync(filepath, 'utf-8') | |
const requires = detective(source) | |
const id = ID++ | |
return { id, filepath, source, requires } | |
} |
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
function pack(modules) { | |
const modulesSource = modules.map(module => | |
`${module.id}: { | |
factory: (module, require) => { | |
${module.source} | |
}, | |
map: ${JSON.stringify(module.map)} | |
}` | |
).join() | |
return `(modules => { | |
const require = id => { | |
const { factory, map } = modules[id] | |
const localRequire = name => require(map[name]) | |
const module = { exports: {} } | |
factory(module, localRequire) | |
return module.exports | |
} | |
require(0) | |
})({ ${modulesSource} })` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment