env.js 18.7 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 24 25 26 27 28 29
const {
  getManifestJson,
  isEnableUniPushV2,
  isUniPushOffline
} = require('@dcloudio/uni-cli-shared/lib/manifest')

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

31
process.env.UNI_APP_ID = manifestJsonObj.appid || ''
Q
qiang 已提交
32
process.env.UNI_APP_NAME = manifestJsonObj.name || ''
33
process.env.UNI_PLATFORM = process.env.UNI_PLATFORM || 'h5'
D
DCloud_LXH 已提交
34 35
process.env.UNI_APP_VERSION_NAME = manifestJsonObj.versionName
process.env.UNI_APP_VERSION_CODE = manifestJsonObj.versionCode
36

fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42
// 小程序 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 已提交
43
}
fxy060608's avatar
fxy060608 已提交
44 45 46 47 48 49 50
// 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 已提交
51 52 53

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

fxy060608's avatar
fxy060608 已提交
55 56 57 58 59 60
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 已提交
61

fxy060608's avatar
fxy060608 已提交
62 63
process.env.VUE_APP_NAME = manifestJsonObj.name

fxy060608's avatar
fxy060608 已提交
64
process.env.UNI_USING_V3_SCOPED = true
fxy060608's avatar
fxy060608 已提交
65

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

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

72 73 74
const isH5 = !process.env.UNI_SUB_PLATFORM && process.env.UNI_PLATFORM === 'h5'
const isProduction = process.env.NODE_ENV === 'production'

fxy060608's avatar
fxy060608 已提交
75 76 77
if (process.env.UNI_CLOUD_SPACES) {
  try {
    const spaces = JSON.parse(process.env.UNI_CLOUD_SPACES)
fxy060608's avatar
fxy060608 已提交
78
    if (Array.isArray(spaces)) {
79
      const hasUniCloudSpace = spaces.length > 0
fxy060608's avatar
fxy060608 已提交
80
      if (spaces.length === 1) {
fxy060608's avatar
fxy060608 已提交
81
        const space = spaces[0]
fxy060608's avatar
fxy060608 已提交
82 83 84
        console.log(uniI18n.__('pluginUni.currentProjectDefaultSpaceId', {
          0: space.id
        }))
fxy060608's avatar
fxy060608 已提交
85
      }
86 87 88 89 90 91

      if (
        hasUniCloudSpace &&
        isH5 &&
        isProduction
      ) {
fxy060608's avatar
fxy060608 已提交
92 93 94
        console.warn(uniI18n.__('pluginUni.unicloudReleaseH5', {
          0: 'https://uniapp.dcloud.io/uniCloud/quickstart?id=useinh5'
        }))
95 96 97 98 99
      } else if (
        hasUniCloudSpace &&
        isH5 &&
        !isProduction
      ) {
fxy060608's avatar
fxy060608 已提交
100 101 102
        console.warn(uniI18n.__('pluginUni.unicloudShowedRunByHBuilderX', {
          0: 'https://uniapp.dcloud.io/uniCloud/quickstart?id=useinh5'
        }))
103 104
      }

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

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

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

Q
qiang 已提交
136
process.env.UNI_OUTPUT_DEFAULT_DIR = path.resolve(process.env.UNI_CLI_CONTEXT, defaultOutputDir)
fxy060608's avatar
fxy060608 已提交
137 138 139 140 141 142
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
143
process.env.UNI_OUTPUT_DIR = process.env.UNI_OUTPUT_DIR || process.env.UNI_OUTPUT_DEFAULT_DIR
fxy060608's avatar
fxy060608 已提交
144 145 146 147 148 149 150

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 已提交
151
if (process.env.NODE_ENV === 'production') { // 发行模式,不启用 cache
152
  delete process.env.UNI_USING_CACHE
fxy060608's avatar
fxy060608 已提交
153 154
}

fxy060608's avatar
fxy060608 已提交
155 156 157 158 159 160
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 已提交
161
const {
fxy060608's avatar
fxy060608 已提交
162
  normalizePath,
fxy060608's avatar
fxy060608 已提交
163 164
  isSupportSubPackages,
  runByHBuilderX,
fxy060608's avatar
fxy060608 已提交
165
  getPagesJson
166 167 168
} = require('@dcloudio/uni-cli-shared')

process.env.RUN_BY_HBUILDERX = JSON.stringify(runByHBuilderX)
169 170 171 172 173 174

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

initUniModules()
fxy060608's avatar
fxy060608 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

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

fxy060608's avatar
fxy060608 已提交
198 199 200 201 202 203 204 205
if (manifestJsonObj.debug) {
  process.env.VUE_APP_DEBUG = true
}

process.UNI_STAT_CONFIG = {
  appid: manifestJsonObj.appid
}

206 207
// 默认启用 自定义组件模式
// if (isInHBuilderXAlpha) {
208
let usingComponentsAbsent = false
209
if (!hasOwn(platformOptions, 'usingComponents')) {
210
  usingComponentsAbsent = true
211
}
fxy060608's avatar
fxy060608 已提交
212
platformOptions.usingComponents = true
213
// }
fxy060608's avatar
fxy060608 已提交
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

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
    }
  }
244 245
  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 已提交
246 247 248 249 250 251
}

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

