env.js 21.0 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')
D
DCloud_LXH 已提交
9
require('../util/console')
fxy060608's avatar
fxy060608 已提交
10

11 12
const hasOwnProperty = Object.prototype.hasOwnProperty

Q
qiang 已提交
13
function hasOwn (obj, key) {
14 15 16
  return hasOwnProperty.call(obj, key)
}

fxy060608's avatar
fxy060608 已提交
17 18 19 20 21 22
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 已提交
23
const {
24
  getManifestJson,
fxy060608's avatar
fxy060608 已提交
25
  isEnableUniPushV1,
fxy060608's avatar
fxy060608 已提交
26
  isEnableUniPushV2,
雪洛's avatar
雪洛 已提交
27 28
  isUniPushOffline,
  isEnableSecureNetwork
fxy060608's avatar
fxy060608 已提交
29 30 31
} = require('@dcloudio/uni-cli-shared/lib/manifest')

const manifestJsonObj = getManifestJson()
fxy060608's avatar
fxy060608 已提交
32

33
process.env.UNI_APP_ID = manifestJsonObj.appid || ''
Q
qiang 已提交
34
process.env.UNI_APP_NAME = manifestJsonObj.name || ''
35
process.env.UNI_PLATFORM = process.env.UNI_PLATFORM || 'h5'
D
DCloud_LXH 已提交
36 37
process.env.UNI_APP_VERSION_NAME = manifestJsonObj.versionName
process.env.UNI_APP_VERSION_CODE = manifestJsonObj.versionCode
D
DCloud_LXH 已提交
38
process.env.VUE_APP_DARK_MODE = (manifestJsonObj[process.env.UNI_PLATFORM] || {}).darkmode
39

fxy060608's avatar
fxy060608 已提交
40 41 42 43 44 45
// 小程序 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 已提交
46
}
fxy060608's avatar
fxy060608 已提交
47 48 49 50 51 52
// v2 uni-push
if (isEnableUniPushV2(manifestJsonObj, process.env.UNI_PLATFORM)) {
  process.env.UNI_PUSH_V2 = true
  if (process.env.UNI_PLATFORM === 'app-plus' && isUniPushOffline(manifestJsonObj)) {
    process.env.UNI_PUSH_V2_OFFLINE = true
  }
fxy060608's avatar
fxy060608 已提交
53 54
} else if (isEnableUniPushV1(manifestJsonObj, process.env.UNI_PLATFORM)) {
  process.env.UNI_PUSH_V1 = true
fxy060608's avatar
fxy060608 已提交
55
}
fxy060608's avatar
fxy060608 已提交
56 57 58

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

fxy060608's avatar
fxy060608 已提交
60 61 62 63 64 65
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 已提交
66

fxy060608's avatar
fxy060608 已提交
67 68
process.env.VUE_APP_NAME = manifestJsonObj.name

fxy060608's avatar
fxy060608 已提交
69
process.env.UNI_USING_V3_SCOPED = true
fxy060608's avatar
fxy060608 已提交
70

71
// 导出到小程序插件
fxy060608's avatar
fxy060608 已提交
72 73
process.env.UNI_MP_PLUGIN_EXPORT = JSON.stringify(Object.keys(platformOptions.plugins || {}).map(pluginName =>
  platformOptions.plugins[pluginName].export))
74

75 76 77
const isH5 = !process.env.UNI_SUB_PLATFORM && process.env.UNI_PLATFORM === 'h5'
const isProduction = process.env.NODE_ENV === 'production'

