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

12 13
import { VitePluginUniResolvedOptions } from '..'

fxy060608's avatar
fxy060608 已提交
14 15
export function initPluginVueOptions(
  options: VitePluginUniResolvedOptions,
fxy060608's avatar
fxy060608 已提交
16
  UniVitePlugins: UniVitePlugin[],
fxy060608's avatar
fxy060608 已提交
17 18 19 20 21
  uniPluginOptions: Required<
    Omit<Required<UniVitePlugin>['uni'], 'compiler'>
  > & {
    compiler?: TemplateCompiler
  }
fxy060608's avatar
fxy060608 已提交
22
) {
23
  const vueOptions = options.vueOptions || (options.vueOptions = {})
24 25 26
  // if (!hasOwn(vueOptions, 'reactivityTransform')) {
  //   vueOptions.reactivityTransform = true
  // }
fxy060608's avatar
fxy060608 已提交
27 28 29 30 31 32 33 34
  if (!vueOptions.include) {
    vueOptions.include = []
  }
  if (!isArray(vueOptions.include)) {
    vueOptions.include = [vueOptions.include]
  }
  vueOptions.include.push(EXTNAME_VUE_RE)

fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40 41
  const styleOptions = vueOptions.style || (vueOptions.style = {})
  if (!styleOptions.postcssPlugins) {
    styleOptions.postcssPlugins = []
  }
  // 解析 scoped 中 deep 等特殊语法
  styleOptions.postcssPlugins.push(uniPostcssScopedPlugin())

42 43 44 45
  const templateOptions = vueOptions.template || (vueOptions.template = {})

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

fxy060608's avatar
fxy060608 已提交
47
  const {
fxy060608's avatar
fxy060608 已提交
48
    compiler,
fxy060608's avatar
fxy060608 已提交
49
    compilerOptions: {
fxy060608's avatar
fxy060608 已提交
50
      miniProgram,
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55
      isNativeTag,
      isCustomElement,
      nodeTransforms,
      directiveTransforms,
    },
fxy060608's avatar
fxy060608 已提交
56
  } = uniPluginOptions
57

fxy060608's avatar
fxy060608 已提交
58 59 60
  if (compiler) {
    templateOptions.compiler = compiler
  }
fxy060608's avatar
fxy060608 已提交
61 62 63
  if (miniProgram) {
    ;(compilerOptions as any).miniProgram = miniProgram
  }
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

  if (isNativeTag) {
    const userIsNativeTag = compilerOptions.isNativeTag
    compilerOptions.isNativeTag = (tag) => {
      if (isNativeTag(tag)) {
        return true
      }
      if (userIsNativeTag && userIsNativeTag(tag)) {
        return true
      }
      return false
    }
  }

  if (isCustomElement) {
    const userIsCustomElement = compilerOptions.isCustomElement
    compilerOptions.isCustomElement = (tag) => {
      if (isCustomElement(tag)) {
        return true
      }
      if (userIsCustomElement && userIsCustomElement(tag)) {
        return true
      }
      return false
    }
  }
fxy060608's avatar
fxy060608 已提交
90 91 92 93

  compilerOptions.directiveTransforms = {
    ...compilerOptions.directiveTransforms,
    ...directiveTransforms,
fxy060608's avatar
fxy060608 已提交
94 95
  }

96 97 98
  if (!compilerOptions.nodeTransforms) {
    compilerOptions.nodeTransforms = []
  }
99 100 101 102 103 104 105 106 107 108 109 110
  if (options.platform === 'h5') {
    templateOptions.transformAssetUrls = createUniVueTransformAssetUrls(
      isExternalUrl(options.base) ? options.base : ''
    )
  } else {
    // 替换内置的 transformAssetUrls 逻辑
    templateOptions.transformAssetUrls = {
      tags: {},
    }
    compilerOptions.nodeTransforms.push(...getBaseNodeTransforms(options.base))
  }

fxy060608's avatar
fxy060608 已提交
111 112 113
  if (nodeTransforms) {
    compilerOptions.nodeTransforms.push(...nodeTransforms)
  }
114

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

fxy060608's avatar
fxy060608 已提交
117 118 119 120
  // compilerOptions.compatConfig = extend(
  //   compilerOptions.compatConfig || {},
  //   compatConfig
  // )
fxy060608's avatar
fxy060608 已提交
121

fxy060608's avatar
fxy060608 已提交
122 123
  // App,MP 平台不支持使用静态节点
  compilerOptions.hoistStatic = false
124 125
  // 小程序使用了
  ;(compilerOptions as any).root = process.env.UNI_INPUT_DIR
126 127
  return vueOptions
}
fxy060608's avatar
fxy060608 已提交
128

fxy060608's avatar
fxy060608 已提交
129 130 131 132
export function initPluginVueJsxOptions(
  options: VitePluginUniResolvedOptions,
  {
    isCustomElement,
fxy060608's avatar
fxy060608 已提交
133 134
  }: Required<Required<UniVitePlugin>['uni']>['compilerOptions'],
  jsxOptions: Required<Required<UniVitePlugin>['uni']>['jsxOptions']
fxy060608's avatar
fxy060608 已提交
135
) {
fxy060608's avatar
fxy060608 已提交
136 137 138
  const vueJsxOptions = isPlainObject(options.vueJsxOptions)
    ? options.vueJsxOptions
    : (options.vueJsxOptions = {})
fxy060608's avatar
fxy060608 已提交
139 140 141
  if (!hasOwn(vueJsxOptions, 'optimize')) {
    vueJsxOptions.optimize = true
  }
fxy060608's avatar
fxy060608 已提交
142
  vueJsxOptions.isCustomElement = isCustomElement as (tag: string) => boolean
fxy060608's avatar
fxy060608 已提交
143 144 145 146 147 148 149
  if (!vueJsxOptions.babelPlugins) {
    vueJsxOptions.babelPlugins = []
  }
  if (isArray(jsxOptions.babelPlugins)) {
    vueJsxOptions.babelPlugins.push(...jsxOptions.babelPlugins)
  }

fxy060608's avatar
fxy060608 已提交
150 151 152 153 154 155 156 157 158 159
  return vueJsxOptions
}

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