index.ts 4.7 KB
Newer Older
1
import debug from 'debug'
fxy060608's avatar
fxy060608 已提交
2
import { extend } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
3
import { Plugin, ResolvedConfig } from 'vite'
fxy060608's avatar
fxy060608 已提交
4
import { FilterPattern } from '@rollup/pluginutils'
5 6 7 8
import { VitePluginUniResolvedOptions } from '../..'
import { uniPrePlugin } from './pre'
import { uniJsonPlugin } from './json'
import { uniPreCssPlugin } from './preCss'
fxy060608's avatar
fxy060608 已提交
9
import { uniEasycomPlugin } from './easycom'
fxy060608's avatar
fxy060608 已提交
10
import { InjectOptions, uniInjectPlugin } from './inject'
fxy060608's avatar
fxy060608 已提交
11 12 13 14

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
import { uniStaticPlugin } from './static'
fxy060608's avatar
fxy060608 已提交
18
import { uniCssScopedPlugin } from './cssScoped'
fxy060608's avatar
fxy060608 已提交
19 20
import { uniRenderjsPlugin } from './renderjs'
import { uniPreVuePlugin } from './preVue'
fxy060608's avatar
fxy060608 已提交
21

fxy060608's avatar
fxy060608 已提交
22
const debugPlugin = debug('vite:uni:plugin')
23 24 25 26 27 28 29 30

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

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

fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36 37 38 39
const COMMON_EXCLUDE = [
  /pages\.json\.js$/,
  /manifest\.json\.js$/,
  /vite\//,
  /\/@vue\//,
  /\/vue-router\//,
  /\/vuex\//,
  /@dcloudio\/uni-h5-vue/,
  /@dcloudio\/uni-shared/,
fxy060608's avatar
fxy060608 已提交
40
  /@dcloudio\/uni-components\/style/,
fxy060608's avatar
fxy060608 已提交
41 42
]

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

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

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

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

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

fxy060608's avatar
fxy060608 已提交
66
const uniInjectPluginOptions: Partial<InjectOptions> = {
fxy060608's avatar
fxy060608 已提交
67
  exclude: [...COMMON_EXCLUDE],
fxy060608's avatar
fxy060608 已提交
68 69 70 71
  'uni.': '@dcloudio/uni-h5',
  getApp: ['@dcloudio/uni-h5', 'getApp'],
  getCurrentPages: ['@dcloudio/uni-h5', 'getCurrentPages'],
  UniServiceJSBridge: ['@dcloudio/uni-h5', 'UniServiceJSBridge'],
fxy060608's avatar
fxy060608 已提交
72
  UniViewJSBridge: ['@dcloudio/uni-h5', 'UniViewJSBridge'],
fxy060608's avatar
fxy060608 已提交
73 74 75 76 77 78 79 80 81 82 83 84
  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';`)
    }
  },
85 86
}

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

fxy060608's avatar
fxy060608 已提交
112 113
  addPlugin(
    plugins,
114
    uniPreCssPlugin(extend(uniPreCssPluginOptions, options)),
fxy060608's avatar
fxy060608 已提交
115
    'vite:css'
116
  )
fxy060608's avatar
fxy060608 已提交
117 118 119
  addPlugin(plugins, uniPreVuePlugin(), 'vite:vue', 'pre')
  addPlugin(plugins, uniRenderjsPlugin(), 'vite:vue')

fxy060608's avatar
fxy060608 已提交
120 121
  const injectOptions = options.compiler.inject()
  // 可以考虑使用apply:'build'
fxy060608's avatar
fxy060608 已提交
122
  if (command === 'build') {
fxy060608's avatar
fxy060608 已提交
123 124
    addPlugin(
      plugins,
125
      uniInjectPlugin(extend(uniInjectPluginOptions, injectOptions)),
fxy060608's avatar
fxy060608 已提交
126 127
      'vite:vue'
    )
fxy060608's avatar
fxy060608 已提交
128 129 130 131 132
  } else {
    if (injectOptions && Object.keys(injectOptions).length) {
      addPlugin(
        plugins,
        uniInjectPlugin(
133
          extend({ exclude: [...COMMON_EXCLUDE] }, injectOptions)
fxy060608's avatar
fxy060608 已提交
134 135 136 137
        ),
        'vite:vue'
      )
    }
fxy060608's avatar
fxy060608 已提交
138
  }
fxy060608's avatar
fxy060608 已提交
139

fxy060608's avatar
fxy060608 已提交
140 141
  addPlugin(
    plugins,
142
    uniEasycomPlugin(extend(uniEasycomPluginOptions, options)),
fxy060608's avatar
fxy060608 已提交
143 144
    'vite:vue'
  )
fxy060608's avatar
fxy060608 已提交
145
  addPlugin(plugins, uniPageVuePlugin(options), 'vite:vue')
fxy060608's avatar
fxy060608 已提交
146
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
fxy060608's avatar
fxy060608 已提交
147
  addPlugin(plugins, uniStaticPlugin(options, config), 'vite:asset', 'pre')
fxy060608's avatar
fxy060608 已提交
148
  if (command === 'build' && !config.build.ssr) {
fxy060608's avatar
fxy060608 已提交
149 150
    addPlugin(plugins, uniCopyPlugin(options), plugins.length)
  }
151
  if (process.env.DEBUG) {
fxy060608's avatar
fxy060608 已提交
152 153
    debugPlugin(plugins.length)
    debugPlugin(plugins.map((p) => (p as Plugin).name))
154 155
  }
}
fxy060608's avatar
fxy060608 已提交
156 157 158 159 160 161 162 163 164 165 166 167

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