78 79 80
// uniCloud
if (!process.env.UNI_CLOUD_PROVIDER && process.env.UNI_CLOUD_SPACES) {
  process.env.UNI_CLOUD_PROVIDER = JSON.stringify([])
fxy060608's avatar
fxy060608 已提交
81 82
  try {
    const spaces = JSON.parse(process.env.UNI_CLOUD_SPACES)
fxy060608's avatar
fxy060608 已提交
83
    if (Array.isArray(spaces)) {
84
      const hasUniCloudSpace = spaces.length > 0
fxy060608's avatar
fxy060608 已提交
85
      if (spaces.length === 1) {
fxy060608's avatar
fxy060608 已提交
86
        const space = spaces[0]
fxy060608's avatar
fxy060608 已提交
87 88 89
        console.log(uniI18n.__('pluginUni.currentProjectDefaultSpaceId', {
          0: space.id
        }))
fxy060608's avatar
fxy060608 已提交
90
      }
91 92 93 94 95 96

      if (
        hasUniCloudSpace &&
        isH5 &&
        isProduction
      ) {
fxy060608's avatar
fxy060608 已提交
97
        console.warn(uniI18n.__('pluginUni.unicloudReleaseH5', {
雪洛's avatar
雪洛 已提交
98
          0: 'https://uniapp.dcloud.net.cn/uniCloud/publish.html#useinh5'
fxy060608's avatar
fxy060608 已提交
99
        }))
100 101 102 103 104
      } else if (
        hasUniCloudSpace &&
        isH5 &&
        !isProduction
      ) {
fxy060608's avatar
fxy060608 已提交
105
        console.warn(uniI18n.__('pluginUni.unicloudShowedRunByHBuilderX', {
雪洛's avatar
雪洛 已提交
106
          0: 'https://uniapp.dcloud.net.cn/uniCloud/publish.html#useinh5'
fxy060608's avatar
fxy060608 已提交
107
        }))
108 109
      }

fxy060608's avatar
fxy060608 已提交
110
      process.env.UNI_CLOUD_PROVIDER = JSON.stringify(spaces.map(space => {
111 112 113
        if (space.provider === 'tcb') {
          space.provider = 'tencent'
        }
fxy060608's avatar
fxy060608 已提交
114 115
        if (space.clientSecret) {
          return {
116
            provider: space.provider || 'aliyun',
fxy060608's avatar
fxy060608 已提交
117 118 119 120 121 122 123
            spaceName: space.name,
            spaceId: space.id,
            clientSecret: space.clientSecret,
            endpoint: space.apiEndpoint
          }
        } else {
          return {
124
            provider: space.provider || 'tencent',
fxy060608's avatar
fxy060608 已提交
125 126 127 128
            spaceName: space.name,
            spaceId: space.id
          }
        }
129
      }))
fxy060608's avatar
fxy060608 已提交
130 131
    }
  } catch (e) {}
fxy060608's avatar
fxy060608 已提交
132
}
fxy060608's avatar
fxy060608 已提交
133

雪洛's avatar
雪洛 已提交
134
// 安全网络
135
process.env.UNI_SECURE_NETWORK_ENABLE = isEnableSecureNetwork(manifestJsonObj, process.env.UNI_PLATFORM)
雪洛's avatar
雪洛 已提交
136

fxy060608's avatar
fxy060608 已提交
137
// 初始化环境变量
Q
qiang 已提交
138 139 140
process.env.UNI_CLI_CONTEXT = require('@dcloudio/uni-cli-shared/lib/util').getCLIContext()

const defaultOutputDir = './dist/' +
fxy060608's avatar
fxy060608 已提交
141
  (process.env.NODE_ENV === 'production' ? 'build' : 'dev') + '/' +
fxy060608's avatar
fxy060608 已提交
142
  (process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM)
143

Q
qiang 已提交
144
process.env.UNI_OUTPUT_DEFAULT_DIR = path.resolve(process.env.UNI_CLI_CONTEXT, defaultOutputDir)
fxy060608's avatar
fxy060608 已提交
145 146 147 148 149 150
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
151
process.env.UNI_OUTPUT_DIR = process.env.UNI_OUTPUT_DIR || process.env.UNI_OUTPUT_DEFAULT_DIR
fxy060608's avatar
fxy060608 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
initUtsPlatform()

function initUtsPlatform () {
  if (process.env.UNI_APP_PLATFORM === 'android') {
    process.env.UNI_UTS_PLATFORM = 'app-android'
  }
  if (process.env.UNI_APP_PLATFORM === 'ios') {
    process.env.UNI_UTS_PLATFORM = 'app-ios'
  }
  if (process.env.UNI_PLATFORM === 'h5') {
    process.env.UNI_UTS_PLATFORM = 'web'
  } else {
    if (!process.env.UNI_UTS_PLATFORM) {
      process.env.UNI_UTS_PLATFORM = process.env.UNI_PLATFORM
    }
  }
}
fxy060608's avatar
fxy060608 已提交
169 170 171 172 173 174 175

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

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

fxy060608's avatar
fxy060608 已提交
176
if (process.env.NODE_ENV === 'production') { // 发行模式,不启用 cache
177
  delete process.env.UNI_USING_CACHE
fxy060608's avatar
fxy060608 已提交
178 179
}

