index.ts 2.6 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 { initExtraPlugins } from './utils'
fxy060608's avatar
fxy060608 已提交
13
import { createTransformIndexHtml } from './transformIndexHtml'
fxy060608's avatar
fxy060608 已提交
14 15 16

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

fxy060608's avatar
fxy060608 已提交
17 18 19 20
const pkg = require('@dcloudio/vite-plugin-uni/package.json')

process.env.UNI_COMPILER_VERSION = pkg['uni-app']?.['compilerVersion'] || ''

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

fxy060608's avatar
fxy060608 已提交
38
export * from './vue'
39

40 41 42 43 44 45 46 47 48 49
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) {}

50 51
export default function uniPlugin(
  rawOptions: VitePluginUniOptions = {}
52
): Plugin[] {
53 54
  const options: VitePluginUniResolvedOptions = {
    ...rawOptions,
fxy060608's avatar
fxy060608 已提交
55
    base: '/',
fxy060608's avatar
fxy060608 已提交
56
    assetsDir: 'assets',
fxy060608's avatar
fxy060608 已提交
57 58
    inputDir: '',
    outputDir: '',
fxy060608's avatar
fxy060608 已提交
59
    command: 'serve',
fxy060608's avatar
fxy060608 已提交
60
    platform: 'h5',
61
  }
62 63 64 65 66 67 68 69 70 71 72 73 74
  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 已提交
75 76 77 78 79
  const uniPlugins = initExtraPlugins(
    process.env.UNI_CLI_CONTEXT || process.cwd(),
    (process.env.UNI_PLATFORM as UniApp.PLATFORM) || 'h5'
  )
  debugUni(uniPlugins)
80
  plugins.push({
81
    name: 'vite:uni',
fxy060608's avatar
fxy060608 已提交
82
    config: createConfig(options, uniPlugins),
83 84
    configResolved: createConfigResolved(options),
    configureServer: createConfigureServer(options),
fxy060608's avatar
fxy060608 已提交
85
    transformIndexHtml: createTransformIndexHtml(options),
86
  })
fxy060608's avatar
fxy060608 已提交
87
  plugins.push(...uniPlugins)
88
  return plugins
89
}