options.ts 3.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { Plugin } from 'vite'
fxy060608's avatar
fxy060608 已提交
2
import { extend, hasOwn, isArray } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
3
import { SFCTemplateCompileOptions } from '@vue/compiler-sfc'
fxy060608's avatar
fxy060608 已提交
4

fxy060608's avatar
fxy060608 已提交
5
import { isCustomElement, isNativeTag } from '@dcloudio/uni-shared'
6
import { EXTNAME_VUE_RE, parseCompatConfigOnce } from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
7

8
import { VitePluginUniResolvedOptions } from '..'
fxy060608's avatar
fxy060608 已提交
9
import { transformMatchMedia } from './transforms/transformMatchMedia'
fxy060608's avatar
fxy060608 已提交
10
import { createTransformEvent } from './transforms/transformEvent'
11
import { transformContext } from './transforms/transformContext'
12

fxy060608's avatar
fxy060608 已提交
13 14 15 16 17
function createUniVueTransformAssetUrls(
  base: string
): SFCTemplateCompileOptions['transformAssetUrls'] {
  return {
    base,
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    tags: {
      audio: ['src'],
      video: ['src', 'poster'],
      img: ['src'],
      image: ['src'],
      'cover-image': ['src'],
      // h5
      'v-uni-audio': ['src'],
      'v-uni-video': ['src', 'poster'],
      'v-uni-image': ['src'],
      'v-uni-cover-image': ['src'],
      // nvue
      'u-image': ['src'],
      'u-video': ['src', 'poster'],
    },
  }
fxy060608's avatar
fxy060608 已提交
34
}
35

fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41 42 43 44 45
interface UniPlugin extends Plugin {
  uni?: {
    transformEvent?: Record<string, string>
  }
}

export function initPluginVueOptions(
  options: VitePluginUniResolvedOptions,
  uniPlugins: UniPlugin[]
) {
46
  const vueOptions = options.vueOptions || (options.vueOptions = {})
fxy060608's avatar
fxy060608 已提交
47 48 49 50 51 52 53 54
  if (!vueOptions.include) {
    vueOptions.include = []
  }
  if (!isArray(vueOptions.include)) {
    vueOptions.include = [vueOptions.include]
  }
  vueOptions.include.push(EXTNAME_VUE_RE)

55 56
  const templateOptions = vueOptions.template || (vueOptions.template = {})

fxy060608's avatar
fxy060608 已提交
57 58 59
  templateOptions.transformAssetUrls = createUniVueTransformAssetUrls(
    options.base
  )
60 61 62 63 64 65 66

  const compilerOptions =
    templateOptions.compilerOptions || (templateOptions.compilerOptions = {})
  compilerOptions.isNativeTag = isNativeTag
  if (!compilerOptions.nodeTransforms) {
    compilerOptions.nodeTransforms = []
  }
67 68 69 70 71 72 73 74

  const compatConfig = parseCompatConfigOnce(options.inputDir)

  compilerOptions.compatConfig = extend(
    compilerOptions.compatConfig || {},
    compatConfig
  )

fxy060608's avatar
fxy060608 已提交
75 76
  const eventOpts = uniPlugins.reduce<Record<string, string>>(
    (eventOpts, uniPlugin) => {
fxy060608's avatar
fxy060608 已提交
77
      return extend(eventOpts, uniPlugin.uni?.transformEvent)
fxy060608's avatar
fxy060608 已提交
78 79 80
    },
    {}
  )
81
  compilerOptions.nodeTransforms.unshift(transformContext)
fxy060608's avatar
fxy060608 已提交
82
  compilerOptions.nodeTransforms.unshift(createTransformEvent(eventOpts))
fxy060608's avatar
fxy060608 已提交
83 84 85 86
  if (options.platform !== 'mp-weixin') {
    compilerOptions.nodeTransforms.unshift(transformMatchMedia)
  }

87 88
  return vueOptions
}
fxy060608's avatar
fxy060608 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

export function initPluginVueJsxOptions(options: VitePluginUniResolvedOptions) {
  const vueJsxOptions = options.vueJsxOptions || (options.vueJsxOptions = {})
  if (!hasOwn(vueJsxOptions, 'optimize')) {
    vueJsxOptions.optimize = true
  }
  vueJsxOptions.isCustomElement = isCustomElement
  return vueJsxOptions
}

export function initPluginViteLegacyOptions(
  options: VitePluginUniResolvedOptions
) {
  const viteLegacyOptions =
    options.viteLegacyOptions || (options.viteLegacyOptions = {})
  return viteLegacyOptions
}