fxy060608's avatar
fxy060608 已提交
180 181 182 183 184 185
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 已提交
186
const {
fxy060608's avatar
fxy060608 已提交
187
  normalizePath,
fxy060608's avatar
fxy060608 已提交
188 189
  isSupportSubPackages,
  runByHBuilderX,
fxy060608's avatar
fxy060608 已提交
190
  getPagesJson
191 192 193
} = require('@dcloudio/uni-cli-shared')

process.env.RUN_BY_HBUILDERX = JSON.stringify(runByHBuilderX)
194 195 196 197 198 199

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

initUniModules()
fxy060608's avatar
fxy060608 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

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 已提交
221 222
process.UNI_PAGES = pagesJsonObj

fxy060608's avatar
fxy060608 已提交
223 224 225 226 227 228 229 230
if (manifestJsonObj.debug) {
  process.env.VUE_APP_DEBUG = true
}

process.UNI_STAT_CONFIG = {
  appid: manifestJsonObj.appid
}

231 232
// 默认启用 自定义组件模式
// if (isInHBuilderXAlpha) {
233
let usingComponentsAbsent = false
234
if (!hasOwn(platformOptions, 'usingComponents')) {
235
  usingComponentsAbsent = true
236
}
fxy060608's avatar
fxy060608 已提交
237
platformOptions.usingComponents = true
238
// }
fxy060608's avatar
fxy060608 已提交
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

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
    }
  }
D
DCloud_LXH 已提交
269 270 271 272 273 274 275 276 277 278
  const indexCssBuffer = fs.readFileSync(require.resolve('@dcloudio/uni-h5/dist/index.css'))
  process.env.VUE_APP_INDEX_CSS_HASH = loaderUtils.getHashDigest(indexCssBuffer, 'md5', 'hex', 8)
  let indexDarkCssBuffer = ''
  try {
    indexDarkCssBuffer = fs.readFileSync(require.resolve('@dcloudio/uni-h5/dist/index.dark.css'))
    process.env.VUE_APP_INDEX_DARK_CSS_HASH = loaderUtils.getHashDigest(indexDarkCssBuffer, 'md5', 'hex', 8)
  } catch (error) {
    process.env.VUE_APP_INDEX_DARK_CSS_HASH = ''
    process.env.VUE_APP_DARK_MODE = false
  }
fxy060608's avatar
fxy060608 已提交
279 280 281 282 283 284
}

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

285
let isNVueCompiler = true
fxy060608's avatar
fxy060608 已提交
286
if (process.env.UNI_PLATFORM === 'app-plus') {
287 288
  if (platformOptions.nvueCompiler === 'weex') {
    isNVueCompiler = false
fxy060608's avatar
fxy060608 已提交
289
  }
fxy060608's avatar
fxy060608 已提交
290 291 292 293 294 295 296

  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 已提交
297
  }
fxy060608's avatar
fxy060608 已提交
298 299 300
  process.env.UNI_OUTPUT_TMP_DIR = ''
  // isNVueCompiler = true // v3 目前仅支持 uni-app 模式

fxy060608's avatar
fxy060608 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314
  // 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 已提交
315
  }
fxy060608's avatar
fxy060608 已提交
316 317 318
} else { // 其他平台,待确认配置方案
  if (
    manifestJsonObj['app-plus'] &&
fxy060608's avatar
fxy060608 已提交
319
    manifestJsonObj['app-plus'].nvueCompiler === 'weex'
fxy060608's avatar
fxy060608 已提交
320
  ) {
321
    isNVueCompiler = false
fxy060608's avatar
fxy060608 已提交
322 323 324 325 326 327 328
  }
}

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

329
if (platformOptions.nvueStyleCompiler === 'uni-app') {
330
  process.env.UNI_USING_NVUE_STYLE_COMPILER = true
331 332
} else {
  platformOptions.nvueStyleCompiler = 'weex'
333 334
}

fxy060608's avatar
fxy060608 已提交
335 336 337 338 339 340 341 342 343
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
  }
}

344 345
// 兼容历史配置 betterScopedSlots
const modes = ['legacy', 'auto', 'augmented']
Q
qiang 已提交
346 347
const scopedSlotsCompiler = !platformOptions.scopedSlotsCompiler && platformOptions.betterScopedSlots ? modes[2]
  : platformOptions.scopedSlotsCompiler
