env.js 16.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
const fs = require('fs')
fxy060608's avatar
fxy060608 已提交
2
const path = require('path')
fxy060608's avatar
fxy060608 已提交
3
const mkdirp = require('mkdirp')
4
const loaderUtils = require('loader-utils')
d-u-a's avatar
d-u-a 已提交
5
const uniI18n = require('@dcloudio/uni-cli-i18n')
fxy060608's avatar
fxy060608 已提交
6
const moduleAlias = require('module-alias')
fxy060608's avatar
fxy060608 已提交
7 8

require('./error-reporting')
fxy060608's avatar
fxy060608 已提交
9

10 11 12 13 14 15
const hasOwnProperty = Object.prototype.hasOwnProperty

function hasOwn (obj, key) {
  return hasOwnProperty.call(obj, key)
}

fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21
const defaultInputDir = 'src'
if (process.env.UNI_INPUT_DIR && process.env.UNI_INPUT_DIR.indexOf('./') === 0) {
  process.env.UNI_INPUT_DIR = path.resolve(process.cwd(), process.env.UNI_INPUT_DIR)
}
process.env.UNI_INPUT_DIR = process.env.UNI_INPUT_DIR || path.resolve(process.cwd(), defaultInputDir)

fxy060608's avatar
fxy060608 已提交
22
const manifestJsonObj = require('@dcloudio/uni-cli-shared/lib/manifest').getManifestJson()
fxy060608's avatar
fxy060608 已提交
23

24
process.env.UNI_APP_ID = manifestJsonObj.appid || ''
Q
qiang 已提交
25
process.env.UNI_APP_NAME = manifestJsonObj.name || ''
26
process.env.UNI_PLATFORM = process.env.UNI_PLATFORM || 'h5'
27

fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33
// 小程序 vue3 标记
if (process.env.UNI_PLATFORM.indexOf('mp-') === 0) {
  if (manifestJsonObj.vueVersion === '3' || manifestJsonObj.vueVersion === 3) {
    process.env.UNI_USING_VUE3 = true
    process.env.UNI_USING_VUE3_OPTIONS_API = true
  }
fxy060608's avatar
fxy060608 已提交
34
}
fxy060608's avatar
fxy060608 已提交
35 36 37

// 初始化全局插件对象
global.uniPlugin = require('@dcloudio/uni-cli-shared/lib/plugin').init()
fxy060608's avatar
fxy060608 已提交
38

fxy060608's avatar
fxy060608 已提交
39 40 41 42 43 44
const platformOptions = manifestJsonObj[process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM] || {}
// 插件校验环境
global.uniPlugin.validate.forEach(validate => {
  validate(platformOptions, manifestJsonObj)
})
process.UNI_MANIFEST = manifestJsonObj
fxy060608's avatar
fxy060608 已提交
45

fxy060608's avatar
fxy060608 已提交
46 47
process.env.VUE_APP_NAME = manifestJsonObj.name

fxy060608's avatar
fxy060608 已提交
48
process.env.UNI_USING_V3_SCOPED = true
fxy060608's avatar
fxy060608 已提交
49

50
process.env.UNI_CLOUD_PROVIDER = JSON.stringify([])
fxy060608's avatar
fxy060608 已提交
51

52 53 54
const isH5 = !process.env.UNI_SUB_PLATFORM && process.env.UNI_PLATFORM === 'h5'
const isProduction = process.env.NODE_ENV === 'production'

