utils.ts 2.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import fs from 'fs'
fxy060608's avatar
fxy060608 已提交
2
import path from 'path'
fxy060608's avatar
fxy060608 已提交
3 4 5 6
import { BuildOptions, InlineConfig } from 'vite'

import { isInHBuilderX } from '@dcloudio/uni-cli-shared'

fxy060608's avatar
fxy060608 已提交
7
import { CliOptions } from '.'
fxy060608's avatar
fxy060608 已提交
8
import { initNVueEnv } from './nvue'
fxy060608's avatar
fxy060608 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21

export const PLATFORMS = [
  'app',
  'h5',
  'mp-alipay',
  'mp-baidu',
  'mp-qq',
  'mp-toutiao',
  'mp-weixin',
  'quickapp-webview-huawei',
  'quickapp-webview-union',
]

fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
function resolveConfigFile() {
  const viteConfigJs = path.resolve(process.env.UNI_INPUT_DIR, 'vite.config.js')
  const viteConfigTs = path.resolve(process.env.UNI_INPUT_DIR, 'vite.config.ts')

  if (fs.existsSync(viteConfigTs)) {
    return viteConfigTs
  }
  if (fs.existsSync(viteConfigJs)) {
    return viteConfigJs
  }
  return path.resolve(process.env.UNI_CLI_CONTEXT, 'vite.config.js')
}

export function addConfigFile(inlineConfig: InlineConfig) {
  if (isInHBuilderX()) {
    inlineConfig.configFile = resolveConfigFile()
  }
  return inlineConfig
}

fxy060608's avatar
fxy060608 已提交
42 43 44 45 46 47 48 49 50 51 52
export function initEnv(type: 'dev' | 'build', options: CliOptions) {
  if (type === 'dev') {
    process.env.NODE_ENV = 'development'
  } else if (type === 'build') {
    if ((options as BuildOptions).watch) {
      process.env.NODE_ENV = 'development'
    } else {
      process.env.NODE_ENV = 'production'
    }
  }

fxy060608's avatar
fxy060608 已提交
53 54 55
  process.env.UNI_CLI_CONTEXT = isInHBuilderX()
    ? path.resolve(process.env.UNI_HBUILDERX_PLUGINS!, 'uniapp-cli-vite')
    : process.cwd()
fxy060608's avatar
fxy060608 已提交
56

fxy060608's avatar
fxy060608 已提交
57 58
  process.env.UNI_PLATFORM = options.platform as UniApp.PLATFORM

fxy060608's avatar
fxy060608 已提交
59
  process.env.VITE_ROOT_DIR = process.env.UNI_INPUT_DIR || process.cwd()
fxy060608's avatar
fxy060608 已提交
60

fxy060608's avatar
fxy060608 已提交
61 62 63
  process.env.UNI_INPUT_DIR =
    process.env.UNI_INPUT_DIR || path.resolve(process.cwd(), 'src')

fxy060608's avatar
fxy060608 已提交
64 65 66 67
  if (process.env.UNI_OUTPUT_DIR) {
    ;(options as BuildOptions).outDir = process.env.UNI_OUTPUT_DIR
  } else {
    if (!(options as BuildOptions).outDir) {
fxy060608's avatar
fxy060608 已提交
68 69
      ;(options as BuildOptions).outDir = path.resolve(
        process.cwd(),
fxy060608's avatar
fxy060608 已提交
70 71 72 73 74 75 76
        'dist',
        process.env.NODE_ENV === 'production' ? 'build' : 'dev',
        process.env.UNI_PLATFORM
      )
    }
    process.env.UNI_OUTPUT_DIR = (options as BuildOptions).outDir!
  }
fxy060608's avatar
fxy060608 已提交
77
  // tips
fxy060608's avatar
fxy060608 已提交
78 79 80 81 82 83 84 85
  // if (isInHBuilderX() && options.platform === 'app') {
  //   return (
  //     console.error(
  //       `当前项目 Vue 版本为3,暂不支持编译至 App 端,近期将升级支持。`
  //     ),
  //     process.exit(1)
  //   )
  // }
fxy060608's avatar
fxy060608 已提交
86 87 88
  if (process.env.UNI_PLATFORM === 'app') {
    initNVueEnv()
  }
fxy060608's avatar
fxy060608 已提交
89
}
fxy060608's avatar
fxy060608 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

export function cleanOptions(options: CliOptions) {
  const ret = { ...options }
  delete ret['--']

  delete ret.platform
  delete ret.p
  delete ret.ssr

  delete ret.debug
  delete ret.d
  delete ret.filter
  delete ret.f
  delete ret.logLevel
  delete ret.l
  delete ret.clearScreen
  return ret
}