plugin.ts 3.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import path from 'path'
fxy060608's avatar
fxy060608 已提交
2 3
import type { Plugin } from 'vite'
import { extend, isArray, isString, isFunction } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8
import type {
  CopyOptions,
  UniViteCopyPluginTarget,
  UniVitePlugin,
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
9
import type { TemplateCompiler } from '@vue/compiler-sfc'
fxy060608's avatar
fxy060608 已提交
10
import { VitePluginUniResolvedOptions } from '..'
fxy060608's avatar
fxy060608 已提交
11 12 13 14 15 16 17 18 19 20 21

interface PluginConfig {
  id: string
  name: string
  apply?: UniApp.PLATFORM | UniApp.PLATFORM[]
  config: {
    name: string
    main?: string
  }
}

fxy060608's avatar
fxy060608 已提交
22
export function initPluginUniOptions(UniVitePlugins: UniVitePlugin[]) {
fxy060608's avatar
fxy060608 已提交
23
  const assets: string[] = []
fxy060608's avatar
fxy060608 已提交
24
  const targets: UniViteCopyPluginTarget[] = []
fxy060608's avatar
fxy060608 已提交
25
  const transformEvent: Record<string, string> = Object.create(null)
fxy060608's avatar
fxy060608 已提交
26
  const compilerOptions: Required<UniVitePlugin>['uni']['compilerOptions'] = {}
fxy060608's avatar
fxy060608 已提交
27
  let compiler: TemplateCompiler | undefined
fxy060608's avatar
fxy060608 已提交
28 29
  UniVitePlugins.forEach((plugin) => {
    const {
fxy060608's avatar
fxy060608 已提交
30
      compiler: pluginTemplateCompiler,
fxy060608's avatar
fxy060608 已提交
31
      copyOptions: pluginCopyOptions,
fxy060608's avatar
fxy060608 已提交
32 33
      compilerOptions: pluginCompilerOptions,
    } = plugin.uni || {}
fxy060608's avatar
fxy060608 已提交
34 35 36
    if (pluginTemplateCompiler) {
      compiler = pluginTemplateCompiler
    }
fxy060608's avatar
fxy060608 已提交
37
    if (pluginCompilerOptions) {
fxy060608's avatar
fxy060608 已提交
38
      extend(compilerOptions, pluginCompilerOptions)
fxy060608's avatar
fxy060608 已提交
39
    }
fxy060608's avatar
fxy060608 已提交
40 41 42 43 44 45 46 47 48 49 50 51
    if (pluginCopyOptions) {
      let copyOptions = pluginCopyOptions as CopyOptions
      if (isFunction(pluginCopyOptions)) {
        copyOptions = pluginCopyOptions()
      }
      if (copyOptions.assets) {
        assets.push(...copyOptions.assets)
      }
      if (copyOptions.targets) {
        targets.push(...copyOptions.targets)
      }
    }
fxy060608's avatar
fxy060608 已提交
52 53
  })
  return {
fxy060608's avatar
fxy060608 已提交
54
    compiler,
fxy060608's avatar
fxy060608 已提交
55 56 57 58
    copyOptions: {
      assets,
      targets,
    },
fxy060608's avatar
fxy060608 已提交
59 60 61 62 63
    transformEvent,
    compilerOptions,
  }
}

fxy060608's avatar
fxy060608 已提交
64 65 66 67 68 69
export function initExtraPlugins(
  cliRoot: string,
  platform: UniApp.PLATFORM,
  options: VitePluginUniResolvedOptions
) {
  return initPlugins(resolvePlugins(cliRoot, platform), options)
fxy060608's avatar
fxy060608 已提交
70 71
}

fxy060608's avatar
fxy060608 已提交
72 73 74 75 76 77 78 79 80 81
function initPlugin(
  { id, config: { main } }: PluginConfig,
  options: VitePluginUniResolvedOptions
): Plugin | void {
  let plugin = require(path.join(id, main || '/lib/uni.plugin.js'))
  plugin = plugin.default || plugin
  if (isFunction(plugin)) {
    plugin = plugin(options)
  }
  return plugin
fxy060608's avatar
fxy060608 已提交
82 83
}

fxy060608's avatar
fxy060608 已提交
84 85 86 87
function initPlugins(
  plugins: PluginConfig[],
  options: VitePluginUniResolvedOptions
): Plugin[] {
fxy060608's avatar
fxy060608 已提交
88
  return plugins
fxy060608's avatar
fxy060608 已提交
89
    .map((plugin) => initPlugin(plugin, options))
fxy060608's avatar
fxy060608 已提交
90
    .flat()
fxy060608's avatar
fxy060608 已提交
91
    .filter<Plugin>(Boolean as any)
fxy060608's avatar
fxy060608 已提交
92 93 94 95 96 97
    .map((plugin) => {
      if (isFunction(plugin)) {
        return plugin(options)
      }
      return plugin
    })
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}

function resolvePlugins(cliRoot: string, platform: UniApp.PLATFORM) {
  const pkg = require(path.join(cliRoot, 'package.json'))
  return Object.keys(pkg.devDependencies || {})
    .concat(Object.keys(pkg.dependencies || {}))
    .map<PluginConfig | void>((id) => {
      try {
        const pluginPkg = require(id + '/package.json')
        const config = pluginPkg['uni-app'] as PluginConfig
        if (!config || !config.name) {
          return
        }
        const { apply } = config
        if (isArray(apply)) {
          if (!apply.includes(platform)) {
            return
          }
        } else if (isString(apply)) {
fxy060608's avatar
fxy060608 已提交
117
          if (!new RegExp(apply).test(platform)) {
fxy060608's avatar
fxy060608 已提交
118 119 120 121 122 123 124 125 126 127 128 129
            return
          }
        }
        return {
          id,
          name: config.name,
          config,
        }
      } catch (e) {}
    })
    .filter<PluginConfig>(Boolean as any)
}