easycom.ts 4.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import path from 'path'
fxy060608's avatar
fxy060608 已提交
2
import type { Plugin } from 'vite'
fxy060608's avatar
fxy060608 已提交
3
import { createFilter, FilterPattern } from '@rollup/pluginutils'
fxy060608's avatar
fxy060608 已提交
4 5
import { camelize, capitalize } from '@vue/shared'

6
import { COMPONENT_PREFIX, isBuiltInComponent } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
7
import {
fxy060608's avatar
fxy060608 已提交
8
  EXTNAME_VUE,
fxy060608's avatar
fxy060608 已提交
9 10
  H5_COMPONENTS_STYLE_PATH,
  BASE_COMPONENTS_STYLE_PATH,
11
  COMPONENT_DEPS_CSS,
fxy060608's avatar
fxy060608 已提交
12
  parseVueRequest,
fxy060608's avatar
fxy060608 已提交
13 14
  buildInCssSet,
  isCombineBuiltInCss,
fxy060608's avatar
fxy060608 已提交
15
  matchEasycom,
fxy060608's avatar
fxy060608 已提交
16 17
  addImportDeclaration,
  genResolveEasycomCode,
fxy060608's avatar
fxy060608 已提交
18 19
} from '@dcloudio/uni-cli-shared'

fxy060608's avatar
fxy060608 已提交
20
const H5_COMPONENTS_PATH = '@dcloudio/uni-h5'
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

const baseComponents = [
  'audio',
  'button',
  'canvas',
  'checkbox',
  'checkbox-group',
  'editor',
  'form',
  'icon',
  'image',
  'input',
  'label',
  'movable-area',
  'movable-view',
  'navigator',
  'picker-view',
  'picker-view-column',
  'progress',
  'radio',
  'radio-group',
  'resize-sensor',
  'rich-text',
  'scroll-view',
  'slider',
  'swiper',
  'swiper-item',
  'switch',
  'text',
  'textarea',
  'view',
]
fxy060608's avatar
fxy060608 已提交
53

fxy060608's avatar
fxy060608 已提交
54 55 56 57
interface UniEasycomPluginOptions {
  include?: FilterPattern
  exclude?: FilterPattern
}
fxy060608's avatar
fxy060608 已提交
58

fxy060608's avatar
fxy060608 已提交
59
export function uniEasycomPlugin(options: UniEasycomPluginOptions): Plugin {
fxy060608's avatar
fxy060608 已提交
60
  const filter = createFilter(options.include, options.exclude)
fxy060608's avatar
fxy060608 已提交
61
  let needCombineBuiltInCss = false
fxy060608's avatar
fxy060608 已提交
62
  return {
fxy060608's avatar
fxy060608 已提交
63 64 65 66
    name: 'vite:uni-h5-easycom',
    configResolved(config) {
      needCombineBuiltInCss = isCombineBuiltInCss(config)
    },
fxy060608's avatar
fxy060608 已提交
67 68 69 70
    transform(code, id) {
      if (!filter(id)) {
        return
      }
fxy060608's avatar
fxy060608 已提交
71
      const { filename, query } = parseVueRequest(id)
fxy060608's avatar
fxy060608 已提交
72 73 74 75
      if (
        query.type !== 'template' &&
        (query.vue || !EXTNAME_VUE.includes(path.extname(filename)))
      ) {
fxy060608's avatar
fxy060608 已提交
76 77 78 79
        return
      }
      let i = 0
      const importDeclarations: string[] = []
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84
      code = code.replace(
        /_resolveComponent\("(.+?)"(, true)?\)/g,
        (str, name) => {
          if (name && !name.startsWith('_')) {
            if (isBuiltInComponent(name)) {
85
              name = name.replace(COMPONENT_PREFIX, '')
fxy060608's avatar
fxy060608 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
              const local = `__syscom_${i++}`
              if (needCombineBuiltInCss) {
                // 发行模式下,应该将内置组件css输出到入口css中
                resolveBuiltInCssImport(name).forEach((cssImport) =>
                  buildInCssSet.add(cssImport)
                )
                return addImportDeclaration(
                  importDeclarations,
                  local,
                  H5_COMPONENTS_PATH,
                  capitalize(camelize(name))
                )
              }
fxy060608's avatar
fxy060608 已提交
99 100
              return addBuiltInImportDeclaration(
                importDeclarations,
fxy060608's avatar
fxy060608 已提交
101
                local,
fxy060608's avatar
fxy060608 已提交
102 103 104 105 106
                name
              )
            }
            const source = matchEasycom(name)
            if (source) {
fxy060608's avatar
fxy060608 已提交
107 108 109 110
              // 处理easycom组件优先级
              return genResolveEasycomCode(
                importDeclarations,
                str,
fxy060608's avatar
fxy060608 已提交
111 112 113 114 115
                addImportDeclaration(
                  importDeclarations,
                  `__easycom_${i++}`,
                  source
                )
fxy060608's avatar
fxy060608 已提交
116 117
              )
            }
fxy060608's avatar
fxy060608 已提交
118
          }
fxy060608's avatar
fxy060608 已提交
119
          return str
fxy060608's avatar
fxy060608 已提交
120
        }
fxy060608's avatar
fxy060608 已提交
121
      )
fxy060608's avatar
fxy060608 已提交
122 123 124
      if (importDeclarations.length) {
        code = importDeclarations.join('') + code
      }
fxy060608's avatar
fxy060608 已提交
125 126
      return {
        code,
fxy060608's avatar
fxy060608 已提交
127
        map: null,
fxy060608's avatar
fxy060608 已提交
128
      }
fxy060608's avatar
fxy060608 已提交
129 130 131 132
    },
  }
}

fxy060608's avatar
fxy060608 已提交
133 134 135 136 137 138 139 140 141 142 143 144
function resolveBuiltInCssImport(name: string) {
  const cssImports: string[] = []
  if (baseComponents.includes(name)) {
    cssImports.push(BASE_COMPONENTS_STYLE_PATH + name + '.css')
  } else {
    cssImports.push(H5_COMPONENTS_STYLE_PATH + name + '.css')
  }
  const deps = COMPONENT_DEPS_CSS[name as keyof typeof COMPONENT_DEPS_CSS]
  deps && deps.forEach((dep) => cssImports.push(dep))
  return cssImports
}

fxy060608's avatar
fxy060608 已提交
145
function addBuiltInImportDeclaration(
fxy060608's avatar
fxy060608 已提交
146 147 148 149
  importDeclarations: string[],
  local: string,
  name: string
) {
fxy060608's avatar
fxy060608 已提交
150 151 152
  resolveBuiltInCssImport(name).forEach((cssImport) =>
    importDeclarations.push(`import '${cssImport}';`)
  )
fxy060608's avatar
fxy060608 已提交
153 154 155
  return addImportDeclaration(
    importDeclarations,
    local,
fxy060608's avatar
fxy060608 已提交
156
    H5_COMPONENTS_PATH,
fxy060608's avatar
fxy060608 已提交
157 158 159
    capitalize(camelize(name))
  )
}