Skip to content

Instantly share code, notes, and snippets.

@jennings
Last active March 7, 2019 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennings/8ce3cc15f9ae47e1fbbf8c85718dc129 to your computer and use it in GitHub Desktop.
Save jennings/8ce3cc15f9ae47e1fbbf8c85718dc129 to your computer and use it in GitHub Desktop.
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