348
process.env.SCOPED_SLOTS_COMPILER = modes.includes(scopedSlotsCompiler) ? scopedSlotsCompiler : modes[1]
349
// 快手小程序、小红书小程序 抽象组件编译报错,如未指定 legacy 固定为 augmented 模式
M
mehaotian 已提交
350 351
if ((process.env.UNI_PLATFORM === 'mp-kuaishou' || process.env.UNI_PLATFORM === 'mp-xhs') && process.env
  .SCOPED_SLOTS_COMPILER !== modes[0]) {
352 353
  process.env.SCOPED_SLOTS_COMPILER = modes[2]
}
354

355 356
process.env.MERGE_VIRTUAL_HOST_ATTRIBUTES = (!!platformOptions.mergeVirtualHostAttributes).toString()

M
mehaotian 已提交
357 358 359
process.env.UNI_STATISTICS_CONFIG = '""'
process.env.UNI_STAT_UNI_CLOUD = '""'
process.env.UNI_STAT_DEBUG = '""'
fxy060608's avatar
fxy060608 已提交
360
if (
361 362
  process.env.UNI_USING_COMPONENTS ||
  process.env.UNI_PLATFORM === 'h5'
fxy060608's avatar
fxy060608 已提交
363
) { // 自定义组件模式或 h5 平台
364 365 366 367 368
  const uniStatistics = Object.assign(
    manifestJsonObj.uniStatistics || {},
    platformOptions.uniStatistics || {}
  )

fxy060608's avatar
fxy060608 已提交
369
  if (uniStatistics.enable === true) {
370 371 372 373 374 375 376
    const uniStatLog = (text) => {
      console.log()
      console.warn(text)
      console.log()
    }
    const version = Number(uniStatistics.version) === 2 ? '2' : '1'
    process.env.UNI_USING_STAT = version
377 378
    // 获取服务空间配置信息
    const uniCloudConfig = uniStatistics.uniCloud || {}
M
mehaotian 已提交
379
    process.env.UNI_STATISTICS_CONFIG = JSON.stringify(uniStatistics)
380 381 382
    process.env.UNI_STAT_UNI_CLOUD = JSON.stringify(uniCloudConfig)
    process.env.UNI_STAT_DEBUG = uniStatistics.debug === true ? 'true' : 'false'

M
mehaotian 已提交
383 384
    if (process.env.NODE_ENV === 'production') {
      if (!process.UNI_STAT_CONFIG.appid) {
385
        uniStatLog(uniI18n.__('pluginUni.uniStatisticsNoAppid', {
M
mehaotian 已提交
386 387 388 389
          0: 'https://ask.dcloud.net.cn/article/36303'
        }))
      } else {
        if (!uniStatistics.version) {
390
          uniStatLog(uniI18n.__('pluginUni.uniStatisticsNoVersion', {
M
mehaotian 已提交
391 392
            0: 'https://uniapp.dcloud.io/uni-stat-v2.html'
          }))
M
mehaotian 已提交
393
        } else {
394 395
          uniStatLog(`已开启 uni统计${uniStatistics.version}.0 版本`)
          if (version === '2') {
fxy060608's avatar
fxy060608 已提交
396 397 398
            uniStatLog(
              '【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097'
            )
399
          }
M
mehaotian 已提交
400 401
        }
      }
402
    } else {
403
      if (!uniStatistics.version) {
404
        uniStatLog(uniI18n.__('pluginUni.uniStatisticsNoVersion', {
M
mehaotian 已提交
405 406
          0: 'https://uniapp.dcloud.io/uni-stat-v2.html'
        }))
407
      } else {
408 409
        uniStatLog(`已开启 uni统计${uniStatistics.version}.0 版本`)
        if (version === '2') {
fxy060608's avatar
fxy060608 已提交
410 411 412
          uniStatLog(
            '【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097'
          )
413
        }
414
      }
415 416 417 418
    }
  }
}

fxy060608's avatar
fxy060608 已提交
419 420 421 422 423 424 425 426 427
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 已提交
428 429
  }
}
430

431
const warningMsg =
Q
qiang 已提交
432 433 434
  usingComponentsAbsent
    ? '该应用之前可能是非自定义组件模式,目前以自定义组件模式运行。非自定义组件已于2019年11月1日起停止支持。详见:https://ask.dcloud.net.cn/article/36385'
    : 'uni-app已于2019年11月1日起停止支持非自定义组件模式 [详情](https://ask.dcloud.net.cn/article/36385)'
435 436

