index.ts 3.2 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
import { uniCopyPlugin } from './copy'
fxy060608's avatar
fxy060608 已提交
17

18 19 20 21 22 23 24 25 26
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 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
const COMMON_EXCLUDE = [
  /pages\.json\.js$/,
  /manifest\.json\.js$/,
  /vue&type=/,
  /vite\//,
  /\/@vue\//,
  /\/vue-router\//,
  /\/vuex\//,
  /@dcloudio\/uni-h5-vue/,
  /@dcloudio\/uni-shared/,
  /\.html$/,
]

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

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

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

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

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

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