index.ts 1.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { Plugin, ResolvedConfig } from 'vite'
fxy060608's avatar
fxy060608 已提交
2
import { extend } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
3 4 5
import {
  checkUpdate,
  isWindows,
6
  formatErrMsg,
fxy060608's avatar
fxy060608 已提交
7
  formatInfoMsg,
8
  formatWarnMsg,
fxy060608's avatar
fxy060608 已提交
9
  isInHybridNVue,
fxy060608's avatar
fxy060608 已提交
10
} from '@dcloudio/uni-cli-shared'
11
import { VitePluginUniResolvedOptions } from '..'
fxy060608's avatar
fxy060608 已提交
12 13 14 15

import { initEnv } from './env'
import { initOptions } from './options'
import { initPlugins } from './plugins'
16
import { customResolver } from '../config/resolve'
17 18 19

export function createConfigResolved(options: VitePluginUniResolvedOptions) {
  return ((config) => {
fxy060608's avatar
fxy060608 已提交
20 21 22 23
    // 如果是混合编译且是 nvue 时,部分逻辑无需执行
    if (!isInHybridNVue(config)) {
      initEnv(config)
    }
fxy060608's avatar
fxy060608 已提交
24
    initLogger(config)
fxy060608's avatar
fxy060608 已提交
25 26
    initOptions(options, config)
    initPlugins(config, options)
fxy060608's avatar
fxy060608 已提交
27 28 29
    if (!isInHybridNVue(config)) {
      initCheckUpdate()
    }
30 31
    if (isWindows) {
      // TODO 等 https://github.com/vitejs/vite/issues/3331 修复后,可以移除下列代码
32
      // 2.8.0 已修复,但为了兼容旧版本,先不移除
33
      const item = config.resolve.alias.find((item) =>
fxy060608's avatar
fxy060608 已提交
34
        typeof item.find !== 'string' ? item.find.test('@/') : false
35 36 37 38 39
      )
      if (item) {
        item.customResolver = customResolver
      }
    }
40 41
  }) as Plugin['configResolved']
}
fxy060608's avatar
fxy060608 已提交
42 43

function initCheckUpdate() {
fxy060608's avatar
fxy060608 已提交
44 45
  checkUpdate({
    inputDir: process.env.UNI_INPUT_DIR,
fxy060608's avatar
fxy060608 已提交
46 47
    compilerVersion: process.env.UNI_COMPILER_VERSION,
    versionType: process.env.UNI_COMPILER_VERSION_TYPE,
fxy060608's avatar
fxy060608 已提交
48
  })
fxy060608's avatar
fxy060608 已提交
49
}
fxy060608's avatar
fxy060608 已提交
50

fxy060608's avatar
fxy060608 已提交
51
function initLogger({ logger, nvue }: ResolvedConfig & { nvue?: boolean }) {
52
  const { info, warn, error } = logger
fxy060608's avatar
fxy060608 已提交
53
  logger.info = (msg, opts) => {
54
    msg = formatInfoMsg(msg, extend(opts || {}, { nvue }))
fxy060608's avatar
fxy060608 已提交
55 56 57 58
    if (msg) {
      return info(msg, opts)
    }
  }
59
  logger.warn = (msg, opts) => {
fxy060608's avatar
fxy060608 已提交
60
    msg = formatWarnMsg(msg, opts)
61 62 63 64
    if (msg) {
      return warn(msg, opts)
    }
  }
fxy060608's avatar
fxy060608 已提交
65
  logger.error = (msg, opts) => {
fxy060608's avatar
fxy060608 已提交
66
    msg = formatErrMsg(msg, opts)
fxy060608's avatar
fxy060608 已提交
67 68 69
    if (msg) {
      return error(msg, opts)
    }
fxy060608's avatar
fxy060608 已提交
70 71
  }
}