index.ts 2.4 KB
Newer Older
1
import debug from 'debug'
fxy060608's avatar
fxy060608 已提交
2
import { extend } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
3
import { Plugin, ResolvedConfig } from 'vite'
fxy060608's avatar
fxy060608 已提交
4
import { FilterPattern } from '@rollup/pluginutils'
5

fxy060608's avatar
fxy060608 已提交
6
import { COMMON_EXCLUDE } from '@dcloudio/uni-cli-shared'
7

8 9 10 11
import { VitePluginUniResolvedOptions } from '../..'
import { uniPrePlugin } from './pre'
import { uniJsonPlugin } from './json'
import { uniPreCssPlugin } from './preCss'
fxy060608's avatar
fxy060608 已提交
12
import { uniEasycomPlugin } from './easycom'
fxy060608's avatar
fxy060608 已提交
13

fxy060608's avatar
fxy060608 已提交
14
import { uniStaticPlugin } from './static'
fxy060608's avatar
fxy060608 已提交
15
import { uniPreVuePlugin } from './preVue'
fxy060608's avatar
fxy060608 已提交
16
import { uniSSRPlugin } from './ssr'
fxy060608's avatar
fxy060608 已提交
17
import { uniResolveIdPlugin } from './resolveId'
fxy060608's avatar
fxy060608 已提交
18

fxy060608's avatar
fxy060608 已提交
19
const debugPlugin = debug('vite:uni:plugin')
20 21 22 23 24 25 26 27

export interface UniPluginFilterOptions extends VitePluginUniResolvedOptions {
  include?: FilterPattern
  exclude?: FilterPattern
}

const UNI_H5_RE = /@dcloudio\/uni-h5/

fxy060608's avatar
fxy060608 已提交
28 29
const APP_VUE_RE = /App.vue$/

30
const uniPrePluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
31
  exclude: [...COMMON_EXCLUDE, UNI_H5_RE],
32 33
}
const uniPreCssPluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
34
  exclude: [...COMMON_EXCLUDE, UNI_H5_RE],
35 36
}

fxy060608's avatar
fxy060608 已提交
37
const uniEasycomPluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
38
  exclude: [APP_VUE_RE, UNI_H5_RE],
fxy060608's avatar
fxy060608 已提交
39 40
}

fxy060608's avatar
fxy060608 已提交
41
export function initPlugins(
fxy060608's avatar
fxy060608 已提交
42
  config: ResolvedConfig,
43 44
  options: VitePluginUniResolvedOptions
) {
fxy060608's avatar
fxy060608 已提交
45
  const plugins = config.plugins as Plugin[]
46

fxy060608's avatar
fxy060608 已提交
47 48
  addPlugin(plugins, uniResolveIdPlugin(options), 'vite:resolve', 'pre')

fxy060608's avatar
fxy060608 已提交
49 50
  addPlugin(
    plugins,
51
    uniPrePlugin(extend(uniPrePluginOptions, options)),
52
    0,
fxy060608's avatar
fxy060608 已提交
53
    'pre'
54
  )
fxy060608's avatar
fxy060608 已提交
55

fxy060608's avatar
fxy060608 已提交
56 57
  addPlugin(
    plugins,
58
    uniPreCssPlugin(extend(uniPreCssPluginOptions, options)),
fxy060608's avatar
fxy060608 已提交
59
    'vite:css'
60
  )
fxy060608's avatar
fxy060608 已提交
61 62
  addPlugin(plugins, uniPreVuePlugin(), 'vite:vue', 'pre')

fxy060608's avatar
fxy060608 已提交
63 64
  addPlugin(
    plugins,
fxy060608's avatar
fxy060608 已提交
65
    uniSSRPlugin(config, extend({ exclude: [...COMMON_EXCLUDE] }, options)),
fxy060608's avatar
fxy060608 已提交
66 67 68
    'vite:vue'
  )

fxy060608's avatar
fxy060608 已提交
69 70
  addPlugin(
    plugins,
fxy060608's avatar
fxy060608 已提交
71
    uniEasycomPlugin(extend(uniEasycomPluginOptions, options), config),
fxy060608's avatar
fxy060608 已提交
72 73 74
    'vite:vue'
  )
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
fxy060608's avatar
fxy060608 已提交
75
  addPlugin(plugins, uniStaticPlugin(options, config), 'vite:asset', 'pre')
fxy060608's avatar
fxy060608 已提交
76

77
  if (process.env.DEBUG) {
fxy060608's avatar
fxy060608 已提交
78 79
    debugPlugin(plugins.length)
    debugPlugin(plugins.map((p) => (p as Plugin).name))
80 81
  }
}
fxy060608's avatar
fxy060608 已提交
82 83 84 85 86 87 88 89 90 91 92 93

function addPlugin(
  plugins: Plugin[],
  plugin: Plugin,
  index: string | number,
  type: 'pre' | 'post' = 'post'
) {
  if (typeof index === 'string') {
    index = plugins.findIndex((plugin) => (plugin as Plugin).name === index)
  }
  return plugins.splice(index + (type === 'pre' ? 0 : 1), 0, plugin)
}