mh-two-thousand-and-two
2024-04-12 7fc6dbf547b8899d949b67cdec36b96a7d1701c7
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
const fs = require('fs')
const path = require('path')
const merge = require('merge')
 
const {
  parsePages,
  normalizePath,
  getPlatformProject,
  isSupportSubPackages
} = require('@dcloudio/uni-cli-shared')
 
const {
  updateAppJsonUsingComponents
} = require('@dcloudio/uni-cli-shared/lib/cache')
 
const {
  darkmode,
  hasTheme
} = require('@dcloudio/uni-cli-shared/lib/theme')
 
const {
  hasOwn,
  parseStyle,
  trimMPJson,
  NON_APP_JSON_KEYS
} = require('../util')
 
const uniI18n = require('@dcloudio/uni-cli-i18n')
 
function defaultCopy (name, value, json) {
  json[name] = value
}
 
function isPlainObject (a) {
  if (a === null) {
    return false
  }
  return typeof a === 'object'
}
 
function deepCopy (name, value, json) {
  if (isPlainObject(value) && isPlainObject(json[name])) {
    json[name] = merge.recursive(true, json[name], value)
  } else {
    defaultCopy(name, value, json)
  }
}
 
const pagesJson2AppJson = {
  globalStyle: function (name, value, json) {
    json.window = parseStyle(value)
    if (json.window.usingComponents || json.window.usingSwanComponents) {
      // 暂定 usingComponents 优先级高于 usingSwanComponents
      json.usingComponents = Object.assign({}, json.window.usingSwanComponents, json.window.usingComponents)
      delete json.window.usingComponents
      delete json.window.usingSwanComponents
    } else {
      json.usingComponents = {}
    }
  },
  tabBar: function (name, value, json, fromJson) {
    if (value && value.list && value.list.length) {
      if (value.list.length < 2) {
        console.error(
          uniI18n.__('pagesLoader.pagesTabbarMinItem2', {
            0: 'tabBar.list'
          })
        )
      }
      const pages = json.pages
      value.list.forEach((page, index) => {
        if (!pages.includes(page.pagePath)) {
          if (
            !(
              fromJson &&
              fromJson.nvue &&
              fromJson.nvue.pages &&
              fromJson.nvue.pages.find(
                ({
                  path
                }) => path === page.pagePath + '.html'
              )
            )
          ) {
            console.error(
              uniI18n.__('pagesLoader.needInPagesNode', {
                0: `pages.json tabBar['list'][${index}]['pagePath'] "${page.pagePath}"`
              })
            )
          }
        }
      })
    }
    json[name] = value
  },
  preloadRule: defaultCopy,
  workers: defaultCopy,
  plugins: defaultCopy,
  entryPagePath: defaultCopy
}
 
const manifestJson2AppJson = {
  networkTimeout: defaultCopy,
  debug: defaultCopy
}
 
function parseCondition (projectJson, pagesJson) {
  if (process.env.NODE_ENV === 'development') {
    // 仅开发期间 condition 生效
    // 启动Condition
    const condition = getCondition(pagesJson)
    if (condition) {
      if (!projectJson.condition) {
        projectJson.condition = {}
      }
      projectJson.condition.miniprogram = condition
    }
  }
}
 
const pagesJson2ProjectJson = {}
 
const manifestJson2ProjectJson = {
  name: function (name, value, json) {
    if (!value) {
      value = path.basename(process.env.UNI_INPUT_DIR)
      if (value === 'src') {
        value = path.basename(path.dirname(process.env.UNI_INPUT_DIR))
      }
    }
    json.projectname = value
  }
}
 
const platformJson2ProjectJson = {
  appid: defaultCopy,
  setting: deepCopy,
  miniprogramRoot: defaultCopy,
  cloudfunctionRoot: defaultCopy,
  qcloudRoot: defaultCopy,
  pluginRoot: defaultCopy,
  compileType: defaultCopy,
  libVersion: defaultCopy,
  projectname: defaultCopy,
  packOptions: deepCopy,
  debugOptions: deepCopy,
  scripts: deepCopy,
  cloudbaseRoot: defaultCopy
}
 
function copyToJson (json, fromJson, options) {
  Object.keys(options).forEach(name => {
    if (hasOwn(fromJson, name)) {
      options[name](name, fromJson[name], json, fromJson)
    }
  })
}
 
function getCondition (pagesJson) {
  const condition = pagesJson.condition
  const launchPagePath = process.env.UNI_CLI_LAUNCH_PAGE_PATH || ''
  const launchPageQuery = process.env.UNI_CLI_LAUNCH_PAGE_QUERY || ''
 
  const launchPageOptions = {
    id: 0,
    name: launchPagePath, // 模式名称
    pathName: launchPagePath, // 启动页面,必选
    query: launchPageQuery // 启动参数,在页面的onLoad函数里面得到。
  }
  if (condition) {
    let current = -1
    if (Array.isArray(condition.list) && condition.list.length) {
      condition.list.forEach(function (item, index) {
        item.id = item.id || index
        if (item.path) {
          item.pathName = item.path
          delete item.path
        }
        if (launchPagePath) {
          if (
            item.pathName === launchPagePath &&
            item.query === launchPageQuery
          ) {
            // 指定了入口页
            current = index
          }
        }
      })
      if (launchPagePath) {
        if (current !== -1) {
          // 已存在
          condition.current = current
        } else {
          // 不存在
          condition.list.push(
            Object.assign(launchPageOptions, {
              id: condition.list.length
            })
          )
          condition.current = condition.list.length - 1
        }
      }
      return condition
    }
  }
  if (launchPagePath) {
    pagesJson.condition = {
      current: 0,
      list: [launchPageOptions]
    }
    return pagesJson.condition
  }
  return false
}
 
