index.ts 4.8 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
import { uniSSRPlugin } from './ssr'
fxy060608's avatar
fxy060608 已提交
22

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

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

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

fxy060608's avatar
fxy060608 已提交
32 33 34 35 36 37 38 39 40
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 已提交
41
  /@dcloudio\/uni-components\/style/,
fxy060608's avatar
fxy060608 已提交
42 43
]

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

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

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

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

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

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

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

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

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

fxy060608's avatar
fxy060608 已提交
141 142 143 144 145 146
  addPlugin(
    plugins,
    uniSSRPlugin(extend({ exclude: [...COMMON_EXCLUDE] }, options)),
    'vite:vue'
  )

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

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