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

fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63
const API_STYLES = {
  showModal: 'modal',
  showToast: 'toast',
  showActionSheet: 'action-sheet',
}

fxy060608's avatar
fxy060608 已提交
64
const uniInjectPluginOptions: Partial<InjectOptions> = {
fxy060608's avatar
fxy060608 已提交
65
  exclude: [...COMMON_EXCLUDE],
fxy060608's avatar
fxy060608 已提交
66 67 68 69
  'uni.': '@dcloudio/uni-h5',
  getApp: ['@dcloudio/uni-h5', 'getApp'],
  getCurrentPages: ['@dcloudio/uni-h5', 'getCurrentPages'],
  UniServiceJSBridge: ['@dcloudio/uni-h5', 'UniServiceJSBridge'],
fxy060608's avatar
fxy060608 已提交
70
  UniViewJSBridge: ['@dcloudio/uni-h5', 'UniViewJSBridge'],
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76 77 78 79 80 81 82
  callback(imports, mod) {
    const style =
      mod[0] === '@dcloudio/uni-h5' &&
      API_STYLES[mod[1] as keyof typeof API_STYLES]
    if (!style) {
      return
    }
    const hash = `${mod[0]}.${mod[1]}`
    if (!imports.has(hash)) {
      imports.set(hash, `import '@dcloudio/uni-h5/style/api/${style}.css';`)
    }
  },
83 84 85
}

export function resolvePlugins(
fxy060608's avatar
fxy060608 已提交
86
  config: ResolvedConfig,
87 88
  options: VitePluginUniResolvedOptions
) {
fxy060608's avatar
fxy060608 已提交
89 90
  const command = config.command
  const plugins = config.plugins as Plugin[]
fxy060608's avatar
fxy060608 已提交
91 92 93 94 95 96 97 98 99
  if (options.platform === 'h5') {
    // h5平台需要为非App.vue组件自动增加scoped
    addPlugin(
      plugins,
      uniCssScopedPlugin(Object.assign(uniCssScopedPluginOptions, options)),
      0,
      'pre'
    )
  }
fxy060608's avatar
fxy060608 已提交
100 101 102
  addPlugin(
    plugins,
    uniPrePlugin(Object.assign(uniPrePluginOptions, options)),
103
    0,
fxy060608's avatar
fxy060608 已提交
104
    'pre'
105
  )
fxy060608's avatar
fxy060608 已提交
106
  addPlugin(plugins, uniMainJsPlugin(options), 1, 'pre')
fxy060608's avatar
fxy060608 已提交
107
  addPlugin(plugins, uniPagesJsonPlugin(config, options), 1, 'pre')
fxy060608's avatar
fxy060608 已提交
108
  addPlugin(plugins, uniManifestJsonPlugin(config, options), 1, 'pre')
fxy060608's avatar
fxy060608 已提交
109

fxy060608's avatar
fxy060608 已提交
110 111 112 113
  addPlugin(
    plugins,
    uniPreCssPlugin(Object.assign(uniPreCssPluginOptions, options)),
    'vite:css'
114
  )
fxy060608's avatar
fxy060608 已提交
115
  if (command === 'build') {
fxy060608's avatar
fxy060608 已提交
116 117 118 119 120 121
    addPlugin(
      plugins,
      uniInjectPlugin(Object.assign(uniInjectPluginOptions, options)),
      'vite:vue'
    )
  }
fxy060608's avatar
fxy060608 已提交
122 123 124 125 126
  addPlugin(
    plugins,
    uniEasycomPlugin(Object.assign(uniEasycomPluginOptions, options)),
    'vite:vue'
  )
fxy060608's avatar
fxy060608 已提交
127
  addPlugin(plugins, uniPageVuePlugin(options), 'vite:vue')
fxy060608's avatar
fxy060608 已提交
128
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
fxy060608's avatar
fxy060608 已提交
129
  addPlugin(plugins, uniStaticPlugin(options, config), 'vite:asset', 'pre')
fxy060608's avatar
fxy060608 已提交
130 131 132
  if (command === 'build') {
    addPlugin(plugins, uniCopyPlugin(options), plugins.length)
  }
133
  if (process.env.DEBUG) {
fxy060608's avatar
fxy060608 已提交
134 135
    debugPlugin(plugins.length)
    debugPlugin(plugins.map((p) => (p as Plugin).name))
136 137
  }
}
fxy060608's avatar
fxy060608 已提交
138 139 140 141 142 143 144 145 146 147 148 149

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