mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
85
86
87
88
89
90
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const {
  isNormalPage
} = require('./util')
/**
 * 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
 */
let jsonFileMap = new Map()
const changedJsonFileSet = new Set()
let componentSet = new Set()
 
let pageSet = new Set()
 
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 (const 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 (const name of changedJsonFileSet.values()) {
    if (isNormalPage(name)) {
      changedJsonFileMap.set(name + '.json', jsonFileMap.get(name))
    }
  }
  clear && changedJsonFileSet.clear()
  return changedJsonFileMap
}
 
function updateAppJson (name, jsonObj) {
  updateComponentJson(name, jsonObj, true, 'App')
}
 
function updatePageJson (name, jsonObj) {
  pageSet.add(name)
  updateComponentJson(name, jsonObj, true, 'Page')
}
 
function updateProjectJson (name, jsonObj) {
  updateComponentJson(name, jsonObj, false, 'Project')
}
 
const supportGlobalUsingComponents = process.env.UNI_PLATFORM === 'mp-weixin' || process.env.UNI_PLATFORM === 'mp-qq'
 
function updateComponentJson (name, jsonObj, usingComponents = true, type = 'Component') {
  if (type === 'Component') {
    jsonObj.component = true
    if (process.env.UNI_PLATFORM === 'mp-alipay') {
      const manifestConfig = process.UNI_MANIFEST
      const alipayConfig = manifestConfig['mp-alipay'] || {}
      jsonObj.styleIsolation = alipayConfig.styleIsolation || 'apply-shared'
    }
  } else if (type === 'Page') {
    if (process.env.UNI_PLATFORM === 'mp-baidu') {
      jsonObj.component = true
    }
  }
 
  const oldJsonStr = getJsonFile(name)
  if (oldJsonStr) { // update
    if (usingComponents) { // merge usingComponents
      // 其实直接拿新的 merge 到旧的应该就行
      const oldJsonObj = JSON.parse(oldJsonStr)
      jsonObj.usingComponents = oldJsonObj.usingComponents || {}
      jsonObj.usingAutoImportComponents = oldJsonObj.usingAutoImportComponents || {}
      if (oldJsonObj.genericComponents) {
        jsonObj.genericComponents = oldJsonObj.genericComponents
      }
      if (oldJsonObj.usingGlobalComponents) { // 复制 global components(针对不支持全局 usingComponents 的平台)
        jsonObj.usingGlobalComponents = oldJsonObj.usingGlobalComponents
      }
    }
    const newJsonStr = JSON.stringify(jsonObj, null, 2)
    if (newJsonStr !== oldJsonStr) {
      updateJsonFile(name, newJsonStr)
    }
  } else { // add
    updateJsonFile(name, jsonObj)
  }
}
 
function updateUsingGlobalComponents (name, usingGlobalComponents) {
  const manifestConfig = process.UNI_MANIFEST
  const weixinConfig = manifestConfig['mp-weixin']
  const independentSwitch = !!weixinConfig.independent
 
  if (!independentSwitch && 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 updateUsingAutoImportComponents (name, usingAutoImportComponents) {
  const oldJsonStr = getJsonFile(name)
  if (oldJsonStr) { // update
    const jsonObj = JSON.parse(oldJsonStr)
    jsonObj.usingAutoImportComponents = usingAutoImportComponents
    const newJsonStr = JSON.stringify(jsonObj, null, 2)
    if (newJsonStr !== oldJsonStr) {
      updateJsonFile(name, newJsonStr)
    }
  } else { // add
    updateJsonFile(name, {
      usingAutoImportComponents
    })
  }
}
 
function updateUsingComponents (name, usingComponents, type) {
  if (type === 'Component') {
    componentSet.add(name)
  }
  if (type === 'App') { // 记录全局组件
    globalUsingComponents = usingComponents
  }
 
  const oldJsonStr = getJsonFile(name)
  const jsonObj = oldJsonStr ? JSON.parse(oldJsonStr) : {
    usingComponents
  }
  if (type === 'Component') {
    jsonObj.component = true
    if (process.env.UNI_PLATFORM === 'mp-alipay') {
      const manifestConfig = process.UNI_MANIFEST
      const alipayConfig = manifestConfig['mp-alipay'] || {}
      jsonObj.styleIsolation = alipayConfig.styleIsolation || 'apply-shared'
    }
  } else if (type === 'Page') {
    if (process.env.UNI_PLATFORM === 'mp-baidu') {
      jsonObj.component = true
    }
  }
  if (oldJsonStr) { // update
    jsonObj.usingComponents = usingComponents
    const newJsonStr = JSON.stringify(jsonObj, null, 2)
    if (newJsonStr !== oldJsonStr) {
      updateJsonFile(name, newJsonStr)
    }
  } else { // add
    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] || []
}
 
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')
}
 
module.exports = {
  getPageSet () {
    return pageSet
  },
  getJsonFileMap () {
    return jsonFileMap
  },
  // 先简单处理,该方案不好,
  // 后续为 pages-loader 增加 cache-loader,
  // 然后其他修改 json 的地方也要定制 cache-loader
  store () {
    const pagesJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'pages.json')
    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'
    )
 
    const files = Array.from(jsonFileMap.entries())
    const pages = Array.from(pageSet)
    const components = Array.from(componentSet)
    const methods = componentSpecialMethods
    fs.writeFileSync(filepath, JSON.stringify({
      mtimeMs: fs.statSync(pagesJsonPath).mtimeMs,
      files,
      pages,
      components,
      methods,
      globalUsingComponents,
      appJsonUsingComponents
    }))
  },
  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 pagesJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'pages.json')
    const mtimeMs = fs.statSync(pagesJsonPath).mtimeMs
    const jsonCache = require(filepath)
    if (jsonCache.mtimeMs !== mtimeMs) {
      try {
        clearCache()
      } catch (e) { }
      return
    }
    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 (const name of jsonFileMap.keys()) {
      changedJsonFileSet.add(name)
    }
    return true
  },
  getJsonFile,
  getPagesJson,
  getComponentSet,
  getWXComponents,
  getGlobalUsingComponents,
  updateAppJson,
  updateJsonFile,
  updatePageJson,
  updateProjectJson,
  updateComponentJson,
  updateSpecialMethods,
  updateUsingComponents,
  updateUsingGlobalComponents,
  updateAppJsonUsingComponents,
  updateUsingAutoImportComponents,
  updateComponentGenerics,
  updateGenericComponents,
  getChangedJsonFileMap,
  getSpecialMethods,
  supportGlobalUsingComponents
}