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

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

const {
  getPageSet,
  getJsonFileMap,
10 11
  getChangedJsonFileMap,
  supportGlobalUsingComponents
fxy060608's avatar
fxy060608 已提交
12 13 14
} = require('@dcloudio/uni-cli-shared/lib/cache')

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

const usingComponentsMap = {}

19 20 21 22 23 24 25 26 27 28
// 百度小程序动态组件库 usingSwanComponents 引用组件
const mpBaiduDynamicLibs = [
  'dynamicLib://editorLib/editor',
  'dynamicLib://echartsLib/chart',
  'dynamicLib://myModelviewer/modelviewer',
  'dynamicLib://myDynamicLib/panoviewer',
  'dynamicLib://myDynamicLib/spintileviewer',
  'dynamicLib://myDynamicLib/vrvideo'
]

29 30
const AnalyzeDependency = require('@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/index');

Q
qiang 已提交
31
function analyzeUsingComponents () {
fxy060608's avatar
fxy060608 已提交
32 33 34 35 36 37 38
  if (!process.env.UNI_OPT_SUBPACKAGES) {
    return
  }
  const pageSet = getPageSet()
  const jsonFileMap = getJsonFileMap()

  // 生成所有组件引用关系
fxy060608's avatar
fxy060608 已提交
39
  for (const name of jsonFileMap.keys()) {
fxy060608's avatar
fxy060608 已提交
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
    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)
67
      if (pkgs.size > 1) { // 被多个分包引用
fxy060608's avatar
fxy060608 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        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 已提交
98
  //   }, {})
fxy060608's avatar
fxy060608 已提交
99 100
}

101 102
const parseRequirePath = path => /^[A-z]/.test(path) ? `./${path}` : path

Q
qiang 已提交
103
function normalizeUsingComponents (file, usingComponents) {
fxy060608's avatar
fxy060608 已提交
104 105 106 107 108 109
  const names = Object.keys(usingComponents)
  if (!names.length) {
    return usingComponents
  }
  file = path.dirname('/' + file)
  names.forEach(name => {
110
    usingComponents[name] = normalizePath(parseRequirePath(path.relative(file, usingComponents[name])))
fxy060608's avatar
fxy060608 已提交
111 112 113 114
  })
  return usingComponents
}

S
songyu 已提交
115
const cacheFileMap = new Map();
Q
qiang 已提交
116
module.exports = function generateJson (compilation) {
fxy060608's avatar
fxy060608 已提交
117 118
  analyzeUsingComponents()

S
songyu 已提交
119
  const emitFileMap = new Map([...cacheFileMap]);
fxy060608's avatar
fxy060608 已提交
120
  const jsonFileMap = getChangedJsonFileMap()
fxy060608's avatar
fxy060608 已提交
121
  for (const name of jsonFileMap.keys()) {
fxy060608's avatar
fxy060608 已提交
122
    const jsonObj = JSON.parse(jsonFileMap.get(name))
123
    if (process.env.UNI_PLATFORM === 'app-plus') { // App平台默认增加usingComponents,激活__wxAppCode__
Q
qiang 已提交
124
      jsonObj.usingComponents = jsonObj.usingComponents || {}
125
    }
fxy060608's avatar
fxy060608 已提交
126
    // customUsingComponents
fxy060608's avatar
fxy060608 已提交
127
    if (jsonObj.customUsingComponents && Object.keys(jsonObj.customUsingComponents).length) {
fxy060608's avatar
fxy060608 已提交
128 129 130 131
      jsonObj.usingComponents = Object.assign(jsonObj.customUsingComponents, jsonObj.usingComponents)
    }
    delete jsonObj.customUsingComponents
    // usingGlobalComponents
132
    if (!supportGlobalUsingComponents && jsonObj.usingGlobalComponents && Object.keys(jsonObj.usingGlobalComponents).length) {
fxy060608's avatar
fxy060608 已提交
133 134 135
      jsonObj.usingComponents = Object.assign(jsonObj.usingGlobalComponents, jsonObj.usingComponents)
    }

136 137 138 139 140 141
    // usingAutoImportComponents
    if (jsonObj.usingAutoImportComponents && Object.keys(jsonObj.usingAutoImportComponents).length) {
      jsonObj.usingComponents = Object.assign(jsonObj.usingAutoImportComponents, jsonObj.usingComponents)
    }
    delete jsonObj.usingAutoImportComponents

142 143 144 145 146 147
    // 百度小程序插件内组件使用 usingSwanComponents
    if (process.env.UNI_PLATFORM === 'mp-baidu') {
      const usingComponents = jsonObj.usingComponents || {}
      Object.keys(usingComponents).forEach(key => {
        const value = usingComponents[key]
        if (value.includes('://')) {
148
          /**
149 150 151
           * 部分动态库组件(如:editor)使用‘usingSwanComponents’ 引入
           * 部分动态库组件(如:swan-sitemap-list)使用'usingComponents'引入
           * 做白名单机制
152
           */
153 154 155 156 157
          if (mpBaiduDynamicLibs.includes(value)) {
            delete usingComponents[key]
            jsonObj.usingSwanComponents = jsonObj.usingSwanComponents || {}
            jsonObj.usingSwanComponents[key] = value
          }
158 159 160
        }
      })
    }
161 162 163 164 165 166 167 168
    // fix mp-alipay plugin
    if (process.env.UNI_PLATFORM === 'mp-alipay') {
      const usingComponents = jsonObj.usingComponents || {}
      if (Object.values(usingComponents).find(value => value.startsWith('plugin://'))) {
        const componentName = 'plugin-wrapper'
        usingComponents[componentName] = '/' + componentName
      }
    }
169

fxy060608's avatar
fxy060608 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    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] = {
Q
qiang 已提交
197
          size () {
fxy060608's avatar
fxy060608 已提交
198 199
            return Buffer.byteLength(scopedSlotComponentJsonSource, 'utf8')
          },
Q
qiang 已提交
200
          source () {
fxy060608's avatar
fxy060608 已提交
201 202 203 204 205 206 207
            return scopedSlotComponentJsonSource
          }
        }
      })
    }

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

