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

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

9 10 11 12
import { VitePluginUniResolvedOptions } from '../..'
import { uniPrePlugin } from './pre'
import { uniJsonPlugin } from './json'
import { uniPreCssPlugin } from './preCss'
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 28

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

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

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

fxy060608's avatar
fxy060608 已提交
35
export function initPlugins(
fxy060608's avatar
fxy060608 已提交
36
  config: ResolvedConfig,
37 38
  options: VitePluginUniResolvedOptions
) {
fxy060608's avatar
fxy060608 已提交
39
  const plugins = config.plugins as Plugin[]
40

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

fxy060608's avatar
fxy060608 已提交
43 44
  addPlugin(
    plugins,
45
    uniPrePlugin(config, extend(uniPrePluginOptions, options)),
46
    0,
fxy060608's avatar
fxy060608 已提交
47
    'pre'
48
  )
fxy060608's avatar
fxy060608 已提交
49

fxy060608's avatar
fxy060608 已提交
50 51
  addPlugin(
    plugins,
52
    uniPreCssPlugin(config, extend(uniPreCssPluginOptions, options)),
fxy060608's avatar
fxy060608 已提交
53
    'vite:css'
54
  )
fxy060608's avatar
fxy060608 已提交
55 56
  addPlugin(plugins, uniPreVuePlugin(), 'vite:vue', 'pre')

fxy060608's avatar
fxy060608 已提交
57 58
  addPlugin(
    plugins,
fxy060608's avatar
fxy060608 已提交
59
    uniSSRPlugin(config, extend({ exclude: [...COMMON_EXCLUDE] }, options)),
fxy060608's avatar
fxy060608 已提交
60 61 62
    'vite:vue'
  )

fxy060608's avatar
fxy060608 已提交
63
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
fxy060608's avatar
fxy060608 已提交
64
  addPlugin(plugins, uniStaticPlugin(options, config), 'vite:asset', 'pre')
fxy060608's avatar
fxy060608 已提交
65

fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
  if (isInHBuilderX()) {
    try {
      require(path.resolve(
        process.env.UNI_HBUILDERX_PLUGINS,
        'uni_helpers/lib/bytenode'
      ))
      const { V } = require(path.join(
        process.env.UNI_HBUILDERX_PLUGINS,
        'uni_helpers'
      ))
      addPlugin(plugins, V({ dir: process.env.UNI_INPUT_DIR }), 0, 'pre')
    } catch (e) {}
  }

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

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