fix: cache module load error

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-12-17 18:34:57 +08:00
parent 51fc4549da
commit 54e40b6768
2 changed files with 33 additions and 11 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>MiaoScript</artifactId> <artifactId>MiaoScript</artifactId>
<version>0.10.0</version> <version>0.10.1</version>
<developers> <developers>
<developer> <developer>
<id>502647092</id> <id>502647092</id>

View File

@ -95,7 +95,7 @@
* @returns {*} * @returns {*}
*/ */
function _isFile(file) { function _isFile(file) {
return file.isFile && file.isFile() return file && file.isFile && file.isFile()
} }
/** /**
@ -362,7 +362,7 @@
* 检查缓存模块 * 检查缓存模块
*/ */
function checkCacheModule(optional) { function checkCacheModule(optional) {
return !optional.path.startsWith('/') && cacheModuleIds[optional.parentId] && cacheModuleIds[optional.parentId][optional.path] return optional.local ? cacheModuleIds[optional.parentId] && cacheModuleIds[optional.parentId][optional.path] : cacheModuleIds[optional.path]
} }
/** /**
* 加载模块 * 加载模块
@ -372,12 +372,16 @@
* @returns {*} * @returns {*}
*/ */
function _require(name, path, optional) { function _require(name, path, optional) {
// require direct file
var file = _isFile(name) ? name : new File(name)
if (_isFile(file)) { return _requireFile(file, optional) }
// require cache module
var cachePath = checkCacheModule(optional) var cachePath = checkCacheModule(optional)
if (cachePath) { return _requireFile(new File(cachePath), optional) } var cacheFile = new File(cachePath)
if (cachePath && cacheFile.exists()) { return _requireFile(cacheFile, optional) }
// search module
name = checkCoreModule(name, path, optional) name = checkCoreModule(name, path, optional)
var file = new File(name) if ((file = resolve(name, path, optional)) === undefined) {
file = _isFile(file) ? file : resolve(name, path, optional)
if (file === undefined) {
// excloud local dir, prevent too many recursive call and cache not found module // excloud local dir, prevent too many recursive call and cache not found module
if (optional.local || optional.recursive || notFoundModules[name]) { if (optional.local || optional.recursive || notFoundModules[name]) {
console.log(name, path, optional, notFoundModules[name]) console.log(name, path, optional, notFoundModules[name])
@ -391,12 +395,24 @@
throw new FileNotFoundException("Can't found module " + name + ' in directory ' + path + ' ERROR: ' + ex) throw new FileNotFoundException("Can't found module " + name + ' in directory ' + path + ' ERROR: ' + ex)
} }
} }
var parent = cacheModuleIds[optional.parentId] setCacheModule(file, optional)
if (!parent) { cacheModuleIds[optional.parentId] = {} }
cacheModuleIds[optional.parentId][optional.path] = _canonical(file)
return _requireFile(file, optional) return _requireFile(file, optional)
} }
/**
* 设置模块缓存
* @param {any} file
* @param {any} optional
*/
function setCacheModule(file, optional) {
if (optional.local) {
var parent = cacheModuleIds[optional.parentId]
if (!parent) { cacheModuleIds[optional.parentId] = {} }
return cacheModuleIds[optional.parentId][optional.path] = _canonical(file)
}
return cacheModuleIds[optional.path] = _canonical(file)
}
function _requireFile(file, optional) { function _requireFile(file, optional) {
// 重定向文件名称和类型 // 重定向文件名称和类型
return getCacheModule(_canonical(file), file.name.split('.')[0], file, optional) return getCacheModule(_canonical(file), file.name.split('.')[0], file, optional)
@ -440,7 +456,7 @@
function __DynamicDisable__() { function __DynamicDisable__() {
// @ts-ignore // @ts-ignore
base.save(cacheModuleIdsFile, JSON.stringify(cacheModuleIds)) base.save(cacheModuleIdsFile, JSON.stringify(upgradeMode ? {} : cacheModuleIds))
for (var cacheModule in cacheModules) { for (var cacheModule in cacheModules) {
delete cacheModules[cacheModule] delete cacheModules[cacheModule]
} }
@ -452,6 +468,10 @@
notFoundModules = undefined notFoundModules = undefined
} }
function __setUpgradeMode__(status) {
upgradeMode = status
}
/** /**
* @param {string} parent * @param {string} parent
* @param {string} parentId * @param {string} parentId
@ -464,6 +484,7 @@
require.resolve = __DynamicResolve__ require.resolve = __DynamicResolve__
require.clear = __DynamicClear__ require.clear = __DynamicClear__
require.disable = __DynamicDisable__ require.disable = __DynamicDisable__
require.setUpgradeMode = __setUpgradeMode__
require.core_modules = CoreModules require.core_modules = CoreModules
return require return require
} }
@ -484,6 +505,7 @@
* @type {{[key:string]:boolean}} notFoundModules * @type {{[key:string]:boolean}} notFoundModules
*/ */
var notFoundModules = {} var notFoundModules = {}
var upgradeMode = false
console.info('Initialization require module. ParentDir:', _canonical(parent)) console.info('Initialization require module. ParentDir:', _canonical(parent))
console.info('Require module env list:') console.info('Require module env list:')
console.info('- NODE_PATH:', NODE_PATH) console.info('- NODE_PATH:', NODE_PATH)