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

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

export function createConfigResolved(options: VitePluginUniResolvedOptions) {
  return ((config) => {
fxy060608's avatar
fxy060608 已提交
18
    initEnv(config)
fxy060608's avatar
fxy060608 已提交
19
    initLogger(config)
fxy060608's avatar
fxy060608 已提交
20 21
    initOptions(options, config)
    initPlugins(config, options)
fxy060608's avatar
fxy060608 已提交
22
    initCheckUpdate()
23 24 25
    if (isWindows) {
      // TODO 等 https://github.com/vitejs/vite/issues/3331 修复后,可以移除下列代码
      const item = config.resolve.alias.find((item) =>
fxy060608's avatar
fxy060608 已提交
26
        typeof item.find !== 'string' ? item.find.test('@/') : false
27 28 29 30 31
      )
      if (item) {
        item.customResolver = customResolver
      }
    }
32 33
  }) as Plugin['configResolved']
}
fxy060608's avatar
fxy060608 已提交
34 35

function initCheckUpdate() {
fxy060608's avatar
fxy060608 已提交
36 37
  checkUpdate({
    inputDir: process.env.UNI_INPUT_DIR,
fxy060608's avatar
fxy060608 已提交
38 39
    compilerVersion: process.env.UNI_COMPILER_VERSION,
    versionType: process.env.UNI_COMPILER_VERSION_TYPE,
fxy060608's avatar
fxy060608 已提交
40
  })
fxy060608's avatar
fxy060608 已提交
41
}
fxy060608's avatar
fxy060608 已提交
42 43

function initLogger({ logger }: ResolvedConfig) {
44
  const { info, warn, error } = logger
fxy060608's avatar
fxy060608 已提交
45
  logger.info = (msg, opts) => {
fxy060608's avatar
fxy060608 已提交
46
    msg = formatInfoMsg(msg, opts)
fxy060608's avatar
fxy060608 已提交
47 48 49 50
    if (msg) {
      return info(msg, opts)
    }
  }
51
  logger.warn = (msg, opts) => {
fxy060608's avatar
fxy060608 已提交
52
    msg = formatWarnMsg(msg, opts)
53 54 55 56
    if (msg) {
      return warn(msg, opts)
    }
  }
fxy060608's avatar
fxy060608 已提交
57
  logger.error = (msg, opts) => {
fxy060608's avatar
fxy060608 已提交
58
    msg = formatErrMsg(msg, opts)
fxy060608's avatar
fxy060608 已提交
59 60 61
    if (msg) {
      return error(msg, opts)
    }
fxy060608's avatar
fxy060608 已提交
62 63
  }
}