checkcomponents.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const path = require('path')
  2. const _ = require('./utils')
  3. const config = require('./config')
  4. const srcPath = config.srcPath
  5. let hasCheckCompoenntMap = {}
  6. /**
  7. * 获取 json 路径相关信息
  8. */
  9. function getJsonPathInfo(jsonPath) {
  10. const dirPath = path.dirname(jsonPath)
  11. const fileName = path.basename(jsonPath, '.json')
  12. const relative = path.relative(srcPath, dirPath)
  13. const fileBase = path.join(relative, fileName)
  14. return {
  15. dirPath, fileName, relative, fileBase
  16. }
  17. }
  18. /**
  19. * 检测是否包含其他自定义组件
  20. */
  21. const checkProps = ['usingComponents', 'componentGenerics']
  22. async function checkIncludedComponents(jsonPath, componentListMap) {
  23. const json = _.readJson(jsonPath)
  24. if (!json) throw new Error(`json is not valid: "${jsonPath}"`)
  25. const {dirPath, fileName, fileBase} = getJsonPathInfo(jsonPath)
  26. if (hasCheckCompoenntMap[fileBase]) return
  27. hasCheckCompoenntMap[fileBase] = true
  28. for (let i = 0, len = checkProps.length; i < len; i++) {
  29. const checkProp = checkProps[i]
  30. const checkPropValue = json[checkProp] || {}
  31. const keys = Object.keys(checkPropValue)
  32. for (let j = 0, jlen = keys.length; j < jlen; j++) {
  33. const key = keys[j]
  34. let value = typeof checkPropValue[key] === 'object' ? checkPropValue[key].default : checkPropValue[key]
  35. if (!value || typeof value === 'boolean') continue
  36. value = _.transformPath(value, path.sep)
  37. // 检查相对路径
  38. const componentPath = `${path.join(dirPath, value)}.json`
  39. const isExists = await _.checkFileExists(componentPath)
  40. if (isExists) {
  41. await checkIncludedComponents(componentPath, componentListMap)
  42. }
  43. }
  44. }
  45. const wholeFileBase = path.join(dirPath, fileName)
  46. let jsExt = '.js'
  47. const isJsFileExists = await _.checkFileExists(wholeFileBase + '.ts')
  48. if (isJsFileExists) {
  49. jsExt = '.ts'
  50. }
  51. // 进入存储
  52. componentListMap.wxmlFileList.push(`${fileBase}.wxml`)
  53. componentListMap.wxssFileList.push(`${fileBase}.scss`)
  54. componentListMap.jsonFileList.push(`${fileBase}.json`)
  55. componentListMap.jsFileList.push(`${fileBase}${jsExt}`)
  56. componentListMap.jsFileMap[fileBase] = `${wholeFileBase}${jsExt}`
  57. componentListMap.wxsFileList.push(`${fileBase}.wxs`)
  58. }
  59. module.exports = async function (entry) {
  60. const componentListMap = {
  61. wxmlFileList: [],
  62. wxssFileList: [],
  63. jsonFileList: [],
  64. jsFileList: [],
  65. wxsFileList:[],
  66. jsFileMap: {}, // 为 webpack entry 所用
  67. }
  68. const isExists = await _.checkFileExists(entry)
  69. if (!isExists) {
  70. const {dirPath, fileName, fileBase} = getJsonPathInfo(entry)
  71. const wholeFileBase = path.join(dirPath, fileName)
  72. let jsExt = '.js'
  73. const isJsFileExists = await _.checkFileExists(wholeFileBase + '.ts')
  74. if (isJsFileExists) {
  75. jsExt = '.ts'
  76. }
  77. componentListMap.jsFileList.push(`${fileBase}${jsExt}`)
  78. componentListMap.jsFileMap[fileBase] = `${wholeFileBase}${jsExt}`
  79. return componentListMap
  80. }
  81. hasCheckCompoenntMap = {}
  82. await checkIncludedComponents(entry, componentListMap)
  83. return componentListMap
  84. }