index.ts 3.3 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'
fxy060608's avatar
fxy060608 已提交
6 7 8
import vuePlugin from '@vitejs/plugin-vue'
import type VueJsxPlugin from '@vitejs/plugin-vue-jsx'
import type ViteLegacyPlugin from '@vitejs/plugin-legacy'
fxy060608's avatar
fxy060608 已提交
9

fxy060608's avatar
fxy060608 已提交
10 11 12 13 14
import {
  CopyOptions,
  initModuleAlias,
  initPreContext,
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
15

16 17 18
import { createConfig } from './config'
import { createConfigResolved } from './configResolved'
import { createConfigureServer } from './configureServer'
fxy060608's avatar
fxy060608 已提交
19 20 21 22 23 24
import { initExtraPlugins, initPluginUniOptions } from './utils'
import {
  initPluginViteLegacyOptions,
  initPluginVueJsxOptions,
  initPluginVueOptions,
} from './vue'
fxy060608's avatar
fxy060608 已提交
25
// import { createResolveId } from './resolveId'
fxy060608's avatar
fxy060608 已提交
26 27 28

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

fxy060608's avatar
fxy060608 已提交
29 30
const pkg = require('@dcloudio/vite-plugin-uni/package.json')

fxy060608's avatar
fxy060608 已提交
31 32
initModuleAlias()

fxy060608's avatar
fxy060608 已提交
33 34
process.env.UNI_COMPILER_VERSION = pkg['uni-app']?.['compilerVersion'] || ''

35 36
export interface VitePluginUniOptions {
  inputDir?: string
fxy060608's avatar
fxy060608 已提交
37
  outputDir?: string
38
  vueOptions?: VueOptions
fxy060608's avatar
fxy060608 已提交
39 40
  vueJsxOptions?: VueJSXPluginOptions
  viteLegacyOptions?: ViteLegacyOptions
fxy060608's avatar
fxy060608 已提交
41
}
42
export interface VitePluginUniResolvedOptions extends VitePluginUniOptions {
fxy060608's avatar
fxy060608 已提交
43
  base: string
fxy060608's avatar
fxy060608 已提交
44
  command: ResolvedConfig['command']
fxy060608's avatar
fxy060608 已提交
45
  platform: UniApp.PLATFORM
46
  inputDir: string
fxy060608's avatar
fxy060608 已提交
47
  outputDir: string
fxy060608's avatar
fxy060608 已提交
48
  assetsDir: string
49
  devServer?: ViteDevServer
fxy060608's avatar
fxy060608 已提交
50
  copyOptions?: Required<CopyOptions>
fxy060608's avatar
fxy060608 已提交
51
}
52

fxy060608's avatar
fxy060608 已提交
53
export { runDev, runBuild } from './cli/action'
54

55 56 57 58 59 60 61 62 63 64
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) {}

65 66
export default function uniPlugin(
  rawOptions: VitePluginUniOptions = {}
67
): Plugin[] {
68 69
  const options: VitePluginUniResolvedOptions = {
    ...rawOptions,
fxy060608's avatar
fxy060608 已提交
70
    base: '/',
fxy060608's avatar
fxy060608 已提交
71
    assetsDir: 'assets',
fxy060608's avatar
fxy060608 已提交
72 73
    inputDir: '',
    outputDir: '',
fxy060608's avatar
fxy060608 已提交
74
    command: 'serve',
fxy060608's avatar
fxy060608 已提交
75
    platform: 'h5',
76
  }
fxy060608's avatar
fxy060608 已提交
77 78 79 80 81 82

  options.platform = (process.env.UNI_PLATFORM as UniApp.PLATFORM) || 'h5'
  options.inputDir = process.env.UNI_INPUT_DIR

  initPreContext(options.platform)

83 84 85 86 87
  const plugins: Plugin[] = []

  if (createViteLegacyPlugin && options.viteLegacyOptions !== false) {
    plugins.push(
      ...(createViteLegacyPlugin(
fxy060608's avatar
fxy060608 已提交
88
        initPluginViteLegacyOptions(options)
89 90 91 92 93
      ) as unknown as Plugin[])
    )
  }

  if (createVueJsxPlugin && options.vueJsxOptions !== false) {
fxy060608's avatar
fxy060608 已提交
94
    plugins.push(createVueJsxPlugin(initPluginVueJsxOptions(options)))
95
  }
fxy060608's avatar
fxy060608 已提交
96

fxy060608's avatar
fxy060608 已提交
97 98 99 100 101
  const uniPlugins = initExtraPlugins(
    process.env.UNI_CLI_CONTEXT || process.cwd(),
    (process.env.UNI_PLATFORM as UniApp.PLATFORM) || 'h5'
  )
  debugUni(uniPlugins)
fxy060608's avatar
fxy060608 已提交
102 103 104 105 106

  const uniPluginOptions = initPluginUniOptions(uniPlugins)

  options.copyOptions = uniPluginOptions.copyOptions

107
  plugins.push({
108
    name: 'vite:uni',
fxy060608's avatar
fxy060608 已提交
109
    config: createConfig(options, uniPlugins),
fxy060608's avatar
fxy060608 已提交
110
    // resolveId: createResolveId(options),
111 112
    configResolved: createConfigResolved(options),
    configureServer: createConfigureServer(options),
113
  })
fxy060608's avatar
fxy060608 已提交
114
  plugins.push(...uniPlugins)
fxy060608's avatar
fxy060608 已提交
115

fxy060608's avatar
fxy060608 已提交
116 117 118
  plugins.unshift(
    vuePlugin(initPluginVueOptions(options, uniPlugins, uniPluginOptions))
  )
fxy060608's avatar
fxy060608 已提交
119

120
  return plugins
121
}