252
let isNVueCompiler = true
fxy060608's avatar
fxy060608 已提交
253
if (process.env.UNI_PLATFORM === 'app-plus') {
254 255
  if (platformOptions.nvueCompiler === 'weex') {
    isNVueCompiler = false
fxy060608's avatar
fxy060608 已提交
256
  }
fxy060608's avatar
fxy060608 已提交
257 258 259 260 261 262 263

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

fxy060608's avatar
fxy060608 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281
  // 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 已提交
282
  }
fxy060608's avatar
fxy060608 已提交
283 284 285
} else { // 其他平台,待确认配置方案
  if (
    manifestJsonObj['app-plus'] &&
fxy060608's avatar
fxy060608 已提交
286
    manifestJsonObj['app-plus'].nvueCompiler === 'weex'
fxy060608's avatar
fxy060608 已提交
287
  ) {
288
    isNVueCompiler = false
fxy060608's avatar
fxy060608 已提交
289 290 291 292 293 294 295
  }
}

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

296
if (platformOptions.nvueStyleCompiler === 'uni-app') {
297
  process.env.UNI_USING_NVUE_STYLE_COMPILER = true
298 299
} else {
  platformOptions.nvueStyleCompiler = 'weex'
300 301
}

fxy060608's avatar
fxy060608 已提交
302 303 304 305 306 307 308 309 310
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
  }
}

311 312
// 兼容历史配置 betterScopedSlots
const modes = ['legacy', 'auto', 'augmented']
Q
qiang 已提交
313 314
const scopedSlotsCompiler = !platformOptions.scopedSlotsCompiler && platformOptions.betterScopedSlots ? modes[2]
  : platformOptions.scopedSlotsCompiler
315
process.env.SCOPED_SLOTS_COMPILER = modes.includes(scopedSlotsCompiler) ? scopedSlotsCompiler : modes[1]
316
// 快手小程序、小红书小程序 抽象组件编译报错,如未指定 legacy 固定为 augmented 模式
M
mehaotian 已提交
317 318
if ((process.env.UNI_PLATFORM === 'mp-kuaishou' || process.env.UNI_PLATFORM === 'mp-xhs') && process.env
  .SCOPED_SLOTS_COMPILER !== modes[0]) {
319 320
  process.env.SCOPED_SLOTS_COMPILER = modes[2]
}
321