function weixinSkyline (config) {
  return config.renderer === 'skyline' && config.lazyCodeLoading === 'requiredComponents'
}
 
function openES62ES5 (config) {
  if (!config.setting) {
    config.setting = {}
  }
  if (!config.setting.es6) {
    config.setting.es6 = true
  }
}
 
module.exports = function (pagesJson, manifestJson, project = {}) {
  const app = {
    pages: [],
    subPackages: []
  }
 
  const subPackages = {}
 
  parsePages(
    pagesJson,
    function (page) {
      app.pages.push(page.path)
    },
    function (root, page, subPackage) {
      if (!isSupportSubPackages()) {
        // 不支持分包
        app.pages.push(normalizePath(path.join(root, page.path)))
      } else {
        if (!subPackages[root]) {
          subPackages[root] = {
            root,
            pages: []
          }
          Object.keys(subPackage).forEach(name => {
            if (['root', 'pages'].indexOf(name) === -1) {
              subPackages[root][name] = subPackage[name]
            }
          })
        }
        subPackages[root].pages.push(page.path)
      }
    }
  )
 
  Object.keys(subPackages).forEach(root => {
    app.subPackages.push(subPackages[root])
  })
 
  copyToJson(app, pagesJson, pagesJson2AppJson)
 
  copyToJson(app, manifestJson, manifestJson2AppJson)
 
  if (app.usingComponents) {
    updateAppJsonUsingComponents(app.usingComponents)
  }
 
  const themeLocation = (manifestJson[process.env.UNI_PLATFORM] || {}).themeLocation
  if (darkmode() && hasTheme(themeLocation)) {
    app.themeLocation = themeLocation || 'theme.json'
  }
 
  const projectName = getPlatformProject()
 
  const projectPath =
    projectName &&
    path.resolve(process.env.VUE_CLI_CONTEXT || process.cwd(), projectName)
 
  if (projectPath && fs.existsSync(projectPath)) {
    // 自定义 project.config.json
    const platform = process.env.UNI_PLATFORM
 
    // app-plus时不需要处理平台配置到 app 中
    if (platform !== 'app-plus' && hasOwn(manifestJson, platform)) {
      const platformJson = manifestJson[platform] || {}
 
      const projectKeys = Object.keys(platformJson2ProjectJson)
 
      Object.keys(platformJson).forEach(key => {
        if (
          !projectKeys.includes(key) && !NON_APP_JSON_KEYS.includes(key)
        ) {
          // usingComponents 是编译模式开关,需要过滤,不能拷贝到 app
          app[key] = platformJson[key]
        }
      })
    }
 
    if (
      platform === 'mp-weixin' ||
      platform === 'mp-qq'
    ) {
      // 微信不需要生成,其他平台做拷贝
      return {
        app: {
          name: 'app',
          content: trimMPJson(app)
        }
      }
    }
    return {
      app: {
        name: 'app',
        content: trimMPJson(app)
      },
      project: {
        name: 'project.config',
        content: require(projectPath)
      }
    }
  } else {
    parseCondition(project, pagesJson)
 
    copyToJson(project, pagesJson, pagesJson2ProjectJson)
 
    copyToJson(project, manifestJson, manifestJson2ProjectJson)
 
    const platform = process.env.UNI_PLATFORM
 
    // app-plus时不需要处理平台配置到 app 中
    if (platform !== 'app-plus' && hasOwn(manifestJson, platform)) {
      const platformJson = manifestJson[platform] || {}
 
      copyToJson(project, platformJson, platformJson2ProjectJson)
 
      const projectKeys = Object.keys(platformJson2ProjectJson)
 
      Object.keys(platformJson).forEach(key => {
        if (
          !projectKeys.includes(key) && !NON_APP_JSON_KEYS.includes(key)
        ) {
          // usingComponents 是编译模式开关,需要过滤,不能拷贝到 app
          app[key] = platformJson[key]
        }
      })
    }
 
    // 引用了原生小程序组件,自动开启 ES6=>ES5
    const wxcomponentsPath = path.resolve(
      process.env.UNI_INPUT_DIR,
      './wxcomponents'
    )
    if (fs.existsSync(wxcomponentsPath)) {
      const wxcomponentsFiles = fs.readdirSync(wxcomponentsPath)
      if (wxcomponentsFiles.length) {
        if (!project.setting) {
          project.setting = {}
        }
        project.setting.es6 = true
      }
    }
 
    // 使用了微信小程序手势系统,自动开启 ES6=>ES5
    platform === 'mp-weixin' && weixinSkyline(manifestJson[platform]) && openES62ES5(project)
 
    if (process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
      if (!project.setting) {
        project.setting = {}
      }
      // automator时,强制不检测域名
      project.setting.urlCheck = false
    }
 
    if (!project.appid) {
      project.appid = 'touristappid'
    }
 
    return {
      app: {
        name: 'app',
        content: trimMPJson(app)
      },
      project: {
        name: 'project.config',
        content: project
      }
    }
  }
}