fxy060608's avatar
fxy060608 已提交
55 56 57
if (process.env.UNI_CLOUD_SPACES) {
  try {
    const spaces = JSON.parse(process.env.UNI_CLOUD_SPACES)
fxy060608's avatar
fxy060608 已提交
58
    if (Array.isArray(spaces)) {
59
      const hasUniCloudSpace = spaces.length > 0
fxy060608's avatar
fxy060608 已提交
60
      if (spaces.length === 1) {
fxy060608's avatar
fxy060608 已提交
61
        const space = spaces[0]
fxy060608's avatar
fxy060608 已提交
62 63 64
        console.log(uniI18n.__('pluginUni.currentProjectDefaultSpaceId', {
          0: space.id
        }))
fxy060608's avatar
fxy060608 已提交
65
      }
66 67 68 69 70 71

      if (
        hasUniCloudSpace &&
        isH5 &&
        isProduction
      ) {
fxy060608's avatar
fxy060608 已提交
72 73 74
        console.warn(uniI18n.__('pluginUni.unicloudReleaseH5', {
          0: 'https://uniapp.dcloud.io/uniCloud/quickstart?id=useinh5'
        }))
75 76 77 78 79
      } else if (
        hasUniCloudSpace &&
        isH5 &&
        !isProduction
      ) {
fxy060608's avatar
fxy060608 已提交
80 81 82
        console.warn(uniI18n.__('pluginUni.unicloudShowedRunByHBuilderX', {
          0: 'https://uniapp.dcloud.io/uniCloud/quickstart?id=useinh5'
        }))
83 84
      }

fxy060608's avatar
fxy060608 已提交
85 86 87
      process.env.UNI_CLOUD_PROVIDER = JSON.stringify(spaces.map(space => {
        if (space.clientSecret) {
          return {
88
            provider: space.provider || 'aliyun',
fxy060608's avatar
fxy060608 已提交
89 90 91 92 93 94 95
            spaceName: space.name,
            spaceId: space.id,
            clientSecret: space.clientSecret,
            endpoint: space.apiEndpoint
          }
        } else {
          return {
96
            provider: space.provider || 'tencent',
fxy060608's avatar
fxy060608 已提交
97 98 99 100
            spaceName: space.name,
            spaceId: space.id
          }
        }
101
      }))
fxy060608's avatar
fxy060608 已提交
102 103
    }
  } catch (e) {}
fxy060608's avatar
fxy060608 已提交
104
}
fxy060608's avatar
fxy060608 已提交
105

fxy060608's avatar
fxy060608 已提交
106 107 108
// 初始化环境变量
const defaultOutputDir = '../../../../dist/' +
  (process.env.NODE_ENV === 'production' ? 'build' : 'dev') + '/' +
fxy060608's avatar
fxy060608 已提交
109
  (process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM)
110

111
process.env.UNI_OUTPUT_DEFAULT_DIR = path.resolve(__dirname, defaultOutputDir)
fxy060608's avatar
fxy060608 已提交
112 113 114 115 116 117
if (process.env.UNI_OUTPUT_DIR && process.env.UNI_OUTPUT_DIR.indexOf('./') === 0) {
  process.env.UNI_OUTPUT_DIR = path.resolve(process.cwd(), process.env.UNI_OUTPUT_DIR)
}

process.env.UNI_PLATFORM = process.env.UNI_PLATFORM || 'h5'
process.env.VUE_APP_PLATFORM = process.env.UNI_PLATFORM
118
process.env.UNI_OUTPUT_DIR = process.env.UNI_OUTPUT_DIR || process.env.UNI_OUTPUT_DEFAULT_DIR
fxy060608's avatar
fxy060608 已提交
119 120 121 122 123 124 125 126 127

if (process.env.UNI_PLATFORM === 'app-plus') {
  process.env.UNI_OUTPUT_TMP_DIR = path.resolve(process.env.UNI_OUTPUT_DIR, '../.tmp/app-plus')
}

process.env.UNI_CLI_CONTEXT = path.resolve(__dirname, '../../../../')

process.UNI_LIBRARIES = process.UNI_LIBRARIES || ['@dcloudio/uni-ui']

fxy060608's avatar
fxy060608 已提交
128
if (process.env.NODE_ENV === 'production') { // 发行模式,不启用 cache
129
  delete process.env.UNI_USING_CACHE
fxy060608's avatar
fxy060608 已提交
130 131
}

fxy060608's avatar
fxy060608 已提交
132 133 134 135 136 137
process.env.BROWSERSLIST_CONFIG = [
  path.resolve(process.env.UNI_INPUT_DIR, '.browserslistrc'),
  path.resolve(process.env.UNI_CLI_CONTEXT, 'package.json'),
  path.resolve(process.cwd(), 'package.json')
].find(file => fs.existsSync(file))

