generate-json.js 7.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
const path = require('path')

const {
  normalizePath
} = require('@dcloudio/uni-cli-shared')

const {
  getPageSet,
  getJsonFileMap,
  getChangedJsonFileMap
} = require('@dcloudio/uni-cli-shared/lib/cache')

// 主要解决 extends 且未实际引用的组件
fxy060608's avatar
fxy060608 已提交
14
const EMPTY_COMPONENT = 'Component({})'
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21 22 23 24 25

const usingComponentsMap = {}

function analyzeUsingComponents () {
  if (!process.env.UNI_OPT_SUBPACKAGES) {
    return
  }
  const pageSet = getPageSet()
  const jsonFileMap = getJsonFileMap()

  // 生成所有组件引用关系
fxy060608's avatar
fxy060608 已提交
26
  for (const name of jsonFileMap.keys()) {
fxy060608's avatar
fxy060608 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    const jsonObj = JSON.parse(jsonFileMap.get(name))
    const usingComponents = jsonObj.usingComponents
    if (!usingComponents || !pageSet.has(name)) {
      continue
    }
    // usingComponentsMap[name] = {}

    Object.keys(usingComponents).forEach(componentName => {
      const componentPath = usingComponents[componentName].slice(1)
      if (!usingComponentsMap[componentPath]) {
        usingComponentsMap[componentPath] = new Set()
      }
      usingComponentsMap[componentPath].add(name)
    })
  }

  const subPackageRoots = Object.keys(process.UNI_SUBPACKAGES)

  const findSubPackage = function (pages) {
    const pkgs = new Set()
    for (let i = 0; i < pages.length; i++) {
      const pagePath = pages[i]
      const pkgRoot = subPackageRoots.find(root => pagePath.indexOf(root) === 0)
      if (!pkgRoot) { // 被非分包引用
        return false
      }
      pkgs.add(pkgRoot)
      if (pkgs.length > 1) { // 被多个分包引用
        return false
      }
    }
    return [...pkgs][0]
  }

  Object.keys(usingComponentsMap).forEach(componentName => {
    const subPackage = findSubPackage([...usingComponentsMap[componentName]])
    if (subPackage && componentName.indexOf(subPackage) !== 0) { // 仅存在一个子包引用且未在该子包
      console.warn(`自定义组件 ${componentName} 建议移动到子包 ${subPackage} 内`)
    }
  })

  // 生成所有组件递归引用关系
  //   Object.keys(usingComponentsMap).forEach(name => {
  //     Object.keys(usingComponentsMap[name]).forEach(componentName => {
  //       const usingComponents = usingComponentsMap[componentName.slice(1)]
  //       if (usingComponents) {
  //         usingComponentsMap[name][componentName] = usingComponents
  //       }
  //     })
  //   })
  //
  //   // 生成页面组件引用关系
  //   const pageSet = getPageSet()
  //   const pagesUsingComponents = Object.keys(usingComponentsMap).reduce((pages, name) => {
  //     if (pageSet.has(name)) {
  //       pages[name] = usingComponentsMap[name]
  //     }
  //     return pages
fxy060608's avatar
fxy060608 已提交
85
  //   }, {})
fxy060608's avatar
fxy060608 已提交
86 87
}

fxy060608's avatar
fxy060608 已提交
88 89 90 91 92 93 94 95 96 97 98 99
function normalizeUsingComponents (file, usingComponents) {
  const names = Object.keys(usingComponents)
  if (!names.length) {
    return usingComponents
  }
  file = path.dirname('/' + file)
  names.forEach(name => {
    usingComponents[name] = path.relative(file, usingComponents[name])
  })
  return usingComponents
}