322 323
process.env.UNI_STAT_UNI_CLOUD = ''
process.env.UNI_STAT_DEBUG = ''
fxy060608's avatar
fxy060608 已提交
324
if (
325 326
  process.env.UNI_USING_COMPONENTS ||
  process.env.UNI_PLATFORM === 'h5'
fxy060608's avatar
fxy060608 已提交
327
) { // 自定义组件模式或 h5 平台
328 329 330 331 332
  const uniStatistics = Object.assign(
    manifestJsonObj.uniStatistics || {},
    platformOptions.uniStatistics || {}
  )

fxy060608's avatar
fxy060608 已提交
333
  if (uniStatistics.enable === true) {
M
mehaotian 已提交
334
    process.env.UNI_USING_STAT = uniStatistics.version === '2' ? '2' : '1'
335 336 337 338 339
    // 获取服务空间配置信息
    const uniCloudConfig = uniStatistics.uniCloud || {}
    process.env.UNI_STAT_UNI_CLOUD = JSON.stringify(uniCloudConfig)
    process.env.UNI_STAT_DEBUG = uniStatistics.debug === true ? 'true' : 'false'

M
mehaotian 已提交
340 341 342 343 344 345 346 347 348
    if (process.env.NODE_ENV === 'production') {
      if (!process.UNI_STAT_CONFIG.appid) {
        console.log()
        console.warn(uniI18n.__('pluginUni.uniStatisticsNoAppid', {
          0: 'https://ask.dcloud.net.cn/article/36303'
        }))
        console.log()
      } else {
        if (!uniStatistics.version) {
M
mehaotian 已提交
349 350 351 352 353
          console.log()
          console.warn(uniI18n.__('pluginUni.uniStatisticsNoVersion', {
            0: 'https://uniapp.dcloud.io/uni-stat-v2.html'
          }))
          console.log()
M
mehaotian 已提交
354 355 356 357
        } else {
          console.log()
          console.warn(`已开启 uni统计${uniStatistics.version}.0 版本`)
          console.log()
M
mehaotian 已提交
358 359
        }
      }
360
    } else {
361
      if (!uniStatistics.version) {
M
mehaotian 已提交
362 363 364 365 366
        console.log()
        console.warn(uniI18n.__('pluginUni.uniStatisticsNoVersion', {
          0: 'https://uniapp.dcloud.io/uni-stat-v2.html'
        }))
        console.log()
367 368 369 370 371
      } else {
        console.log()
        console.warn(`已开启 uni统计${uniStatistics.version}.0 版本`)
        console.log()
      }
372 373 374 375
    }
  }
}

fxy060608's avatar
fxy060608 已提交
376 377 378 379 380 381 382 383 384
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 已提交
385 386
  }
}
387

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

const needWarning = !platformOptions.usingComponents || usingComponentsAbsent
fxy060608's avatar
fxy060608 已提交
394
let hasNVue = false
fxy060608's avatar
fxy060608 已提交
395
// 输出编译器版本等信息
D
DCloud_LXH 已提交
396 397 398
const pagesPkg = require('@dcloudio/webpack-uni-pages-loader/package.json')
process.env.UNI_COMPILER_VERSION = ''
if (pagesPkg) {
Q
qiang 已提交
399
  process.env.UNI_COMPILER_VERSION = pagesPkg['uni-app'].compilerVersion
D
DCloud_LXH 已提交
400
}
Q
qiang 已提交
401
const compileModeUrl = 'https://ask.dcloud.net.cn/article/36074'
fxy060608's avatar
fxy060608 已提交
402
if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
Q
qiang 已提交
403
  const compileMode = (process.env.UNI_USING_V3_NATIVE ? '(v3)' : '') + '' + (isNVueCompiler ? 'uni-app' : 'weex')
fxy060608's avatar
fxy060608 已提交
404 405 406 407
  console.log(uniI18n.__('pluginUni.nvueCompileModeForDetail', {
    0: compileMode,
    1: compileModeUrl
  }))
