cache.js 9.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8 9
/**
 * 1.page-loader 缓存基础的  app.json page.json project.config.json
 * 2.main-loader 缓存 app.json 中的 usingComponents 节点
 * 3.script-loader 修改缓存 usingComponents 节点
 * 5.webpack plugin 中获取被修改的 page.json,component.json 并 emitFile
 */
fxy060608's avatar
fxy060608 已提交
10
let jsonFileMap = new Map()
fxy060608's avatar
fxy060608 已提交
11
const changedJsonFileSet = new Set()
fxy060608's avatar
fxy060608 已提交
12
let componentSet = new Set()
fxy060608's avatar
fxy060608 已提交
13

fxy060608's avatar
fxy060608 已提交
14
let pageSet = new Set()
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21 22 23 24 25 26 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

let globalUsingComponents = Object.create(null)
let appJsonUsingComponents = Object.create(null)
let componentSpecialMethods = Object.create(null)

function getPagesJson () {
  if (process.env.UNI_PLATFORM === 'h5') {
    return process.UNI_H5_PAGES_JSON
  }
  const pagesJson = {
    pages: {}
  }
  for (let name of pageSet.values()) {
    const style = JSON.parse(getJsonFile(name) || '{}')
    delete style.customUsingComponents
    pagesJson.pages[name] = style
  }
  const appJson = JSON.parse(getJsonFile('app') || '{}')
  pagesJson.globalStyle = appJson['window'] || {}
  return pagesJson
}

function updateJsonFile (name, jsonStr) {
  changedJsonFileSet.add(name)
  if (typeof jsonStr !== 'string') {
    jsonStr = JSON.stringify(jsonStr, null, 2)
  }
  jsonFileMap.set(name, jsonStr)
}

function getJsonFile (name) {
  return jsonFileMap.get(name)
}

function getChangedJsonFileMap (clear = true) {
  const changedJsonFileMap = new Map()
  for (let name of changedJsonFileSet.values()) {
    changedJsonFileMap.set(name + '.json', jsonFileMap.get(name))
  }
  clear && changedJsonFileSet.clear()
  return changedJsonFileMap
}

function updateAppJson (name, jsonObj) {
fxy060608's avatar
fxy060608 已提交
59
  updateComponentJson(name, jsonObj, true, 'App')
fxy060608's avatar
fxy060608 已提交
60 61 62 63
}

function updatePageJson (name, jsonObj) {
  pageSet.add(name)
fxy060608's avatar
fxy060608 已提交
64
  updateComponentJson(name, jsonObj, true, 'Page')
fxy060608's avatar
fxy060608 已提交
65 66 67
}

function updateProjectJson (name, jsonObj) {
fxy060608's avatar
fxy060608 已提交
68
  updateComponentJson(name, jsonObj, false, 'Project')
fxy060608's avatar
fxy060608 已提交
69 70 71 72
}

const supportGlobalUsingComponents = process.env.UNI_PLATFORM === 'mp-weixin' || process.env.UNI_PLATFORM === 'mp-qq'

fxy060608's avatar
fxy060608 已提交
73 74 75 76 77 78 79 80 81 82
function updateComponentJson (name, jsonObj, usingComponents = true, type = 'Component') {
  if (type === 'Component') {
    jsonObj.component = true
  }
  if (type === 'Page') {
    if (process.env.UNI_PLATFORM === 'mp-baidu') {
      jsonObj.component = true
    }
  }

fxy060608's avatar
fxy060608 已提交
83 84 85
  const oldJsonStr = getJsonFile(name)
  if (oldJsonStr) { // update
    if (usingComponents) { // merge usingComponents
86 87 88 89 90
      const oldJsonObj = JSON.parse(oldJsonStr)
      jsonObj.usingComponents = oldJsonObj.usingComponents || {}
      if (oldJsonObj.usingGlobalComponents) { // 复制 global components(针对不支持全局 usingComponents 的平台)
        jsonObj.usingGlobalComponents = oldJsonObj.usingGlobalComponents
      }
fxy060608's avatar
fxy060608 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    }
    const newJsonStr = JSON.stringify(jsonObj, null, 2)
    if (newJsonStr !== oldJsonStr) {
      updateJsonFile(name, newJsonStr)
    }
  } else { // add
    updateJsonFile(name, jsonObj)
  }
}