fxy060608's avatar
fxy060608 已提交
100 101 102 103
module.exports = function generateJson (compilation) {
  analyzeUsingComponents()

  const jsonFileMap = getChangedJsonFileMap()
fxy060608's avatar
fxy060608 已提交
104
  for (const name of jsonFileMap.keys()) {
fxy060608's avatar
fxy060608 已提交
105
    const jsonObj = JSON.parse(jsonFileMap.get(name))
106
    if (process.env.UNI_PLATFORM === 'app-plus') { // App平台默认增加usingComponents,激活__wxAppCode__
Q
qiang 已提交
107
      jsonObj.usingComponents = jsonObj.usingComponents || {}
108
    }
fxy060608's avatar
fxy060608 已提交
109
    // customUsingComponents
fxy060608's avatar
fxy060608 已提交
110
    if (jsonObj.customUsingComponents && Object.keys(jsonObj.customUsingComponents).length) {
fxy060608's avatar
fxy060608 已提交
111 112 113 114
      jsonObj.usingComponents = Object.assign(jsonObj.customUsingComponents, jsonObj.usingComponents)
    }
    delete jsonObj.customUsingComponents
    // usingGlobalComponents
fxy060608's avatar
fxy060608 已提交
115
    if (jsonObj.usingGlobalComponents && Object.keys(jsonObj.usingGlobalComponents).length) {
fxy060608's avatar
fxy060608 已提交
116 117 118 119
      jsonObj.usingComponents = Object.assign(jsonObj.usingGlobalComponents, jsonObj.usingComponents)
    }
    delete jsonObj.usingGlobalComponents

120 121 122 123 124 125
    // usingAutoImportComponents
    if (jsonObj.usingAutoImportComponents && Object.keys(jsonObj.usingAutoImportComponents).length) {
      jsonObj.usingComponents = Object.assign(jsonObj.usingAutoImportComponents, jsonObj.usingComponents)
    }
    delete jsonObj.usingAutoImportComponents

126 127 128 129 130 131
    // 百度小程序插件内组件使用 usingSwanComponents
    if (process.env.UNI_PLATFORM === 'mp-baidu') {
      const usingComponents = jsonObj.usingComponents || {}
      Object.keys(usingComponents).forEach(key => {
        const value = usingComponents[key]
        if (value.includes('://')) {
132 133 134 135 136 137
          /**
           * 百度小程序部分组件(如:editor)使用‘usingSwanComponents’ 引入
           * 部分组件(如:swan-sitemap-list)使用'usingComponents'引入
           * 经测试,两者保留都不会报错,因此去除以下 delete 语句
           */
          // delete usingComponents[key]
138 139 140 141 142 143
          jsonObj.usingSwanComponents = jsonObj.usingSwanComponents || {}
          jsonObj.usingSwanComponents[key] = value
        }
      })
    }

fxy060608's avatar
fxy060608 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    if (jsonObj.genericComponents && jsonObj.genericComponents.length) { // scoped slots
      // 生成genericComponents json
      const genericComponents = Object.create(null)

      const scopedSlotComponents = []
      jsonObj.genericComponents.forEach(genericComponentName => {
        const genericComponentFile = normalizePath(
          path.join(path.dirname(name), genericComponentName + '.json')
        )
        genericComponents[genericComponentName] = '/' +
          genericComponentFile.replace(
            path.extname(genericComponentFile), ''
          )
        scopedSlotComponents.push(genericComponentFile)
      })

      jsonObj.usingComponents = Object.assign(genericComponents, jsonObj.usingComponents)

      const scopedSlotComponentJson = {
        component: true,
        usingComponents: jsonObj.usingComponents
      }

      const scopedSlotComponentJsonSource = JSON.stringify(scopedSlotComponentJson, null, 2)

      scopedSlotComponents.forEach(scopedSlotComponent => {
        compilation.assets[scopedSlotComponent] = {
          size () {
            return Buffer.byteLength(scopedSlotComponentJsonSource, 'utf8')
          },
          source () {
            return scopedSlotComponentJsonSource
          }
        }
      })
    }

    delete jsonObj.genericComponents
fxy060608's avatar
fxy060608 已提交
182

fxy060608's avatar
fxy060608 已提交
183 184 185 186
    if (process.env.UNI_PLATFORM !== 'app-plus' && process.env.UNI_PLATFORM !== 'h5') {
      delete jsonObj.navigationBarShadow
    }

fxy060608's avatar
fxy060608 已提交
187 188
    if (process.env.UNI_SUBPACKGE && jsonObj.usingComponents) {
      jsonObj.usingComponents = normalizeUsingComponents(name, jsonObj.usingComponents)
fxy060608's avatar
fxy060608 已提交
189
    }
fxy060608's avatar
fxy060608 已提交
190 191 192 193
    const source = JSON.stringify(jsonObj, null, 2)

    const jsFile = name.replace('.json', '.js')
    if (
fxy060608's avatar
fxy060608 已提交
194 195 196 197 198 199 200 201 202
      ![
        'app.js',
        'manifest.js',
        'mini.project.js',
        'quickapp.config.js',
        'project.config.js',
        'project.swan.js'
      ].includes(
        jsFile) &&
fxy060608's avatar
fxy060608 已提交
203 204
      !compilation.assets[jsFile]
    ) {
fxy060608's avatar
fxy060608 已提交
205
      const jsFileAsset = {
fxy060608's avatar
fxy060608 已提交
206 207 208 209 210 211 212
        size () {
          return Buffer.byteLength(EMPTY_COMPONENT, 'utf8')
        },
        source () {
          return EMPTY_COMPONENT
        }
      }
fxy060608's avatar
fxy060608 已提交
213
      compilation.assets[jsFile] = jsFileAsset
fxy060608's avatar
fxy060608 已提交
214
    }
fxy060608's avatar
fxy060608 已提交
215
    const jsonAsset = {
fxy060608's avatar
fxy060608 已提交
216 217 218 219 220 221 222
      size () {
        return Buffer.byteLength(source, 'utf8')
      },
      source () {
        return source
      }
    }
fxy060608's avatar
fxy060608 已提交
223 224 225 226 227

    compilation.assets[name] = jsonAsset
  }
  if (process.env.UNI_USING_CACHE && jsonFileMap.size) {
    setTimeout(() => {
fxy060608's avatar
fxy060608 已提交
228
      require('@dcloudio/uni-cli-shared/lib/cache').store()
fxy060608's avatar
fxy060608 已提交
229
    }, 50)
fxy060608's avatar
fxy060608 已提交
230
  }
231
}