fxy060608's avatar
fxy060608 已提交
408
} else if (process.env.UNI_PLATFORM !== 'h5' && process.env.UNI_PLATFORM !== 'quickapp-native') {
fxy060608's avatar
fxy060608 已提交
409
  try {
Q
qiang 已提交
410
    const info = process.env.UNI_COMPILER_VERSION
fxy060608's avatar
fxy060608 已提交
411
    if (process.env.UNI_PLATFORM === 'app-plus') {
fxy060608's avatar
fxy060608 已提交
412 413 414
      if (process.env.UNI_USING_V3) {
        console.log(info)
      } else {
fxy060608's avatar
fxy060608 已提交
415 416 417 418 419 420 421 422 423
        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 已提交
424 425 426 427
          console.log(uniI18n.__('pluginUni.nvueCompileModeForDetail', {
            0: (isNVueCompiler ? 'uni-app' : 'weex'),
            1: compileModeUrl
          }))
fxy060608's avatar
fxy060608 已提交
428
        } else {
429
          console.log(info)
fxy060608's avatar
fxy060608 已提交
430 431 432
          if (needWarning) {
            console.log(warningMsg)
          }
433
        }
fxy060608's avatar
fxy060608 已提交
434 435
      }
    } else {
436
      if (needWarning) {
437
        console.log(warningMsg)
438
      }
fxy060608's avatar
fxy060608 已提交
439 440 441
    }
  } catch (e) {}
}
fxy060608's avatar
fxy060608 已提交
442
if (process.env.NODE_ENV !== 'production') { // 运行模式性能提示
d-u-a's avatar
d-u-a 已提交
443
  let perfMsg = uniI18n.__('pluginUni.runDebugMode')
fxy060608's avatar
fxy060608 已提交
444
  if (hasNVue) { // app-nvue
d-u-a's avatar
d-u-a 已提交
445
    perfMsg = perfMsg + uniI18n.__('pluginUni.runDebugModeNvue')
fxy060608's avatar
fxy060608 已提交
446
  } else if (process.env.UNI_PLATFORM.indexOf('mp-') === 0) { // 小程序
d-u-a's avatar
d-u-a 已提交
447
    perfMsg = perfMsg + uniI18n.__('pluginUni.runDebugModeMP')
fxy060608's avatar
fxy060608 已提交
448 449 450
  }
  console.log(perfMsg)
}
fxy060608's avatar
fxy060608 已提交
451

452
// 将 template-compiler 指向修订后的版本
fxy060608's avatar
fxy060608 已提交
453
moduleAlias.addAlias('vue-template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/vue-template-compiler')
fxy060608's avatar
fxy060608 已提交
454
moduleAlias.addAlias('@megalo/template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/@megalo/template-compiler')
455
moduleAlias.addAlias('mpvue-template-compiler', '@dcloudio/vue-cli-plugin-uni/packages/mpvue-template-compiler')
456
// vue-loader
fxy060608's avatar
fxy060608 已提交
457
moduleAlias.addAlias('vue-loader', '@dcloudio/vue-cli-plugin-uni/packages/vue-loader')
fxy060608's avatar
fxy060608 已提交
458
// sass-loader
fxy060608's avatar
fxy060608 已提交
459
moduleAlias.addAlias('sass-loader', '@dcloudio/vue-cli-plugin-uni/packages/sass-loader')
fxy060608's avatar
fxy060608 已提交
460

fxy060608's avatar
fxy060608 已提交
461 462
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 已提交
463
  moduleAlias.addAlias('../runtime/getUrl.js', '@dcloudio/vue-cli-plugin-uni/lib/app-plus/getUrl.js')
fxy060608's avatar
fxy060608 已提交
464
  moduleAlias.addAlias('vue-style-loader', '@dcloudio/vue-cli-plugin-uni/packages/app-vue-style-loader')
465
}
fxy060608's avatar
fxy060608 已提交
466

Q
qiang 已提交
467 468 469
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 已提交
470