fxy060608's avatar
fxy060608 已提交
138
const {
fxy060608's avatar
fxy060608 已提交
139
  normalizePath,
fxy060608's avatar
fxy060608 已提交
140 141
  isSupportSubPackages,
  runByHBuilderX,
fxy060608's avatar
fxy060608 已提交
142
  getPagesJson
143 144 145
} = require('@dcloudio/uni-cli-shared')

process.env.RUN_BY_HBUILDERX = JSON.stringify(runByHBuilderX)
146 147 148 149 150 151

const {
  initUniModules
} = require('@dcloudio/uni-cli-shared/lib/uni_modules')

initUniModules()
fxy060608's avatar
fxy060608 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

const pagesJsonObj = getPagesJson()
// 读取分包
process.UNI_SUBPACKAGES = {}
if (Array.isArray(pagesJsonObj.subPackages)) {
  pagesJsonObj.subPackages.forEach(subPackage => {
    if (subPackage && subPackage.root) {
      const {
        name,
        root,
        independent
      } = subPackage
      process.UNI_SUBPACKAGES[root] = {
        name,
        root,
        independent
      }
    }
  })
}

fxy060608's avatar
fxy060608 已提交
173 174
process.UNI_PAGES = pagesJsonObj

fxy060608's avatar
fxy060608 已提交
175 176 177 178 179 180 181 182
if (manifestJsonObj.debug) {
  process.env.VUE_APP_DEBUG = true
}

process.UNI_STAT_CONFIG = {
  appid: manifestJsonObj.appid
}

183 184
// 默认启用 自定义组件模式
// if (isInHBuilderXAlpha) {
185
let usingComponentsAbsent = false
186
if (!hasOwn(platformOptions, 'usingComponents')) {
187
  usingComponentsAbsent = true
188
}
fxy060608's avatar
fxy060608 已提交
189
platformOptions.usingComponents = true
190
// }
fxy060608's avatar
fxy060608 已提交
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

if (process.env.UNI_PLATFORM === 'h5') {
  const optimization = platformOptions.optimization
  if (optimization) {
    // 发行模式且主动启用优化
    const treeShaking = optimization.treeShaking
    if (
      treeShaking &&
      treeShaking.enable &&
      process.env.NODE_ENV === 'production'
    ) {
      process.env.UNI_OPT_TREESHAKINGNG = true
      process.UNI_USER_APIS = new Set()
      if (Array.isArray(treeShaking.modules) && treeShaking.modules.length) {
        const {
          parseUserApis
        } = require('@dcloudio/uni-cli-shared/lib/api')
        try {
          const modules = require('@dcloudio/uni-h5/lib/modules.json')
          process.UNI_USER_APIS = parseUserApis(treeShaking.modules || [], modules)
        } catch (e) {}
      }
    }
    if (optimization.prefetch) {
      process.env.UNI_OPT_PREFETCH = true
    }
    if (optimization.preload) {
      process.env.UNI_OPT_PRELOAD = true
    }
  }
221 222
  const buffer = fs.readFileSync(require.resolve('@dcloudio/uni-h5/dist/index.css'))
  process.env.VUE_APP_INDEX_CSS_HASH = loaderUtils.getHashDigest(buffer, 'md5', 'hex', 8)
fxy060608's avatar
fxy060608 已提交
223 224 225 226 227 228
}

if (process.env.UNI_PLATFORM === 'mp-qq') { // QQ小程序 强制自定义组件模式
  platformOptions.usingComponents = true
}

