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'
fxy060608's avatar
fxy060608 已提交
5
import { API_STYLES } from '@dcloudio/uni-cli-shared'
6 7 8 9
import { VitePluginUniResolvedOptions } from '../..'
import { uniPrePlugin } from './pre'
import { uniJsonPlugin } from './json'
import { uniPreCssPlugin } from './preCss'
fxy060608's avatar
fxy060608 已提交
10
import { uniEasycomPlugin } from './easycom'
fxy060608's avatar
fxy060608 已提交
11
import { InjectOptions, uniInjectPlugin } from './inject'
fxy060608's avatar
fxy060608 已提交
12 13 14 15

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

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

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

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

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

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

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

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

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

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

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

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

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

fxy060608's avatar
fxy060608 已提交
137 138
  addPlugin(
    plugins,
fxy060608's avatar
fxy060608 已提交
139
    uniSSRPlugin(config, extend({ exclude: [...COMMON_EXCLUDE] }, options)),
fxy060608's avatar
fxy060608 已提交
140 141 142
    'vite:vue'
  )

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

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