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
import vue from '@vitejs/plugin-vue'

7
import { API_DEPS_CSS } from '@dcloudio/uni-cli-shared'
8

9 10 11 12
import { VitePluginUniResolvedOptions } from '../..'
import { uniPrePlugin } from './pre'
import { uniJsonPlugin } from './json'
import { uniPreCssPlugin } from './preCss'
fxy060608's avatar
fxy060608 已提交
13
import { uniEasycomPlugin } from './easycom'
fxy060608's avatar
fxy060608 已提交
14
import { InjectOptions, uniInjectPlugin } from './inject'
fxy060608's avatar
fxy060608 已提交
15 16 17 18

import { uniMainJsPlugin } from './mainJs'
import { uniPagesJsonPlugin } from './pagesJson'
import { uniManifestJsonPlugin } from './manifestJson'
fxy060608's avatar
fxy060608 已提交
19
import { uniPageVuePlugin } from './pageVue'
fxy060608's avatar
fxy060608 已提交
20
import { uniCopyPlugin } from './copy'
fxy060608's avatar
fxy060608 已提交
21
import { uniStaticPlugin } from './static'
fxy060608's avatar
fxy060608 已提交
22
import { uniCssScopedPlugin } from './cssScoped'
fxy060608's avatar
fxy060608 已提交
23 24
import { uniRenderjsPlugin } from './renderjs'
import { uniPreVuePlugin } from './preVue'
fxy060608's avatar
fxy060608 已提交
25
import { uniSSRPlugin } from './ssr'
fxy060608's avatar
fxy060608 已提交
26
import { uniResolveIdPlugin } from './resolveId'
fxy060608's avatar
fxy060608 已提交
27

fxy060608's avatar
fxy060608 已提交
28
const debugPlugin = debug('vite:uni:plugin')
29 30 31 32 33 34 35 36

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

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

fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44 45
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 已提交
46
  /@dcloudio\/uni-components\/style/,
fxy060608's avatar
fxy060608 已提交
47 48
]

fxy060608's avatar
fxy060608 已提交
49 50 51 52 53 54
const APP_VUE_RE = /App.vue$/

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

55
const uniPrePluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
56
  exclude: [...COMMON_EXCLUDE, UNI_H5_RE],
57 58 59 60 61
}
const uniPreCssPluginOptions: Partial<UniPluginFilterOptions> = {
  exclude: [UNI_H5_RE],
}

fxy060608's avatar
fxy060608 已提交
62
const uniEasycomPluginOptions: Partial<UniPluginFilterOptions> = {
fxy060608's avatar
fxy060608 已提交
63
  exclude: [APP_VUE_RE, UNI_H5_RE],
fxy060608's avatar
fxy060608 已提交
64 65 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
  callback(imports, mod) {
fxy060608's avatar
fxy060608 已提交
74
    const styles =
fxy060608's avatar
fxy060608 已提交
75
      mod[0] === '@dcloudio/uni-h5' &&
76
      API_DEPS_CSS[mod[1] as keyof typeof API_DEPS_CSS]
fxy060608's avatar
fxy060608 已提交
77
    if (!styles) {
fxy060608's avatar
fxy060608 已提交
78 79
      return
    }
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84
    styles.forEach((style) => {
      if (!imports.has(style)) {
        imports.set(style, `import '${style}';`)
      }
    })
fxy060608's avatar
fxy060608 已提交
85
  },
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[]
94 95
  addPlugin(plugins, vue(options.vueOptions), 'vite:uni', 'pre')

fxy060608's avatar
fxy060608 已提交
96 97
  addPlugin(plugins, uniResolveIdPlugin(options), 'vite:resolve', 'pre')

fxy060608's avatar
fxy060608 已提交
98 99 100 101
  if (options.platform === 'h5') {
    // h5平台需要为非App.vue组件自动增加scoped
    addPlugin(
      plugins,
102
      uniCssScopedPlugin(extend(uniCssScopedPluginOptions, options)),
fxy060608's avatar
fxy060608 已提交
103 104 105 106
      0,
      'pre'
    )
  }
fxy060608's avatar
fxy060608 已提交
107 108
  addPlugin(
    plugins,
109
    uniPrePlugin(extend(uniPrePluginOptions, options)),
110
    0,
fxy060608's avatar
fxy060608 已提交
111
    'pre'
112
  )
fxy060608's avatar
fxy060608 已提交
113
  addPlugin(plugins, uniMainJsPlugin(config, options), 1, 'pre')
fxy060608's avatar
fxy060608 已提交
114
  addPlugin(plugins, uniPagesJsonPlugin(config, options), 1, 'pre')
fxy060608's avatar
fxy060608 已提交
115
  addPlugin(plugins, uniManifestJsonPlugin(config, options), 1, 'pre')
fxy060608's avatar
fxy060608 已提交
116

fxy060608's avatar
fxy060608 已提交
117 118
  addPlugin(
    plugins,
119
    uniPreCssPlugin(extend(uniPreCssPluginOptions, options)),
fxy060608's avatar
fxy060608 已提交
120
    'vite:css'
121
  )
fxy060608's avatar
fxy060608 已提交
122 123 124
  addPlugin(plugins, uniPreVuePlugin(), 'vite:vue', 'pre')
  addPlugin(plugins, uniRenderjsPlugin(), 'vite:vue')

fxy060608's avatar
fxy060608 已提交
125
  // 可以考虑使用apply:'build'
fxy060608's avatar
fxy060608 已提交
126
  if (command === 'build') {
fxy060608's avatar
fxy060608 已提交
127
    addPlugin(plugins, uniInjectPlugin(uniInjectPluginOptions), 'vite:vue')
fxy060608's avatar
fxy060608 已提交
128
  }
fxy060608's avatar
fxy060608 已提交
129

fxy060608's avatar
fxy060608 已提交
130 131
  addPlugin(
    plugins,
fxy060608's avatar
fxy060608 已提交
132
    uniSSRPlugin(config, extend({ exclude: [...COMMON_EXCLUDE] }, options)),
fxy060608's avatar
fxy060608 已提交
133 134 135
    'vite:vue'
  )

fxy060608's avatar
fxy060608 已提交
136 137
  addPlugin(
    plugins,
138
    uniEasycomPlugin(extend(uniEasycomPluginOptions, options)),
fxy060608's avatar
fxy060608 已提交
139 140
    'vite:vue'
  )
fxy060608's avatar
fxy060608 已提交
141
  addPlugin(plugins, uniPageVuePlugin(options), 'vite:vue')
fxy060608's avatar
fxy060608 已提交
142
  addPlugin(plugins, uniJsonPlugin(options), 'vite:json', 'pre')
fxy060608's avatar
fxy060608 已提交
143
  addPlugin(plugins, uniStaticPlugin(options, config), 'vite:asset', 'pre')
fxy060608's avatar
fxy060608 已提交
144
  if (command === 'build' && !config.build.ssr) {
fxy060608's avatar
fxy060608 已提交
145 146
    addPlugin(plugins, uniCopyPlugin(options), plugins.length)
  }
fxy060608's avatar
fxy060608 已提交
147

148
  if (process.env.DEBUG) {
fxy060608's avatar
fxy060608 已提交
149 150
    debugPlugin(plugins.length)
    debugPlugin(plugins.map((p) => (p as Plugin).name))
151 152
  }
}
fxy060608's avatar
fxy060608 已提交
153 154 155 156 157 158 159 160 161 162 163 164

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