229
let isNVueCompiler = true
fxy060608's avatar
fxy060608 已提交
230
if (process.env.UNI_PLATFORM === 'app-plus') {
231 232
  if (platformOptions.nvueCompiler === 'weex') {
    isNVueCompiler = false
fxy060608's avatar
fxy060608 已提交
233
  }
fxy060608's avatar
fxy060608 已提交
234 235 236 237 238 239 240

  delete process.env.UNI_USING_CACHE
  if (platformOptions.renderer === 'native') {
    process.env.UNI_USING_V3_NATIVE = true
  } else {
    process.env.UNI_USING_V3 = true
    platformOptions.usingComponents = true
fxy060608's avatar
fxy060608 已提交
241
  }
fxy060608's avatar
fxy060608 已提交
242 243 244
  process.env.UNI_OUTPUT_TMP_DIR = ''
  // isNVueCompiler = true // v3 目前仅支持 uni-app 模式

fxy060608's avatar
fxy060608 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258
  // v3 支持指定 js 混淆(仅发行模式)
  if (
    process.env.NODE_ENV === 'production' &&
    process.env.UNI_USING_V3
  ) {
    const resources = platformOptions.confusion &&
      platformOptions.confusion.resources
    const resourcesKeys = resources &&
      Object.keys(resources).filter(filepath => path.extname(filepath) === '.js')
    if (resourcesKeys && resourcesKeys.length) {
      process.UNI_CONFUSION = resourcesKeys.map(filepath =>
        normalizePath(path.resolve(process.env.UNI_INPUT_DIR, filepath))
      )
    }
fxy060608's avatar
fxy060608 已提交
259
  }
fxy060608's avatar
fxy060608 已提交
260 261 262
} else { // 其他平台,待确认配置方案
  if (
    manifestJsonObj['app-plus'] &&
fxy060608's avatar
fxy060608 已提交
263
    manifestJsonObj['app-plus'].nvueCompiler === 'weex'
fxy060608's avatar
fxy060608 已提交
264
  ) {
265
    isNVueCompiler = false
fxy060608's avatar
fxy060608 已提交
266 267 268 269 270 271 272
  }
}

if (isNVueCompiler) {
  process.env.UNI_USING_NVUE_COMPILER = true
}

273
if (platformOptions.nvueStyleCompiler === 'uni-app') {
274
  process.env.UNI_USING_NVUE_STYLE_COMPILER = true
275 276
} else {
  platformOptions.nvueStyleCompiler = 'weex'
277 278
}

fxy060608's avatar
fxy060608 已提交
279 280 281 282 283 284 285 286 287
if (platformOptions.usingComponents === true) {
  if (process.env.UNI_PLATFORM !== 'h5') {
    process.env.UNI_USING_COMPONENTS = true
  }
  if (process.env.UNI_PLATFORM === 'app-plus') {
    process.env.UNI_USING_V8 = true
  }
}

288 289
// 兼容历史配置 betterScopedSlots
const modes = ['legacy', 'auto', 'augmented']
fxy060608's avatar
fxy060608 已提交
290 291
const scopedSlotsCompiler = !platformOptions.scopedSlotsCompiler && platformOptions.betterScopedSlots ? modes[2]
  : platformOptions.scopedSlotsCompiler
292
process.env.SCOPED_SLOTS_COMPILER = modes.includes(scopedSlotsCompiler) ? scopedSlotsCompiler : modes[1]
293 294 295 296
// 快手小程序抽象组件编译报错,如未指定 legacy 固定为 augmented 模式
if (process.env.UNI_PLATFORM === 'mp-kuaishou' && process.env.SCOPED_SLOTS_COMPILER !== modes[0]) {
  process.env.SCOPED_SLOTS_COMPILER = modes[2]
}
297

fxy060608's avatar
fxy060608 已提交
298
if (
299 300
  process.env.UNI_USING_COMPONENTS ||
  process.env.UNI_PLATFORM === 'h5'
fxy060608's avatar
fxy060608 已提交
301
) { // 自定义组件模式或 h5 平台
302 303 304 305 306
  const uniStatistics = Object.assign(
    manifestJsonObj.uniStatistics || {},
    platformOptions.uniStatistics || {}
  )

fxy060608's avatar
fxy060608 已提交
307
  if (uniStatistics.enable === true) {
308 309
    process.env.UNI_USING_STAT = true
    if (!process.UNI_STAT_CONFIG.appid && process.env.NODE_ENV === 'production') {
310
      console.log()
fxy060608's avatar
fxy060608 已提交
311 312 313
      console.warn(uniI18n.__('pluginUni.uniStatisticsNoAppid', {
        0: 'https://ask.dcloud.net.cn/article/36303'
      }))
314 315 316 317 318
      console.log()
    }
  }
}