function updateUsingGlobalComponents (name, usingGlobalComponents) {
  if (supportGlobalUsingComponents) {
    return
  }
  const oldJsonStr = getJsonFile(name)
  if (oldJsonStr) { // update
    const jsonObj = JSON.parse(oldJsonStr)
    jsonObj.usingGlobalComponents = usingGlobalComponents
    const newJsonStr = JSON.stringify(jsonObj, null, 2)
    if (newJsonStr !== oldJsonStr) {
      updateJsonFile(name, newJsonStr)
    }
  } else { // add
    const jsonObj = {
      usingGlobalComponents
    }
    updateJsonFile(name, jsonObj)
  }
}

function updateUsingComponents (name, usingComponents, type) {
  if (type === 'Component') {
    componentSet.add(name)
  }
  if (type === 'App') { // 记录全局组件
    globalUsingComponents = usingComponents
  }

  const oldJsonStr = getJsonFile(name)
  if (oldJsonStr) { // update
    const jsonObj = JSON.parse(oldJsonStr)
    if (type === 'Component') {
      jsonObj.component = true
    } else if (type === 'Page') {
      if (process.env.UNI_PLATFORM === 'mp-baidu') {
        jsonObj.component = true
      }
    }

    jsonObj.usingComponents = usingComponents
    const newJsonStr = JSON.stringify(jsonObj, null, 2)
    if (newJsonStr !== oldJsonStr) {
      updateJsonFile(name, newJsonStr)
    }
  } else { // add
    const jsonObj = {
      usingComponents
    }
    if (type === 'Component') {
      jsonObj.component = true
    } else if (type === 'Page') {
      if (process.env.UNI_PLATFORM === 'mp-baidu') {
        jsonObj.component = true
      }
    }

    updateJsonFile(name, jsonObj)
  }
}

function updateComponentGenerics (name, componentGenerics) {
  const oldJsonStr = getJsonFile(name)
  if (oldJsonStr) { // update
    const jsonObj = JSON.parse(oldJsonStr)
    jsonObj.componentGenerics = componentGenerics
    const newJsonStr = JSON.stringify(jsonObj, null, 2)
    if (newJsonStr !== oldJsonStr) {
      updateJsonFile(name, newJsonStr)
    }
  } else { // add
    const jsonObj = {
      componentGenerics
    }
    updateJsonFile(name, jsonObj)
  }
}

function updateGenericComponents (name, genericComponents) {
  const oldJsonStr = getJsonFile(name)
  if (oldJsonStr) { // update
    const jsonObj = JSON.parse(oldJsonStr)
    jsonObj.genericComponents = genericComponents
    const newJsonStr = JSON.stringify(jsonObj, null, 2)
    if (newJsonStr !== oldJsonStr) {
      updateJsonFile(name, newJsonStr)
    }
  } else { // add
    const jsonObj = {
      genericComponents
    }
    updateJsonFile(name, jsonObj)
  }
}

function updateAppJsonUsingComponents (usingComponents) {
  appJsonUsingComponents = usingComponents
}

function getComponentSet () {
  return componentSet
}

function getGlobalUsingComponents () {
  // 合并 app.json ,main.js 全局组件
  return Object.assign({}, appJsonUsingComponents, globalUsingComponents)
}

function getWXComponents (name) {
  const oldJsonStr = getJsonFile(name)
  if (oldJsonStr) {
    const jsonObj = JSON.parse(oldJsonStr)
    if (jsonObj.customUsingComponents) {
      return Object.assign({}, appJsonUsingComponents, jsonObj.customUsingComponents)
    }
  }
  return Object.assign({}, appJsonUsingComponents)
}

function updateSpecialMethods (name, specialMethods) {
  if (specialMethods.length) {
    componentSpecialMethods[name] = specialMethods
  } else {
    delete componentSpecialMethods[name]
  }
}

