module.ts 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * @Author HonorLee (dev@honorlee.me)
  3. * @Version 1.0 (2018-05-04)
  4. * @License MIT
  5. */
  6. export default class Module{
  7. public static init(){
  8. //定义全局Module载入语法糖
  9. (global as any).M = function(moduleName:string){
  10. if(!moduleName){
  11. LOGGER.error('Module name undefined!!');
  12. return null;
  13. }
  14. moduleName = moduleName.toLowerCase();
  15. const ext = moduleName.includes('.js')?'':'.js';
  16. const filePath = `${SYSTEM.MVC.PATH.MODULE}/${moduleName}${ext}`;
  17. try{
  18. FILE.statSync(filePath);
  19. return require(filePath);
  20. }catch(e:any){
  21. if(e.code!='ENOENT'){
  22. LOGGER.error('Load moduleName file error:');
  23. if(e.stack) LOGGER.error(e.stack);
  24. }else{
  25. LOGGER.error('No such module ['+moduleName+'],please check the moduleName file name,all letter must be lowercase');
  26. }
  27. return null;
  28. }
  29. }
  30. }
  31. }