Skip to content

Instantly share code, notes, and snippets.

@jennings

jennings/fake_webpack.js

Last active Mar 7, 2019
Embed
What would you like to do?
const MODULE_BODIES = {
// This is the body of the file foo/index.ts, wrapped in a function
["./foo/index.ts"]: function (module) {
const file = __webpack_resolve__("./foo/file.ts")
module.exports.Service = file.Service
},
// This is the body of the file foo/file.ts, wrapped in a function
["./foo/file.ts"]: function (module) {
module.exports.Service = Service;
function Service() {
console.log("hello")
}
},
}
// Once a module has been executed, it's stored in this object
// and pulled from here for every future resolve
const MODULE_CACHE = {}
// webpack replaces "import" and "require()" with a call to this function
function __webpack_resolve__(moduleName) {
// Get it out of the cache if we've already imported the module
let module = MODULE_CACHE[module];
// Otherwise,
if (!module) {
// Find the module's body
const module_body = MODULE_BODIES[moduleName]
// Execute it, passing it a "module" object
const module_object = {}
module = module_body(module_object)
// Store the exported module for future resolves
MODULE_CACHE[moduleName] = module;
}
return module;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.