options.ts 3.7 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
  }
fxy060608's avatar
fxy060608 已提交
64 65
  compilerOptions.isNativeTag = isNativeTag
  compilerOptions.isCustomElement = isCustomElement
fxy060608's avatar
fxy060608 已提交
66 67 68 69

  compilerOptions.directiveTransforms = {
    ...compilerOptions.directiveTransforms,
    ...directiveTransforms,
fxy060608's avatar
fxy060608 已提交
70 71
  }

72 73 74
  if (!compilerOptions.nodeTransforms) {
    compilerOptions.nodeTransforms = []
  }
75 76 77 78 79 80 81 82 83 84 85 86
  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 已提交
87 88 89
  if (nodeTransforms) {
    compilerOptions.nodeTransforms.push(...nodeTransforms)
  }
90

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

fxy060608's avatar
fxy060608 已提交
93 94 95 96
  // compilerOptions.compatConfig = extend(
  //   compilerOptions.compatConfig || {},
  //   compatConfig
  // )
fxy060608's avatar
fxy060608 已提交
97

fxy060608's avatar
fxy060608 已提交
98 99
  // App,MP 平台不支持使用静态节点
  compilerOptions.hoistStatic = false
100 101
  // 小程序使用了
  ;(compilerOptions as any).root = process.env.UNI_INPUT_DIR
102 103
  return vueOptions
}
fxy060608's avatar
fxy060608 已提交
104

fxy060608's avatar
fxy060608 已提交
105 106 107 108
export function initPluginVueJsxOptions(
  options: VitePluginUniResolvedOptions,
  {
    isCustomElement,
fxy060608's avatar
fxy060608 已提交
109 110
  }: Required<Required<UniVitePlugin>['uni']>['compilerOptions'],
  jsxOptions: Required<Required<UniVitePlugin>['uni']>['jsxOptions']
fxy060608's avatar
fxy060608 已提交
111
) {
fxy060608's avatar
fxy060608 已提交
112 113 114
  const vueJsxOptions = isPlainObject(options.vueJsxOptions)
    ? options.vueJsxOptions
    : (options.vueJsxOptions = {})
fxy060608's avatar
fxy060608 已提交
115 116 117
  if (!hasOwn(vueJsxOptions, 'optimize')) {
    vueJsxOptions.optimize = true
  }
fxy060608's avatar
fxy060608 已提交
118
  vueJsxOptions.isCustomElement = isCustomElement as (tag: string) => boolean
fxy060608's avatar
fxy060608 已提交
119 120 121 122 123 124 125
  if (!vueJsxOptions.babelPlugins) {
    vueJsxOptions.babelPlugins = []
  }
  if (isArray(jsxOptions.babelPlugins)) {
    vueJsxOptions.babelPlugins.push(...jsxOptions.babelPlugins)
  }

fxy060608's avatar
fxy060608 已提交
126 127 128 129 130 131 132 133 134 135
  return vueJsxOptions
}

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