options.ts 4.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { extend, hasOwn, isArray, isPlainObject } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2
import { SFCTemplateCompileOptions, TemplateCompiler } from '@vue/compiler-sfc'
fxy060608's avatar
fxy060608 已提交
3
import { isCustomElement } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
4 5 6
import {
  EXTNAME_VUE_RE,
  UniVitePlugin,
fxy060608's avatar
fxy060608 已提交
7
  uniPostcssScopedPlugin,
fxy060608's avatar
fxy060608 已提交
8
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
9

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

fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
function createUniVueTransformAssetUrls(
  base: string
): SFCTemplateCompileOptions['transformAssetUrls'] {
  return {
    base,
    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
export function initPluginVueOptions(
  options: VitePluginUniResolvedOptions,
fxy060608's avatar
fxy060608 已提交
40
  UniVitePlugins: UniVitePlugin[],
fxy060608's avatar
fxy060608 已提交
41 42 43 44 45
  uniPluginOptions: Required<
    Omit<Required<UniVitePlugin>['uni'], 'compiler'>
  > & {
    compiler?: TemplateCompiler
  }
fxy060608's avatar
fxy060608 已提交
46
) {
47
  const vueOptions = options.vueOptions || (options.vueOptions = {})
fxy060608's avatar
fxy060608 已提交
48 49 50 51 52 53 54 55
  if (!vueOptions.include) {
    vueOptions.include = []
  }
  if (!isArray(vueOptions.include)) {
    vueOptions.include = [vueOptions.include]
  }
  vueOptions.include.push(EXTNAME_VUE_RE)

fxy060608's avatar
fxy060608 已提交
56 57 58 59 60 61 62
  const styleOptions = vueOptions.style || (vueOptions.style = {})
  if (!styleOptions.postcssPlugins) {
    styleOptions.postcssPlugins = []
  }
  // 解析 scoped 中 deep 等特殊语法
  styleOptions.postcssPlugins.push(uniPostcssScopedPlugin())

63 64
  const templateOptions = vueOptions.template || (vueOptions.template = {})

fxy060608's avatar
fxy060608 已提交
65 66 67 68
  templateOptions.transformAssetUrls = createUniVueTransformAssetUrls(
    options.base
  )

69 70
  const compilerOptions =
    templateOptions.compilerOptions || (templateOptions.compilerOptions = {})
fxy060608's avatar
fxy060608 已提交
71

fxy060608's avatar
fxy060608 已提交
72
  const {
fxy060608's avatar
fxy060608 已提交
73
    compiler,
fxy060608's avatar
fxy060608 已提交
74 75 76 77 78 79
    compilerOptions: {
      isNativeTag,
      isCustomElement,
      nodeTransforms,
      directiveTransforms,
    },
fxy060608's avatar
fxy060608 已提交
80
  } = uniPluginOptions
fxy060608's avatar
fxy060608 已提交
81 82 83
  if (compiler) {
    templateOptions.compiler = compiler
  }
fxy060608's avatar
fxy060608 已提交
84 85 86 87 88 89 90 91 92
  compilerOptions.isNativeTag = isNativeTag
  compilerOptions.isCustomElement = isCustomElement
  if (directiveTransforms) {
    compilerOptions.directiveTransforms = extend(
      compilerOptions.directiveTransforms || {},
      directiveTransforms
    )
  }

93 94 95
  if (!compilerOptions.nodeTransforms) {
    compilerOptions.nodeTransforms = []
  }
fxy060608's avatar
fxy060608 已提交
96 97 98
  if (nodeTransforms) {
    compilerOptions.nodeTransforms.push(...nodeTransforms)
  }
99

fxy060608's avatar
fxy060608 已提交
100
  // const compatConfig = parseCompatConfigOnce(options.inputDir)
101

fxy060608's avatar
fxy060608 已提交
102 103 104 105
  // compilerOptions.compatConfig = extend(
  //   compilerOptions.compatConfig || {},
  //   compatConfig
  // )
fxy060608's avatar
fxy060608 已提交
106

fxy060608's avatar
fxy060608 已提交
107 108 109
  const eventOpts = UniVitePlugins.reduce<Record<string, string>>(
    (eventOpts, UniVitePlugin) => {
      return extend(eventOpts, UniVitePlugin.uni?.transformEvent)
fxy060608's avatar
fxy060608 已提交
110 111 112
    },
    {}
  )
fxy060608's avatar
fxy060608 已提交
113
  // compilerOptions.nodeTransforms.unshift(transformContext)
fxy060608's avatar
fxy060608 已提交
114
  compilerOptions.nodeTransforms.unshift(createTransformEvent(eventOpts))
fxy060608's avatar
fxy060608 已提交
115 116 117 118
  if (options.platform !== 'mp-weixin') {
    compilerOptions.nodeTransforms.unshift(transformMatchMedia)
  }

fxy060608's avatar
fxy060608 已提交
119 120
  // App,MP 平台不支持使用静态节点
  compilerOptions.hoistStatic = false
121 122
  return vueOptions
}
fxy060608's avatar
fxy060608 已提交
123 124

export function initPluginVueJsxOptions(options: VitePluginUniResolvedOptions) {
fxy060608's avatar
fxy060608 已提交
125 126 127
  const vueJsxOptions = isPlainObject(options.vueJsxOptions)
    ? options.vueJsxOptions
    : (options.vueJsxOptions = {})
fxy060608's avatar
fxy060608 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141
  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
}