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

fxy060608's avatar
fxy060608 已提交
6
import { 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)) {
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97
              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 已提交
98 99
              return addBuiltInImportDeclaration(
                importDeclarations,
fxy060608's avatar
fxy060608 已提交
100
                local,
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105
                name
              )
            }
            const source = matchEasycom(name)
            if (source) {
fxy060608's avatar
fxy060608 已提交
106 107 108 109
              // 处理easycom组件优先级
              return genResolveEasycomCode(
                importDeclarations,
                str,
fxy060608's avatar
fxy060608 已提交
110 111 112 113 114
                addImportDeclaration(
                  importDeclarations,
                  `__easycom_${i++}`,
                  source
                )
fxy060608's avatar
fxy060608 已提交
115 116
              )
            }
fxy060608's avatar
fxy060608 已提交
117
          }
fxy060608's avatar
fxy060608 已提交
118
          return str
fxy060608's avatar
fxy060608 已提交
119
        }
fxy060608's avatar
fxy060608 已提交
120
      )
fxy060608's avatar
fxy060608 已提交
121 122 123
      if (importDeclarations.length) {
        code = importDeclarations.join('') + code
      }
fxy060608's avatar
fxy060608 已提交
124 125
      return {
        code,
fxy060608's avatar
fxy060608 已提交
126
        map: null,
fxy060608's avatar
fxy060608 已提交
127
      }
fxy060608's avatar
fxy060608 已提交
128 129 130 131
    },
  }
}

fxy060608's avatar
fxy060608 已提交
132 133 134 135 136 137 138 139 140 141 142 143
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 已提交
144
function addBuiltInImportDeclaration(
fxy060608's avatar
fxy060608 已提交
145 146 147 148
  importDeclarations: string[],
  local: string,
  name: string
) {
fxy060608's avatar
fxy060608 已提交
149 150 151
  resolveBuiltInCssImport(name).forEach((cssImport) =>
    importDeclarations.push(`import '${cssImport}';`)
  )
fxy060608's avatar
fxy060608 已提交
152 153 154
  return addImportDeclaration(
    importDeclarations,
    local,
fxy060608's avatar
fxy060608 已提交
155
    H5_COMPONENTS_PATH,
fxy060608's avatar
fxy060608 已提交
156 157 158
    capitalize(camelize(name))
  )
}