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
import vuePlugin from '@vitejs/plugin-vue'
import type ViteLegacyPlugin from '@vitejs/plugin-legacy'
fxy060608's avatar
fxy060608 已提交
8

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

15 16 17
import { createConfig } from './config'
import { createConfigResolved } from './configResolved'
import { createConfigureServer } from './configureServer'
fxy060608's avatar
fxy060608 已提交
18
import { uniCopyPlugin } from './plugins/copy'
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
  vueJsxOptions?: VueJSXPluginOptions | boolean
fxy060608's avatar
fxy060608 已提交
40
  viteLegacyOptions?: ViteLegacyOptions | false
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
let createViteLegacyPlugin: typeof ViteLegacyPlugin | undefined
try {
  createViteLegacyPlugin = require('@vitejs/plugin-legacy')
} catch (e) {}

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

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

  initPreContext(options.platform)

78 79
  const plugins: Plugin[] = []

fxy060608's avatar
fxy060608 已提交
80
  if (createViteLegacyPlugin && options.viteLegacyOptions) {
81 82
    plugins.push(
      ...(createViteLegacyPlugin(
fxy060608's avatar
fxy060608 已提交
83
        initPluginViteLegacyOptions(options)
84 85 86 87
      ) as unknown as Plugin[])
    )
  }

fxy060608's avatar
fxy060608 已提交
88 89 90 91
  if (options.vueJsxOptions) {
    plugins.push(
      require('../lib/plugin-vue-jsx/index')(initPluginVueJsxOptions(options))
    )
92
  }
fxy060608's avatar
fxy060608 已提交
93

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

  const uniPluginOptions = initPluginUniOptions(uniPlugins)

  options.copyOptions = uniPluginOptions.copyOptions

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

fxy060608's avatar
fxy060608 已提交
113 114 115
  plugins.unshift(
    vuePlugin(initPluginVueOptions(options, uniPlugins, uniPluginOptions))
  )
fxy060608's avatar
fxy060608 已提交
116

fxy060608's avatar
fxy060608 已提交
117 118 119 120 121 122 123
  plugins.push(
    uniCopyPlugin({
      outputDir: process.env.UNI_OUTPUT_DIR,
      copyOptions: options.copyOptions,
    })
  )

124
  return plugins
125
}