fxy060608's avatar
fxy060608 已提交
319 320 321 322 323 324 325 326 327
if (process.env.UNI_USING_COMPONENTS) { // 是否启用分包优化
  if (platformOptions.optimization) {
    if (
      isSupportSubPackages() &&
      platformOptions.optimization.subPackages &&
      Object.keys(process.UNI_SUBPACKAGES).length
    ) {
      process.env.UNI_OPT_SUBPACKAGES = true
    }
fxy060608's avatar
fxy060608 已提交
328 329
  }
}
330

331 332
const warningMsg =
  usingComponentsAbsent
fxy060608's avatar
fxy060608 已提交
333 334
    ? '该应用之前可能是非自定义组件模式,目前以自定义组件模式运行。非自定义组件已于2019年11月1日起停止支持。详见:https://ask.dcloud.net.cn/article/36385'
    : 'uni-app已于2019年11月1日起停止支持非自定义组件模式 [详情](https://ask.dcloud.net.cn/article/36385)'
335 336

const needWarning = !platformOptions.usingComponents || usingComponentsAbsent
fxy060608's avatar
fxy060608 已提交
337
let hasNVue = false
fxy060608's avatar
fxy060608 已提交
338
// 输出编译器版本等信息
Q
qiang 已提交
339
const compileModeUrl = 'https://ask.dcloud.net.cn/article/36074'
fxy060608's avatar
fxy060608 已提交
340
if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
Q
qiang 已提交
341
  const compileMode = (process.env.UNI_USING_V3_NATIVE ? '(v3)' : '') + '' + (isNVueCompiler ? 'uni-app' : 'weex')
fxy060608's avatar
fxy060608 已提交
342 343 344 345
  console.log(uniI18n.__('pluginUni.nvueCompileModeForDetail', {
    0: compileMode,
    1: compileModeUrl
  }))
fxy060608's avatar
fxy060608 已提交
346
} else if (process.env.UNI_PLATFORM !== 'h5' && process.env.UNI_PLATFORM !== 'quickapp-native') {
fxy060608's avatar
fxy060608 已提交
347 348 349 350 351
  try {
    let info = ''
    if (process.env.UNI_PLATFORM === 'app-plus') {
      const pagesPkg = require('@dcloudio/webpack-uni-pages-loader/package.json')
      if (pagesPkg) {
d-u-a's avatar
d-u-a 已提交
352
        const v3Tips = `(v3)${uniI18n.__('see')}:https://ask.dcloud.net.cn/article/36599。`
fxy060608's avatar
fxy060608 已提交
353 354
        info = uniI18n.__('compilerVersion') + '' + pagesPkg['uni-app'].compilerVersion + (process.env.UNI_USING_V3
          ? v3Tips : '')
fxy060608's avatar
fxy060608 已提交
355
      }
fxy060608's avatar
fxy060608 已提交
356 357 358
      if (process.env.UNI_USING_V3) {
        console.log(info)
      } else {
fxy060608's avatar
fxy060608 已提交
359 360 361 362 363 364 365 366 367
        const glob = require('glob')
        hasNVue = !!glob.sync('pages/**/*.nvue', {
          cwd: process.env.UNI_INPUT_DIR
        }).length
        if (hasNVue) {
          console.log(info)
          if (needWarning) {
            console.log(warningMsg)
          }
fxy060608's avatar
fxy060608 已提交
368 369 370 371
          console.log(uniI18n.__('pluginUni.nvueCompileModeForDetail', {
            0: (isNVueCompiler ? 'uni-app' : 'weex'),
            1: compileModeUrl
          }))
fxy060608's avatar
fxy060608 已提交
372
        } else {
373
          console.log(info)
fxy060608's avatar
fxy060608 已提交
374 375 376
          if (needWarning) {
            console.log(warningMsg)
          }
377
        }
fxy060608's avatar
fxy060608 已提交
378 379
      }
    } else {
380
      if (needWarning) {
381
        console.log(warningMsg)
382
      }
fxy060608's avatar
fxy060608 已提交
383 384 385
    }
  } catch (e) {}
}
fxy060608's avatar
fxy060608 已提交
386
if (process.env.NODE_ENV !== 'production') { // 运行模式性能提示
d-u-a's avatar
d-u-a 已提交
387
  let perfMsg = uniI18n.__('pluginUni.runDebugMode')
fxy060608's avatar
fxy060608 已提交
388
  if (hasNVue) { // app-nvue
d-u-a's avatar
d-u-a 已提交
389
    perfMsg = perfMsg + uniI18n.__('pluginUni.runDebugModeNvue')
fxy060608's avatar
fxy060608 已提交
390
  } else if (process.env.UNI_PLATFORM.indexOf('mp-') === 0) { // 小程序
d-u-a's avatar
d-u-a 已提交
391
    perfMsg = perfMsg + uniI18n.__('pluginUni.runDebugModeMP')
fxy060608's avatar
fxy060608 已提交
392 393 394
  }
  console.log(perfMsg)
}
fxy060608's avatar
fxy060608 已提交
395