const needWarning = !platformOptions.usingComponents || usingComponentsAbsent
fxy060608's avatar
fxy060608 已提交
437
let hasNVue = false
fxy060608's avatar
fxy060608 已提交
438
// 输出编译器版本等信息
D
DCloud_LXH 已提交
439 440 441
const pagesPkg = require('@dcloudio/webpack-uni-pages-loader/package.json')
process.env.UNI_COMPILER_VERSION = ''
if (pagesPkg) {
Q
qiang 已提交
442
  process.env.UNI_COMPILER_VERSION = pagesPkg['uni-app'].compilerVersion
D
DCloud_LXH 已提交
443
}
Q
qiang 已提交
444
const compileModeUrl = 'https://ask.dcloud.net.cn/article/36074'
fxy060608's avatar
fxy060608 已提交
445
if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
Q
qiang 已提交
446
  const compileMode = (process.env.UNI_USING_V3_NATIVE ? '(v3)' : '') + '' + (isNVueCompiler ? 'uni-app' : 'weex')
fxy060608's avatar
fxy060608 已提交
447 448 449 450
  console.log(uniI18n.__('pluginUni.nvueCompileModeForDetail', {
    0: compileMode,
    1: compileModeUrl
  }))
fxy060608's avatar
fxy060608 已提交
451
} else if (process.env.UNI_PLATFORM !== 'h5' && process.env.UNI_PLATFORM !== 'quickapp-native') {
fxy060608's avatar
fxy060608 已提交
452
  try {
Q
qiang 已提交
453
    const info = process.env.UNI_COMPILER_VERSION
fxy060608's avatar
fxy060608 已提交
454
    if (process.env.UNI_PLATFORM === 'app-plus') {
fxy060608's avatar
fxy060608 已提交
455 456 457
      if (process.env.UNI_USING_V3) {
        console.log(info)
      } else {
fxy060608's avatar
fxy060608 已提交
458 459 460 461 462 463 464 465 466
        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 已提交
467 468 469 470
          console.log(uniI18n.__('pluginUni.nvueCompileModeForDetail', {
            0: (isNVueCompiler ? 'uni-app' : 'weex'),
            1: compileModeUrl
          }))
fxy060608's avatar
fxy060608 已提交
471
        } else {
472
          console.log(info)
fxy060608's avatar
fxy060608 已提交
473 474 475
          if (needWarning) {
            console.log(warningMsg)
          }
476
        }
fxy060608's avatar
fxy060608 已提交
477 478
      }
    } else {
479
      if (needWarning) {
480
        console.log(warningMsg)
481
      }
fxy060608's avatar
fxy060608 已提交
482 483 484
    }
  } catch (e) {}
}
fxy060608's avatar
fxy060608 已提交
485
if (process.env.NODE_ENV !== 'production') { // 运行模式性能提示
d-u-a's avatar
d-u-a 已提交
486
  let perfMsg = uniI18n.__('pluginUni.runDebugMode')
fxy060608's avatar
fxy060608 已提交
487
  if (hasNVue) { // app-nvue
d-u-a's avatar
d-u-a 已提交
488
    perfMsg = perfMsg + uniI18n.__('pluginUni.runDebugModeNvue')
fxy060608's avatar
fxy060608 已提交
489
  } else if (process.env.UNI_PLATFORM.indexOf('mp-') === 0) { // 小程序
d-u-a's avatar
d-u-a 已提交
490
    perfMsg = perfMsg + uniI18n.__('pluginUni.runDebugModeMP')
fxy060608's avatar
fxy060608 已提交
491 492 493
  }
  console.log(perfMsg)
}
fxy060608's avatar
fxy060608 已提交
494

495
// 将 template-compiler 指向修订后的版本
fxy060608's avatar
fxy060608 已提交
496
moduleAlias.addAlias('vue-template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/vue-template-compiler')
fxy060608's avatar
fxy060608 已提交
497
moduleAlias.addAlias('@megalo/template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/@megalo/template-compiler')
498
moduleAlias.addAlias('mpvue-template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/mpvue-template-compiler')
499
// vue-loader
fxy060608's avatar
fxy060608 已提交
500
moduleAlias.addAlias('vue-loader', '@dcloudio/vue-cli-plugin-uni/packages/vue-loader')
fxy060608's avatar
fxy060608 已提交
501
// sass-loader
fxy060608's avatar
fxy060608 已提交
502
moduleAlias.addAlias('sass-loader', '@dcloudio/vue-cli-plugin-uni/packages/sass-loader')
fxy060608's avatar
fxy060608 已提交
503

