index.ts 3.1 KB
Newer Older
1 2
import { FilterPattern } from '@rollup/pluginutils'
import debug from 'debug'
fxy060608's avatar
fxy060608 已提交
3
import { UserConfig, Plugin } from 'vite'
4 5 6 7
import { VitePluginUniResolvedOptions } from '../..'
import { uniPrePlugin } from './pre'
import { uniJsonPlugin } from './json'
import { uniPreCssPlugin } from './preCss'
fxy060608's avatar
fxy060608 已提交
8
import { uniEasycomPlugin } from './easycom'
fxy060608's avatar
fxy060608 已提交
9
import { InjectOptions, uniInjectPlugin } from './inject'
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15

import { uniAppVuePlugin } from './appVue'
import { uniMainJsPlugin } from './mainJs'
import { uniPagesJsonPlugin } from './pagesJson'
import { uniManifestJsonPlugin } from './manifestJson'

16 17 18 19 20 21 22 23 24 25 26 27 28 29
const debugPlugin = debug('uni:plugin')

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

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

const uniPrePluginOptions: Partial<UniPluginFilterOptions> = {
  exclude: [
    /pages\.json\.js$/,
    /vite\/dist\/client\/client\.js$/,
    /vue&type=/,
fxy060608's avatar
fxy060608 已提交
30 31 32 33
    /\/@vue\//,
    /\/vue-router\//,
    /\/vuex\//,
    /@dcloudio\/uni-shared/,
fxy060608's avatar
fxy060608 已提交
34 35 36
    /vite\/preload-helper/,
    /vite\/dynamic-import-polyfill/,
    /\.html$/,
37 38 39 40 41 42 43
    UNI_H5_RE,
  ],
}
const uniPreCssPluginOptions: Partial<UniPluginFilterOptions> = {
  exclude: [UNI_H5_RE],
}

fxy060608's avatar
fxy060608 已提交
44
const uniEasycomPluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  exclude: [/App.vue$/, UNI_H5_RE],
}

const uniInjectPluginOptions: Partial<InjectOptions> = {
  exclude: [
    /@dcloudio\/uni-h5-vue/,
    /@dcloudio\/uni-shared/,
    /\/vue-router\//,
    /@vue\/shared/,
  ],
  '__GLOBAL__.': '@dcloudio/uni-h5',
  'uni.': '@dcloudio/uni-h5',
  getApp: ['@dcloudio/uni-h5', 'getApp'],
  getCurrentPages: ['@dcloudio/uni-h5', 'getCurrentPages'],
  UniServiceJSBridge: ['@dcloudio/uni-h5', 'UniServiceJSBridge'],
60 61 62 63 64 65
}

export function resolvePlugins(
  config: UserConfig,
  options: VitePluginUniResolvedOptions
) {
fxy060608's avatar
fxy060608 已提交
66 67 68 69
  const plugins = config.plugins! as Plugin[]
  addPlugin(
    plugins,
    uniPrePlugin(Object.assign(uniPrePluginOptions, options)),
70
    0,
fxy060608's avatar
fxy060608 已提交
71
    'pre'
72
  )
fxy060608's avatar
fxy060608 已提交
73 74 75 76 77
  addPlugin(plugins, uniAppVuePlugin(options), 1, 'pre')
  addPlugin(plugins, uniMainJsPlugin(options), 1, 'pre')
  addPlugin(plugins, uniPagesJsonPlugin(options), 1, 'pre')
  addPlugin(plugins, uniManifestJsonPlugin(options), 1, 'pre')

fxy060608's avatar
fxy060608 已提交
78 79 80 81
  addPlugin(
    plugins,
    uniPreCssPlugin(Object.assign(uniPreCssPluginOptions, options)),
    'vite:css'
82
  )
fxy060608's avatar
fxy060608 已提交
83 84 85 86 87 88 89
  if (!options.devServer) {
    addPlugin(
      plugins,
      uniInjectPlugin(Object.assign(uniInjectPluginOptions, options)),
      'vite:vue'
    )
  }
fxy060608's avatar
fxy060608 已提交
90 91 92 93 94 95
  addPlugin(
    plugins,
    uniEasycomPlugin(Object.assign(uniEasycomPluginOptions, options)),
    'vite:vue'
  )
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
96
  if (process.env.DEBUG) {
fxy060608's avatar
fxy060608 已提交
97
    debugPlugin(config.plugins!.length)
98 99 100
    debugPlugin(config.plugins!.map((p) => (p as Plugin).name))
  }
}
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105 106 107 108 109 110 111 112

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