P
panyiming.325 已提交
471
if (process.env.UNI_PLATFORM === 'mp-toutiao' || process.env.UNI_PLATFORM === 'mp-lark') {
472 473 474 475 476 477 478
  // !important 始终带有一个空格
  moduleAlias.addAlias(
    'postcss-normalize-whitespace',
    '@dcloudio/vue-cli-plugin-uni/packages/postcss-normalize-whitespace'
  )
}

fxy060608's avatar
fxy060608 已提交
479 480
if (runByHBuilderX) {
  const oldError = console.error
Q
qiang 已提交
481
  console.error = function (msg) {
fxy060608's avatar
fxy060608 已提交
482
    if (typeof msg === 'string' && msg.includes(
Q
qiang 已提交
483
      '[BABEL] Note: The code generator has deoptimised the styling of')) {
fxy060608's avatar
fxy060608 已提交
484 485
      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 已提交
486
      console.log('[' + uniI18n.__('warning') + '] `' + path.relative(process.env.UNI_INPUT_DIR, filePath) +
fxy060608's avatar
fxy060608 已提交
487 488 489 490 491 492 493
        '` 文件体积超过 500KB,已跳过压缩以及 ES6 转 ES5 的处理,手机端使用过大的js库影响性能。')
    } else {
      oldError.apply(console, arguments)
      // TODO 如果是首次运行遇到错误直接退出
    }
  }
}
494

fxy060608's avatar
fxy060608 已提交
495 496
if (
  process.env.UNI_USING_CACHE &&
fxy060608's avatar
fxy060608 已提交
497
  process.env.UNI_PLATFORM !== 'h5' &&
fxy060608's avatar
fxy060608 已提交
498
  !process.env.UNI_USING_V3 &&
fxy060608's avatar
fxy060608 已提交
499 500
  !process.env.UNI_USING_NATIVE &&
  !process.env.UNI_USING_V3_NATIVE
fxy060608's avatar
fxy060608 已提交
501 502
) { // 使用 cache, 拷贝 cache 的 json
  const cacheJsonDir = path.resolve(
fxy060608's avatar
fxy060608 已提交
503
    process.env.UNI_CLI_CONTEXT,
fxy060608's avatar
fxy060608 已提交
504
    'node_modules/.cache/uni-pages-loader/' + process.env.UNI_PLATFORM
fxy060608's avatar
fxy060608 已提交
505 506 507 508
  )
  if (!fs.existsSync(cacheJsonDir)) { //  创建 cache 目录
    mkdirp(cacheJsonDir)
  } else {
fxy060608's avatar
fxy060608 已提交
509
    require('@dcloudio/uni-cli-shared/lib/cache').restore()
fxy060608's avatar
fxy060608 已提交
510 511 512
  }
}

513
const {
514
  initAutoImportComponents,
fxy060608's avatar
fxy060608 已提交
515
  initAutoImportScanComponents
516 517 518 519 520 521 522
} = 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()
}
523

fxy060608's avatar
fxy060608 已提交
524 525 526 527
global.uniPlugin.configureEnv.forEach(configureEnv => {
  configureEnv()
})

fxy060608's avatar
fxy060608 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
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 已提交
549
if (process.env.UNI_PLATFORM.startsWith('mp-')) {
fxy060608's avatar
fxy060608 已提交
550 551 552
  console.log(uniI18n.__('pluginUni.mpBrowserKernelDifference', {
    0: 'https://uniapp.dcloud.io/matter?id=mp'
  }))
fxy060608's avatar
fxy060608 已提交
553 554
}

d-u-a's avatar
d-u-a 已提交
555
runByHBuilderX && console.log(uniI18n.__('compiling'))
fxy060608's avatar
fxy060608 已提交
556 557 558

module.exports = {
  manifestPlatformOptions: platformOptions
D
DCloud_LXH 已提交
559
}