Renderer/assets/Scripts/compiler/require_polyfill.js
2024-07-02 16:14:05 -04:00

37 lines
1.0 KiB
JavaScript

/**
* Caches loaded modules
*/
let REQUIRE_CACHE = { }
/**
* Used if the module is directly executed instead of being require'd for some reason
*/
let exports = { }
/**
* Imports a module
* @param {*} path The path of the module
* @param {*} cwd The current working directory
*/
const require = (path) => {
let normalizedFilePath = FILE_RESOLUTION_getFilePath(path)
if(REQUIRE_CACHE[path]){
return REQUIRE_CACHE[normalizedFilePath].exports
} else if(!!COMPILER_fileMap[normalizedFilePath]?.content) {
const code = COMPILER_fileMap[normalizedFilePath].moduleContent
let exports = new Function(code)()
//create module object
const module = {
exports: exports,
exportedValues: Object.keys(exports),
}
REQUIRE_CACHE[normalizedFilePath] = module
loggerScripts.INFO("[require] CREATE MODULE " + normalizedFilePath)
return module.exports
} else {
loggerScripts.WARNING("FAILED TO REQUIRE FILE " + normalizedFilePath)
}
}