index.ts 3.7 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

import { uniMainJsPlugin } from './mainJs'
import { uniPagesJsonPlugin } from './pagesJson'
import { uniManifestJsonPlugin } from './manifestJson'
fxy060608's avatar
fxy060608 已提交
14
import { uniPageVuePlugin } from './pageVue'
fxy060608's avatar
fxy060608 已提交
15
import { uniCopyPlugin } from './copy'
fxy060608's avatar
fxy060608 已提交
16
import { uniStaticPlugin } from './static'
fxy060608's avatar
fxy060608 已提交
17
import { uniCssScopedPlugin } from './cssScoped'
fxy060608's avatar
fxy060608 已提交
18

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

fxy060608's avatar
fxy060608 已提交
41 42 43 44 45 46
const APP_VUE_RE = /App.vue$/

const uniCssScopedPluginOptions: Partial<UniPluginFilterOptions> = {
  exclude: [APP_VUE_RE],
}

47
const uniPrePluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
48
  exclude: [...COMMON_EXCLUDE, UNI_H5_RE],
49 50 51 52 53
}
const uniPreCssPluginOptions: Partial<UniPluginFilterOptions> = {
  exclude: [UNI_H5_RE],
}

fxy060608's avatar
fxy060608 已提交
54
const uniEasycomPluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
55
  exclude: [APP_VUE_RE, UNI_H5_RE],
fxy060608's avatar
fxy060608 已提交
56 57 58
}

const uniInjectPluginOptions: Partial<InjectOptions> = {
fxy060608's avatar
fxy060608 已提交
59
  exclude: [...COMMON_EXCLUDE],
fxy060608's avatar
fxy060608 已提交
60 61 62 63
  'uni.': '@dcloudio/uni-h5',
  getApp: ['@dcloudio/uni-h5', 'getApp'],
  getCurrentPages: ['@dcloudio/uni-h5', 'getCurrentPages'],
  UniServiceJSBridge: ['@dcloudio/uni-h5', 'UniServiceJSBridge'],
fxy060608's avatar
fxy060608 已提交
64
  UniViewJSBridge: ['@dcloudio/uni-h5', 'UniViewJSBridge'],
65 66 67
}

export function resolvePlugins(
fxy060608's avatar
fxy060608 已提交
68
  config: ResolvedConfig,
69 70
  options: VitePluginUniResolvedOptions
) {
fxy060608's avatar
fxy060608 已提交
71 72
  const command = config.command
  const plugins = config.plugins as Plugin[]
fxy060608's avatar
fxy060608 已提交
73 74 75 76 77 78 79 80 81
  if (options.platform === 'h5') {
    // h5平台需要为非App.vue组件自动增加scoped
    addPlugin(
      plugins,
      uniCssScopedPlugin(Object.assign(uniCssScopedPluginOptions, options)),
      0,
      'pre'
    )
  }
fxy060608's avatar
fxy060608 已提交
82 83 84
  addPlugin(
    plugins,
    uniPrePlugin(Object.assign(uniPrePluginOptions, options)),
85
    0,
fxy060608's avatar
fxy060608 已提交
86
    'pre'
87
  )
fxy060608's avatar
fxy060608 已提交
88
  addPlugin(plugins, uniMainJsPlugin(options), 1, 'pre')
fxy060608's avatar
fxy060608 已提交
89
  addPlugin(plugins, uniPagesJsonPlugin(config, options), 1, 'pre')
fxy060608's avatar
fxy060608 已提交
90
  addPlugin(plugins, uniManifestJsonPlugin(config, options), 1, 'pre')
fxy060608's avatar
fxy060608 已提交
91

fxy060608's avatar
fxy060608 已提交
92 93 94 95
  addPlugin(
    plugins,
    uniPreCssPlugin(Object.assign(uniPreCssPluginOptions, options)),
    'vite:css'
96
  )
fxy060608's avatar
fxy060608 已提交
97
  if (command === 'build') {
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102 103
    addPlugin(
      plugins,
      uniInjectPlugin(Object.assign(uniInjectPluginOptions, options)),
      'vite:vue'
    )
  }
fxy060608's avatar
fxy060608 已提交
104 105 106 107 108
  addPlugin(
    plugins,
    uniEasycomPlugin(Object.assign(uniEasycomPluginOptions, options)),
    'vite:vue'
  )
fxy060608's avatar
fxy060608 已提交
109
  addPlugin(plugins, uniPageVuePlugin(options), 'vite:vue')
fxy060608's avatar
fxy060608 已提交
110
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
fxy060608's avatar
fxy060608 已提交
111
  addPlugin(plugins, uniStaticPlugin(options, config), 'vite:asset', 'pre')
fxy060608's avatar
fxy060608 已提交
112 113 114
  if (command === 'build') {
    addPlugin(plugins, uniCopyPlugin(options), plugins.length)
  }
115
  if (process.env.DEBUG) {
fxy060608's avatar
fxy060608 已提交
116 117
    debugPlugin(plugins.length)
    debugPlugin(plugins.map((p) => (p as Plugin).name))
118 119
  }
}
fxy060608's avatar
fxy060608 已提交
120 121 122 123 124 125 126 127 128 129 130 131

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