function getSpecialMethods (name) {
  if (!name) {
    return componentSpecialMethods
  }
  return componentSpecialMethods[name] || []
}

fxy060608's avatar
fxy060608 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
const pagesJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'pages.json')

const cacheTypes = ['babel-loader', 'css-loader', 'uni-template-compiler', 'vue-loader']

function clearCache () {
  const fsExtra = require('fs-extra')
  cacheTypes.forEach(cacheType => {
    fsExtra.emptyDirSync(path.resolve(
      process.env.UNI_CLI_CONTEXT,
      'node_modules/.cache/' + cacheType + '/' + process.env.UNI_PLATFORM
    ))
  })
}

function digest (str) {
  return crypto
    .createHash('md5')
    .update(str)
    .digest('hex')
}

fxy060608's avatar
fxy060608 已提交
255 256 257 258 259 260 261
module.exports = {
  getPageSet () {
    return pageSet
  },
  getJsonFileMap () {
    return jsonFileMap
  },
fxy060608's avatar
fxy060608 已提交
262 263 264
  // 先简单处理,该方案不好,
  // 后续为 pages-loader 增加 cache-loader,
  // 然后其他修改 json 的地方也要定制 cache-loader
fxy060608's avatar
fxy060608 已提交
265
  store () {
fxy060608's avatar
fxy060608 已提交
266 267 268 269 270 271
    const filepath = path.resolve(
      process.env.UNI_CLI_CONTEXT,
      'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM,
      digest(process.env.UNI_INPUT_DIR) + '.json'
    )

fxy060608's avatar
fxy060608 已提交
272 273 274 275
    const files = Array.from(jsonFileMap.entries())
    const pages = Array.from(pageSet)
    const components = Array.from(componentSet)
    const methods = componentSpecialMethods
fxy060608's avatar
fxy060608 已提交
276 277
    fs.writeFileSync(filepath, JSON.stringify({
      mtimeMs: fs.statSync(pagesJsonPath).mtimeMs,
fxy060608's avatar
fxy060608 已提交
278 279 280 281 282 283
      files,
      pages,
      components,
      methods,
      globalUsingComponents,
      appJsonUsingComponents
fxy060608's avatar
fxy060608 已提交
284
    }))
fxy060608's avatar
fxy060608 已提交
285
  },
fxy060608's avatar
fxy060608 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  restore () {
    const filepath = path.resolve(
      process.env.UNI_CLI_CONTEXT,
      'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM,
      digest(process.env.UNI_INPUT_DIR) + '.json'
    )
    if (!fs.existsSync(filepath)) {
      try {
        clearCache()
      } catch (e) {}
      return
    }
    const mtimeMs = fs.statSync(pagesJsonPath).mtimeMs
    const jsonCache = require(filepath)
    if (jsonCache.mtimeMs !== mtimeMs) {
      try {
        clearCache()
      } catch (e) {}
      return
    }
fxy060608's avatar
fxy060608 已提交
306 307 308 309 310 311 312 313 314 315
    jsonFileMap = new Map(jsonCache.files)
    pageSet = new Set(jsonCache.pages)
    componentSet = new Set(jsonCache.components)
    componentSpecialMethods = jsonCache.methods
    globalUsingComponents = jsonCache.globalUsingComponents
    appJsonUsingComponents = jsonCache.appJsonUsingComponents
    // restore 时,所有 file 均触发 change
    for (let name of jsonFileMap.keys()) {
      changedJsonFileSet.add(name)
    }
fxy060608's avatar
fxy060608 已提交
316
    return true
fxy060608's avatar
fxy060608 已提交
317
  },
fxy060608's avatar
fxy060608 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
  getJsonFile,
  getPagesJson,
  getComponentSet,
  getWXComponents,
  getGlobalUsingComponents,
  updateAppJson,
  updatePageJson,
  updateProjectJson,
  updateComponentJson,
  updateSpecialMethods,
  updateUsingComponents,
  updateUsingGlobalComponents,
  updateAppJsonUsingComponents,
  updateComponentGenerics,
  updateGenericComponents,
  getChangedJsonFileMap,
  getSpecialMethods
}