index.ts 1.7 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 38 39 40 41 42
  const pkg = require('@dcloudio/vite-plugin-uni/package.json')
  checkUpdate({
    inputDir: process.env.UNI_INPUT_DIR,
    compilerVersion:
      (pkg['uni-app'] && pkg['uni-app']['compilerVersion']) || '',
    versionType: pkg.version.includes('alpha') ? 'a' : 'r',
  })
fxy060608's avatar
fxy060608 已提交
43
}
fxy060608's avatar
fxy060608 已提交
44 45

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