fxy060608's avatar
fxy060608 已提交
209 210 211 212
    if (process.env.UNI_PLATFORM !== 'app-plus' && process.env.UNI_PLATFORM !== 'h5') {
      delete jsonObj.navigationBarShadow
    }

213
    if ((process.env.UNI_SUBPACKGE || process.env.UNI_MP_PLUGIN) && jsonObj.usingComponents) {
fxy060608's avatar
fxy060608 已提交
214
      jsonObj.usingComponents = normalizeUsingComponents(name, jsonObj.usingComponents)
fxy060608's avatar
fxy060608 已提交
215
    }
216 217

    emitFileMap.set(name, jsonObj);
S
songyu 已提交
218
    cacheFileMap.set(name, JSON.parse(JSON.stringify(jsonObj))); // 做一次拷贝,emitFileMap中内容在后面会被修改
219 220 221 222 223 224
  }


  // 组件依赖分析
  (new AnalyzeDependency()).init(emitFileMap, compilation);

225 226 227 228 229 230 231 232 233 234
    for (const [name, jsonObj] of emitFileMap) {      
      if (name === 'app.json') { // 删除manifest.json携带的配置项
        delete jsonObj.insertAppCssToIndependentSwitch;
        delete jsonObj.independentSwitch;
        delete jsonObj.copyWxComponentsOnDemandSwitch;
      } else { // 删除用于临时记录的属性
        delete jsonObj.usingGlobalComponents;
      }
      emit(name, jsonObj, compilation);      
    }
235 236 237 238 239 240 241 242 243 244 245 246 247

  if (process.env.UNI_USING_CACHE && jsonFileMap.size) {
    setTimeout(() => {
      require('@dcloudio/uni-cli-shared/lib/cache').store()
    }, 50)
  }
}

function emit (name, jsonObj, compilation) {
  if (jsonObj.usingComponents) {
    jsonObj.usingComponents = Object.assign({}, jsonObj.usingComponents);
  }
  const source = JSON.stringify(jsonObj, null, 2)
fxy060608's avatar
fxy060608 已提交
248 249 250

    const jsFile = name.replace('.json', '.js')
    if (
fxy060608's avatar
fxy060608 已提交
251 252 253 254 255 256 257 258 259
      ![
        'app.js',
        'manifest.js',
        'mini.project.js',
        'quickapp.config.js',
        'project.config.js',
        'project.swan.js'
      ].includes(
        jsFile) &&
fxy060608's avatar
fxy060608 已提交
260 261
      !compilation.assets[jsFile]
    ) {
fxy060608's avatar
fxy060608 已提交
262
      const jsFileAsset = {
Q
qiang 已提交
263
        size () {
fxy060608's avatar
fxy060608 已提交
264 265
          return Buffer.byteLength(EMPTY_COMPONENT, 'utf8')
        },
Q
qiang 已提交
266
        source () {
fxy060608's avatar
fxy060608 已提交
267 268 269
          return EMPTY_COMPONENT
        }
      }
fxy060608's avatar
fxy060608 已提交
270
      compilation.assets[jsFile] = jsFileAsset
fxy060608's avatar
fxy060608 已提交
271
    }
fxy060608's avatar
fxy060608 已提交
272
    const jsonAsset = {
Q
qiang 已提交
273
      size () {
fxy060608's avatar
fxy060608 已提交
274 275
        return Buffer.byteLength(source, 'utf8')
      },
Q
qiang 已提交
276
      source () {
fxy060608's avatar
fxy060608 已提交
277 278 279
        return source
      }
    }
fxy060608's avatar
fxy060608 已提交
280

281
  compilation.assets[name] = jsonAsset
282
}
283