396
// 将 template-compiler 指向修订后的版本
fxy060608's avatar
fxy060608 已提交
397
moduleAlias.addAlias('vue-template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/vue-template-compiler')
fxy060608's avatar
fxy060608 已提交
398
moduleAlias.addAlias('@megalo/template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/@megalo/template-compiler')
399
moduleAlias.addAlias('mpvue-template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/mpvue-template-compiler')
400 401
// vue-loader
moduleAlias.addAlias('vue-loader', '@dcloudio/vue-cli-plugin-uni/packages/vue-loader')
fxy060608's avatar
fxy060608 已提交
402

fxy060608's avatar
fxy060608 已提交
403 404
if (process.env.UNI_USING_V3 && process.env.UNI_PLATFORM === 'app-plus') {
  moduleAlias.addAlias('./runtime/getUrl.js', '@dcloudio/vue-cli-plugin-uni/lib/app-plus/getUrl.js')
fxy060608's avatar
fxy060608 已提交
405
  moduleAlias.addAlias('../runtime/getUrl.js', '@dcloudio/vue-cli-plugin-uni/lib/app-plus/getUrl.js')
fxy060608's avatar
fxy060608 已提交
406
  moduleAlias.addAlias('vue-style-loader', '@dcloudio/vue-cli-plugin-uni/packages/app-vue-style-loader')
407
}
fxy060608's avatar
fxy060608 已提交
408

Q
qiang 已提交
409 410 411
if (process.env.UNI_PLATFORM === 'h5') {
  moduleAlias.addAlias('vue-style-loader', '@dcloudio/vue-cli-plugin-uni/packages/h5-vue-style-loader')
}
fxy060608's avatar
fxy060608 已提交
412

fxy060608's avatar
fxy060608 已提交
413
if (process.env.UNI_PLATFORM === 'mp-toutiao') {
414 415 416 417 418 419 420
  // !important 始终带有一个空格
  moduleAlias.addAlias(
    'postcss-normalize-whitespace',
    '@dcloudio/vue-cli-plugin-uni/packages/postcss-normalize-whitespace'
  )
}

