index.ts 3.1 KB
Newer Older
1 2
import { FilterPattern } from '@rollup/pluginutils'
import debug from 'debug'
fxy060608's avatar
fxy060608 已提交
3
import { Plugin, ResolvedConfig } 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

import { uniAppVuePlugin } from './appVue'
import { uniMainJsPlugin } from './mainJs'
import { uniPagesJsonPlugin } from './pagesJson'
import { uniManifestJsonPlugin } from './manifestJson'
fxy060608's avatar
fxy060608 已提交
15
import { uniPageVuePlugin } from './pageVue'
fxy060608's avatar
fxy060608 已提交
16

17 18 19 20 21 22 23 24 25
const debugPlugin = debug('uni:plugin')

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

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

fxy060608's avatar
fxy060608 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38
const COMMON_EXCLUDE = [
  /pages\.json\.js$/,
  /manifest\.json\.js$/,
  /vue&type=/,
  /vite\//,
  /\/@vue\//,
  /\/vue-router\//,
  /\/vuex\//,
  /@dcloudio\/uni-h5-vue/,
  /@dcloudio\/uni-shared/,
  /\.html$/,
]

39
const uniPrePluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
40
  exclude: [...COMMON_EXCLUDE, UNI_H5_RE],
41 42 43 44 45
}
const uniPreCssPluginOptions: Partial<UniPluginFilterOptions> = {
  exclude: [UNI_H5_RE],
}

fxy060608's avatar
fxy060608 已提交
46
const uniEasycomPluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
47 48 49 50
  exclude: [/App.vue$/, UNI_H5_RE],
}

const uniInjectPluginOptions: Partial<InjectOptions> = {
fxy060608's avatar
fxy060608 已提交
51
  exclude: [...COMMON_EXCLUDE],
fxy060608's avatar
fxy060608 已提交
52 53 54 55 56
  '__GLOBAL__.': '@dcloudio/uni-h5',
  'uni.': '@dcloudio/uni-h5',
  getApp: ['@dcloudio/uni-h5', 'getApp'],
  getCurrentPages: ['@dcloudio/uni-h5', 'getCurrentPages'],
  UniServiceJSBridge: ['@dcloudio/uni-h5', 'UniServiceJSBridge'],
57 58 59
}

export function resolvePlugins(
fxy060608's avatar
fxy060608 已提交
60 61
  command: ResolvedConfig['command'],
  plugins: Plugin[],
62 63
  options: VitePluginUniResolvedOptions
) {
fxy060608's avatar
fxy060608 已提交
64 65 66
  addPlugin(
    plugins,
    uniPrePlugin(Object.assign(uniPrePluginOptions, options)),
67
    0,
fxy060608's avatar
fxy060608 已提交
68
    'pre'
69
  )
fxy060608's avatar
fxy060608 已提交
70 71 72 73 74
  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 已提交
75 76 77 78
  addPlugin(
    plugins,
    uniPreCssPlugin(Object.assign(uniPreCssPluginOptions, options)),
    'vite:css'
79
  )
fxy060608's avatar
fxy060608 已提交
80
  if (command === 'build') {
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86
    addPlugin(
      plugins,
      uniInjectPlugin(Object.assign(uniInjectPluginOptions, options)),
      'vite:vue'
    )
  }
fxy060608's avatar
fxy060608 已提交
87 88 89 90 91
  addPlugin(
    plugins,
    uniEasycomPlugin(Object.assign(uniEasycomPluginOptions, options)),
    'vite:vue'
  )
fxy060608's avatar
fxy060608 已提交
92
  addPlugin(plugins, uniPageVuePlugin({ command }), 'vite:vue')
fxy060608's avatar
fxy060608 已提交
93
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
94
  if (process.env.DEBUG) {
fxy060608's avatar
fxy060608 已提交
95 96
    debugPlugin(plugins.length)
    debugPlugin(plugins.map((p) => (p as Plugin).name))
97 98
  }
}
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103 104 105 106 107 108 109 110

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