index.ts 2.7 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 { uniCopyPlugin } from './copy'
fxy060608's avatar
fxy060608 已提交
15
import { uniStaticPlugin } from './static'
fxy060608's avatar
fxy060608 已提交
16 17
import { uniRenderjsPlugin } from './renderjs'
import { uniPreVuePlugin } from './preVue'
fxy060608's avatar
fxy060608 已提交
18
import { uniSSRPlugin } from './ssr'
fxy060608's avatar
fxy060608 已提交
19
import { uniResolveIdPlugin } from './resolveId'
fxy060608's avatar
fxy060608 已提交
20

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

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

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

fxy060608's avatar
fxy060608 已提交
30 31
const APP_VUE_RE = /App.vue$/

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

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

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

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

fxy060608's avatar
fxy060608 已提交
52 53
  addPlugin(
    plugins,
54
    uniPrePlugin(extend(uniPrePluginOptions, options)),
55
    0,
fxy060608's avatar
fxy060608 已提交
56
    'pre'
57
  )
fxy060608's avatar
fxy060608 已提交
58

fxy060608's avatar
fxy060608 已提交
59 60
  addPlugin(
    plugins,
61
    uniPreCssPlugin(extend(uniPreCssPluginOptions, options)),
fxy060608's avatar
fxy060608 已提交
62
    'vite:css'
63
  )
fxy060608's avatar
fxy060608 已提交
64 65 66
  addPlugin(plugins, uniPreVuePlugin(), 'vite:vue', 'pre')
  addPlugin(plugins, uniRenderjsPlugin(), 'vite:vue')

fxy060608's avatar
fxy060608 已提交
67 68
  addPlugin(
    plugins,
fxy060608's avatar
fxy060608 已提交
69
    uniSSRPlugin(config, extend({ exclude: [...COMMON_EXCLUDE] }, options)),
fxy060608's avatar
fxy060608 已提交
70 71 72
    'vite:vue'
  )

fxy060608's avatar
fxy060608 已提交
73 74
  addPlugin(
    plugins,
fxy060608's avatar
fxy060608 已提交
75
    uniEasycomPlugin(extend(uniEasycomPluginOptions, options), config),
fxy060608's avatar
fxy060608 已提交
76 77 78
    'vite:vue'
  )
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
fxy060608's avatar
fxy060608 已提交
79
  addPlugin(plugins, uniStaticPlugin(options, config), 'vite:asset', 'pre')
fxy060608's avatar
fxy060608 已提交
80

fxy060608's avatar
fxy060608 已提交
81
  if (command === 'build' && !config.build.ssr) {
fxy060608's avatar
fxy060608 已提交
82 83
    addPlugin(plugins, uniCopyPlugin(options), plugins.length)
  }
fxy060608's avatar
fxy060608 已提交
84

85
  if (process.env.DEBUG) {
fxy060608's avatar
fxy060608 已提交
86 87
    debugPlugin(plugins.length)
    debugPlugin(plugins.map((p) => (p as Plugin).name))
88 89
  }
}
fxy060608's avatar
fxy060608 已提交
90 91 92 93 94 95 96 97 98 99 100 101

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)
}