fxy060608's avatar
fxy060608 已提交
504 505
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 已提交
506
  moduleAlias.addAlias('../runtime/getUrl.js', '@dcloudio/vue-cli-plugin-uni/lib/app-plus/getUrl.js')
fxy060608's avatar
fxy060608 已提交
507
  moduleAlias.addAlias('vue-style-loader', '@dcloudio/vue-cli-plugin-uni/packages/app-vue-style-loader')
508
}
fxy060608's avatar
fxy060608 已提交
509

Q
qiang 已提交
510 511 512
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 已提交
513

P
panyiming.325 已提交
514
if (process.env.UNI_PLATFORM === 'mp-toutiao' || process.env.UNI_PLATFORM === 'mp-lark') {
515 516 517 518 519 520 521
  // !important 始终带有一个空格
  moduleAlias.addAlias(
    'postcss-normalize-whitespace',
    '@dcloudio/vue-cli-plugin-uni/packages/postcss-normalize-whitespace'
  )
}

fxy060608's avatar
fxy060608 已提交
522 523
if (runByHBuilderX) {
  const oldError = console.error
Q
qiang 已提交
524
  console.error = function (msg) {
fxy060608's avatar
fxy060608 已提交
525
    if (typeof msg === 'string' && msg.includes(
Q
qiang 已提交
526
      '[BABEL] Note: The code generator has deoptimised the styling of')) {
fxy060608's avatar
fxy060608 已提交
527 528
      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 已提交
529
      console.log('[' + uniI18n.__('warning') + '] `' + path.relative(process.env.UNI_INPUT_DIR, filePath) +
fxy060608's avatar
fxy060608 已提交
530 531 532 533 534 535 536
        '` 文件体积超过 500KB,已跳过压缩以及 ES6 转 ES5 的处理,手机端使用过大的js库影响性能。')
    } else {
      oldError.apply(console, arguments)
      // TODO 如果是首次运行遇到错误直接退出
    }
  }
}
537

fxy060608's avatar
fxy060608 已提交
538 539
if (
  process.env.UNI_USING_CACHE &&
fxy060608's avatar
fxy060608 已提交
540
  process.env.UNI_PLATFORM !== 'h5' &&
fxy060608's avatar
fxy060608 已提交
541
  !process.env.UNI_USING_V3 &&
fxy060608's avatar
fxy060608 已提交
542 543
  !process.env.UNI_USING_NATIVE &&
  !process.env.UNI_USING_V3_NATIVE
fxy060608's avatar
fxy060608 已提交
544 545
) { // 使用 cache, 拷贝 cache 的 json
  const cacheJsonDir = path.resolve(
fxy060608's avatar
fxy060608 已提交
546
    process.env.UNI_CLI_CONTEXT,
fxy060608's avatar
fxy060608 已提交
547
    'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM
fxy060608's avatar
fxy060608 已提交
548 549 550 551
  )
  if (!fs.existsSync(cacheJsonDir)) { //  创建 cache 目录
    mkdirp(cacheJsonDir)
  } else {
fxy060608's avatar
fxy060608 已提交
552
    require('@dcloudio/uni-cli-shared/lib/cache').restore()
fxy060608's avatar
fxy060608 已提交
553 554 555
  }
}

556
const {
557
  initAutoImportComponents,
fxy060608's avatar
fxy060608 已提交
558
  initAutoImportScanComponents
559 560 561 562 563 564 565
} = 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()
}
566

fxy060608's avatar
fxy060608 已提交
567 568 569 570
global.uniPlugin.configureEnv.forEach(configureEnv => {
  configureEnv()
})

fxy060608's avatar
fxy060608 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
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 已提交
592
if (process.env.UNI_PLATFORM.startsWith('mp-')) {
fxy060608's avatar
fxy060608 已提交
593 594 595
  console.log(uniI18n.__('pluginUni.mpBrowserKernelDifference', {
    0: 'https://uniapp.dcloud.io/matter?id=mp'
  }))
fxy060608's avatar
fxy060608 已提交
596 597
}

d-u-a's avatar
d-u-a 已提交
598
runByHBuilderX && console.log(uniI18n.__('compiling'))
fxy060608's avatar
fxy060608 已提交
599 600 601

module.exports = {
  manifestPlatformOptions: platformOptions
602
}