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

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

12
import { VitePluginUniResolvedOptions } from '..'
fxy060608's avatar
fxy060608 已提交
13
import { transformMatchMedia } from './transforms/transformMatchMedia'
fxy060608's avatar
fxy060608 已提交
14
import { createTransformEvent } from './transforms/transformEvent'
15
import { transformContext } from './transforms/transformContext'
16

fxy060608's avatar
fxy060608 已提交
17 18 19 20 21
function createUniVueTransformAssetUrls(
  base: string
): SFCTemplateCompileOptions['transformAssetUrls'] {
  return {
    base,
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    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 已提交
38
}
39

fxy060608's avatar
fxy060608 已提交
40 41
export function initPluginVueOptions(
  options: VitePluginUniResolvedOptions,
fxy060608's avatar
fxy060608 已提交
42
  UniVitePlugins: UniVitePlugin[]
fxy060608's avatar
fxy060608 已提交
43
) {
44
  const vueOptions = options.vueOptions || (options.vueOptions = {})
fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50 51 52
  if (!vueOptions.include) {
    vueOptions.include = []
  }
  if (!isArray(vueOptions.include)) {
    vueOptions.include = [vueOptions.include]
  }
  vueOptions.include.push(EXTNAME_VUE_RE)

53 54
  const templateOptions = vueOptions.template || (vueOptions.template = {})

fxy060608's avatar
fxy060608 已提交
55 56 57
  templateOptions.transformAssetUrls = createUniVueTransformAssetUrls(
    options.base
  )
58

fxy060608's avatar
fxy060608 已提交
59 60 61 62
  let isCompilerNativeTag: ParserOptions['isNativeTag'] = isNativeTag
  let isCompilerCustomElement: ParserOptions['isCustomElement'] =
    isCustomElement

fxy060608's avatar
fxy060608 已提交
63 64
  let directiveTransforms: CompilerOptions['directiveTransforms']

fxy060608's avatar
fxy060608 已提交
65
  UniVitePlugins.forEach((plugin) => {
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75 76
    const compilerOptions = plugin.uni?.compilerOptions
    if (compilerOptions) {
      if (compilerOptions.isNativeTag) {
        isCompilerNativeTag = compilerOptions.isNativeTag
      }
      if (compilerOptions.isCustomElement) {
        isCompilerCustomElement = compilerOptions.isCustomElement
      }
      if (compilerOptions.directiveTransforms) {
        directiveTransforms = compilerOptions.directiveTransforms
      }
fxy060608's avatar
fxy060608 已提交
77 78
    }
  })
79 80
  const compilerOptions =
    templateOptions.compilerOptions || (templateOptions.compilerOptions = {})
fxy060608's avatar
fxy060608 已提交
81
  compilerOptions.isNativeTag = isCompilerNativeTag
82 83 84
  if (!compilerOptions.nodeTransforms) {
    compilerOptions.nodeTransforms = []
  }
85 86 87 88 89 90 91 92

  const compatConfig = parseCompatConfigOnce(options.inputDir)

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

fxy060608's avatar
fxy060608 已提交
93
  compilerOptions.isCustomElement = isCompilerCustomElement
fxy060608's avatar
fxy060608 已提交
94

fxy060608's avatar
fxy060608 已提交
95 96 97
  const eventOpts = UniVitePlugins.reduce<Record<string, string>>(
    (eventOpts, UniVitePlugin) => {
      return extend(eventOpts, UniVitePlugin.uni?.transformEvent)
fxy060608's avatar
fxy060608 已提交
98 99 100
    },
    {}
  )
101
  compilerOptions.nodeTransforms.unshift(transformContext)
fxy060608's avatar
fxy060608 已提交
102
  compilerOptions.nodeTransforms.unshift(createTransformEvent(eventOpts))
fxy060608's avatar
fxy060608 已提交
103 104 105 106
  if (options.platform !== 'mp-weixin') {
    compilerOptions.nodeTransforms.unshift(transformMatchMedia)
  }

fxy060608's avatar
fxy060608 已提交
107 108 109 110 111 112 113
  if (directiveTransforms) {
    compilerOptions.directiveTransforms = extend(
      compilerOptions.directiveTransforms || {},
      directiveTransforms
    )
  }

114 115
  return vueOptions
}
fxy060608's avatar
fxy060608 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

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
}