Renderer/assets/Scripts/compiler/require_polyfill.js
austin 94c19d7990
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit
scripting work
2024-07-10 15:27:05 -04:00

48 lines
1.6 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) => {
loggerScripts.DEBUG('Require path ' + path)
let normalizedFilePath = FILE_RESOLUTION_getFilePath(path)
if(!!REQUIRE_CACHE[normalizedFilePath]){
return REQUIRE_CACHE[normalizedFilePath].exports
} else if(!!COMPILER_fileMap[normalizedFilePath]?.content) {
const code = COMPILER_fileMap[normalizedFilePath].moduleContent
loggerScripts.DEBUG('Module code ' + JSON.stringify(code))
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 {
const errorMsg = "FAILED TO REQUIRE FILE " + normalizedFilePath
loggerScripts.WARNING(errorMsg)
loggerScripts.WARNING('Module value:')
const cacheValue = REQUIRE_CACHE?.[normalizedFilePath]
loggerScripts.WARNING(Object.keys(cacheValue ? cacheValue : {}) + '')
loggerScripts.WARNING('Require cache contents:')
loggerScripts.WARNING(Object.keys(REQUIRE_CACHE) + '')
loggerScripts.WARNING('File cache contents:')
loggerScripts.WARNING(Object.keys(COMPILER_fileMap) + '')
throw new Error(errorMsg)
}
}