index.ts 2.6 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'
10 11 12 13 14 15 16 17 18 19 20 21 22 23
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 已提交
24 25 26 27
    /\/@vue\//,
    /\/vue-router\//,
    /\/vuex\//,
    /@dcloudio\/uni-shared/,
fxy060608's avatar
fxy060608 已提交
28 29 30
    /vite\/preload-helper/,
    /vite\/dynamic-import-polyfill/,
    /\.html$/,
31 32 33 34 35 36 37
    UNI_H5_RE,
  ],
}
const uniPreCssPluginOptions: Partial<UniPluginFilterOptions> = {
  exclude: [UNI_H5_RE],
}

fxy060608's avatar
fxy060608 已提交
38
const uniEasycomPluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  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'],
54 55 56 57 58 59
}

export function resolvePlugins(
  config: UserConfig,
  options: VitePluginUniResolvedOptions
) {
fxy060608's avatar
fxy060608 已提交
60 61 62 63
  const plugins = config.plugins! as Plugin[]
  addPlugin(
    plugins,
    uniPrePlugin(Object.assign(uniPrePluginOptions, options)),
64
    0,
fxy060608's avatar
fxy060608 已提交
65
    'pre'
66
  )
fxy060608's avatar
fxy060608 已提交
67 68 69 70
  addPlugin(
    plugins,
    uniPreCssPlugin(Object.assign(uniPreCssPluginOptions, options)),
    'vite:css'
71
  )
fxy060608's avatar
fxy060608 已提交
72 73 74 75 76 77 78
  if (!options.devServer) {
    addPlugin(
      plugins,
      uniInjectPlugin(Object.assign(uniInjectPluginOptions, options)),
      'vite:vue'
    )
  }
fxy060608's avatar
fxy060608 已提交
79 80 81 82 83 84
  addPlugin(
    plugins,
    uniEasycomPlugin(Object.assign(uniEasycomPluginOptions, options)),
    'vite:vue'
  )
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
85
  if (process.env.DEBUG) {
fxy060608's avatar
fxy060608 已提交
86
    debugPlugin(config.plugins!.length)
87 88 89
    debugPlugin(config.plugins!.map((p) => (p as Plugin).name))
  }
}
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)
}