index.ts 2.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import debug from 'debug'
fxy060608's avatar
fxy060608 已提交
2
import { Plugin, ResolvedConfig, ViteDevServer } from 'vite'
3
import { Options as VueOptions } from '@vitejs/plugin-vue'
fxy060608's avatar
fxy060608 已提交
4 5
import { Options as ViteLegacyOptions } from '@vitejs/plugin-legacy'
import { VueJSXPluginOptions } from '@vue/babel-plugin-jsx'
6 7
import VueJsxPlugin from '@vitejs/plugin-vue-jsx'
import ViteLegacyPlugin from '@vitejs/plugin-legacy'
fxy060608's avatar
fxy060608 已提交
8

9 10 11
import { createConfig } from './config'
import { createConfigResolved } from './configResolved'
import { createConfigureServer } from './configureServer'
fxy060608's avatar
fxy060608 已提交
12
import { createHandleHotUpdate } from './handleHotUpdate'
fxy060608's avatar
fxy060608 已提交
13
import { initExtraPlugins } from './utils'
fxy060608's avatar
fxy060608 已提交
14
import { createTransformIndexHtml } from './transformIndexHtml'
fxy060608's avatar
fxy060608 已提交
15 16 17

const debugUni = debug('vite:uni:plugin')

18 19
export interface VitePluginUniOptions {
  inputDir?: string
fxy060608's avatar
fxy060608 已提交
20
  outputDir?: string
21
  vueOptions?: VueOptions
fxy060608's avatar
fxy060608 已提交
22 23
  vueJsxOptions?: VueJSXPluginOptions
  viteLegacyOptions?: ViteLegacyOptions
fxy060608's avatar
fxy060608 已提交
24
}
25
export interface VitePluginUniResolvedOptions extends VitePluginUniOptions {
fxy060608's avatar
fxy060608 已提交
26
  base: string
fxy060608's avatar
fxy060608 已提交
27
  command: ResolvedConfig['command']
fxy060608's avatar
fxy060608 已提交
28
  platform: UniApp.PLATFORM
29
  inputDir: string
fxy060608's avatar
fxy060608 已提交
30
  outputDir: string
fxy060608's avatar
fxy060608 已提交
31
  assetsDir: string
32
  devServer?: ViteDevServer
fxy060608's avatar
fxy060608 已提交
33
}
34

fxy060608's avatar
fxy060608 已提交
35
export * from './vue'
36

37 38 39 40 41 42 43 44 45 46
let createVueJsxPlugin: typeof VueJsxPlugin | undefined
try {
  createVueJsxPlugin = require('@vitejs/plugin-vue-jsx')
} catch (e) {}

let createViteLegacyPlugin: typeof ViteLegacyPlugin | undefined
try {
  createViteLegacyPlugin = require('@vitejs/plugin-legacy')
} catch (e) {}

47 48
export default function uniPlugin(
  rawOptions: VitePluginUniOptions = {}
49
): Plugin[] {
50 51
  const options: VitePluginUniResolvedOptions = {
    ...rawOptions,
fxy060608's avatar
fxy060608 已提交
52
    base: '/',
fxy060608's avatar
fxy060608 已提交
53
    assetsDir: 'assets',
fxy060608's avatar
fxy060608 已提交
54 55
    inputDir: '',
    outputDir: '',
fxy060608's avatar
fxy060608 已提交
56
    command: 'serve',
fxy060608's avatar
fxy060608 已提交
57
    platform: 'h5',
58
  }
59 60 61 62 63 64 65 66 67 68 69 70 71
  const plugins: Plugin[] = []

  if (createViteLegacyPlugin && options.viteLegacyOptions !== false) {
    plugins.push(
      ...(createViteLegacyPlugin(
        options.viteLegacyOptions
      ) as unknown as Plugin[])
    )
  }

  if (createVueJsxPlugin && options.vueJsxOptions !== false) {
    plugins.push(createVueJsxPlugin(options.vueJsxOptions))
  }
fxy060608's avatar
fxy060608 已提交
72 73 74 75 76
  const uniPlugins = initExtraPlugins(
    process.env.UNI_CLI_CONTEXT || process.cwd(),
    (process.env.UNI_PLATFORM as UniApp.PLATFORM) || 'h5'
  )
  debugUni(uniPlugins)
77
  plugins.push({
78
    name: 'vite:uni',
fxy060608's avatar
fxy060608 已提交
79
    config: createConfig(options, uniPlugins),
80 81
    configResolved: createConfigResolved(options),
    configureServer: createConfigureServer(options),
fxy060608's avatar
fxy060608 已提交
82
    handleHotUpdate: createHandleHotUpdate(options),
fxy060608's avatar
fxy060608 已提交
83
    transformIndexHtml: createTransformIndexHtml(options),
84
  })
fxy060608's avatar
fxy060608 已提交
85
  plugins.push(...uniPlugins)
86
  return plugins
87
}
fxy060608's avatar
fxy060608 已提交
88 89

export { uniInjectPlugin } from './configResolved/plugins/inject'