index.ts 4.9 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

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

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

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

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

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

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

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

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

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

  addPlugin(plugins, vue(options.vueOptions), 'vite:uni', 'pre')

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

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

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

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

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

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