fxy060608's avatar
fxy060608 已提交
421 422 423 424 425 426 427
if (runByHBuilderX) {
  const oldError = console.error
  console.error = function (msg) {
    if (typeof msg === 'string' && msg.includes(
      '[BABEL] Note: The code generator has deoptimised the styling of')) {
      const filePath = msg.replace('[BABEL] Note: The code generator has deoptimised the styling of ', '').split(
        ' as ')[0]
d-u-a's avatar
d-u-a 已提交
428
      console.log('[' + uniI18n.__('warning') + '] `' + path.relative(process.env.UNI_INPUT_DIR, filePath) +
fxy060608's avatar
fxy060608 已提交
429 430 431 432 433 434 435
        '` 文件体积超过 500KB,已跳过压缩以及 ES6 转 ES5 的处理,手机端使用过大的js库影响性能。')
    } else {
      oldError.apply(console, arguments)
      // TODO 如果是首次运行遇到错误直接退出
    }
  }
}
436

fxy060608's avatar
fxy060608 已提交
437 438
if (
  process.env.UNI_USING_CACHE &&
fxy060608's avatar
fxy060608 已提交
439
  process.env.UNI_PLATFORM !== 'h5' &&
fxy060608's avatar
fxy060608 已提交
440
  !process.env.UNI_USING_V3 &&
fxy060608's avatar
fxy060608 已提交
441 442
  !process.env.UNI_USING_NATIVE &&
  !process.env.UNI_USING_V3_NATIVE
fxy060608's avatar
fxy060608 已提交
443 444
) { // 使用 cache, 拷贝 cache 的 json
  const cacheJsonDir = path.resolve(
fxy060608's avatar
fxy060608 已提交
445
    process.env.UNI_CLI_CONTEXT,
fxy060608's avatar
fxy060608 已提交
446
    'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM
fxy060608's avatar
fxy060608 已提交
447 448 449 450
  )
  if (!fs.existsSync(cacheJsonDir)) { //  创建 cache 目录
    mkdirp(cacheJsonDir)
  } else {
fxy060608's avatar
fxy060608 已提交
451
    require('@dcloudio/uni-cli-shared/lib/cache').restore()
fxy060608's avatar
fxy060608 已提交
452 453 454
  }
}

455
const {
456
  initAutoImportComponents,
fxy060608's avatar
fxy060608 已提交
457
  initAutoImportScanComponents
458 459 460 461 462 463 464
} = require('@dcloudio/uni-cli-shared/lib/pages')

process.UNI_AUTO_SCAN_COMPONENTS = !(pagesJsonObj.easycom && pagesJsonObj.easycom.autoscan === false)
initAutoImportComponents(pagesJsonObj.easycom)
if (process.UNI_AUTO_SCAN_COMPONENTS) {
  initAutoImportScanComponents()
}
465

fxy060608's avatar
fxy060608 已提交
466 467 468 469
global.uniPlugin.configureEnv.forEach(configureEnv => {
  configureEnv()
})

fxy060608's avatar
fxy060608 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
if (
  process.env.UNI_PLATFORM === 'h5' ||
  (
    process.env.UNI_PLATFORM === 'app-plus' &&
    process.env.UNI_USING_V3
  )
) {
  const migrate = require('@dcloudio/uni-migration')
  const wxcomponentDirs = [path.resolve(process.env.UNI_INPUT_DIR, 'wxcomponents')]
  global.uniModules.forEach(module => {
    wxcomponentDirs.push(path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', module, 'wxcomponents'))
  })
  wxcomponentDirs.forEach(wxcomponentsDir => {
    if (fs.existsSync(wxcomponentsDir)) { // 转换 mp-weixin 小程序组件
      migrate(wxcomponentsDir, false, {
        silent: true // 不输出日志
      })
    }
  })
}

fxy060608's avatar
fxy060608 已提交
491
if (process.env.UNI_PLATFORM.startsWith('mp-')) {
fxy060608's avatar
fxy060608 已提交
492 493 494
  console.log(uniI18n.__('pluginUni.mpBrowserKernelDifference', {
    0: 'https://uniapp.dcloud.io/matter?id=mp'
  }))
fxy060608's avatar
fxy060608 已提交
495 496
}

d-u-a's avatar
d-u-a 已提交
497
runByHBuilderX && console.log(uniI18n.__('compiling'))
fxy060608's avatar
fxy060608 已提交
498 499 500

module.exports = {
  manifestPlatformOptions: platformOptions
501
}