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

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

17 18
import { createConfig } from './config'
import { createConfigResolved } from './configResolved'
fxy060608's avatar
fxy060608 已提交
19
import { uniCopyPlugin } from './plugins/copy'
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24
import {
  initExtraPlugins,
  initPluginUniOptions,
  rewriteCompilerSfcParse,
} from './utils'
fxy060608's avatar
fxy060608 已提交
25 26 27 28 29
import {
  initPluginViteLegacyOptions,
  initPluginVueJsxOptions,
  initPluginVueOptions,
} from './vue'
fxy060608's avatar
fxy060608 已提交
30
// import { createResolveId } from './resolveId'
fxy060608's avatar
fxy060608 已提交
31 32 33

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

fxy060608's avatar
fxy060608 已提交
34
const pkg = require(path.resolve(__dirname, '../package.json'))
fxy060608's avatar
fxy060608 已提交
35

fxy060608's avatar
fxy060608 已提交
36 37
initModuleAlias()

fxy060608's avatar
fxy060608 已提交
38 39
rewriteCompilerSfcParse()

fxy060608's avatar
fxy060608 已提交
40
process.env.UNI_COMPILER_VERSION = pkg['uni-app']?.['compilerVersion'] || ''
fxy060608's avatar
fxy060608 已提交
41 42 43
process.env.UNI_COMPILER_VERSION_TYPE = pkg.version.includes('alpha')
  ? 'a'
  : 'r'
fxy060608's avatar
fxy060608 已提交
44

45 46
export interface VitePluginUniOptions {
  inputDir?: string
fxy060608's avatar
fxy060608 已提交
47
  outputDir?: string
48
  vueOptions?: VueOptions
fxy060608's avatar
fxy060608 已提交
49
  vueJsxOptions?: VueJSXPluginOptions | boolean
fxy060608's avatar
fxy060608 已提交
50
  viteLegacyOptions?: ViteLegacyOptions | false
fxy060608's avatar
fxy060608 已提交
51
}
52
export interface VitePluginUniResolvedOptions extends VitePluginUniOptions {
fxy060608's avatar
fxy060608 已提交
53
  base: string
fxy060608's avatar
fxy060608 已提交
54
  command: ResolvedConfig['command']
fxy060608's avatar
fxy060608 已提交
55
  platform: UniApp.PLATFORM
56
  inputDir: string
fxy060608's avatar
fxy060608 已提交
57
  outputDir: string
fxy060608's avatar
fxy060608 已提交
58
  assetsDir: string
59
  devServer?: ViteDevServer
fxy060608's avatar
fxy060608 已提交
60
  copyOptions?: Required<CopyOptions>
fxy060608's avatar
fxy060608 已提交
61
}
62

fxy060608's avatar
fxy060608 已提交
63
export { runDev, runBuild } from './cli/action'
64 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
  const plugins: Plugin[] = []

fxy060608's avatar
fxy060608 已提交
85
  if (options.viteLegacyOptions) {
86
    plugins.push(
fxy060608's avatar
fxy060608 已提交
87
      ...(legacyPlugin(
fxy060608's avatar
fxy060608 已提交
88
        initPluginViteLegacyOptions(options)
89 90 91 92
      ) as unknown as Plugin[])
    )
  }

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

  const uniPluginOptions = initPluginUniOptions(uniPlugins)

  options.copyOptions = uniPluginOptions.copyOptions

fxy060608's avatar
fxy060608 已提交
104 105
  if (options.vueJsxOptions) {
    plugins.push(
fxy060608's avatar
fxy060608 已提交
106
      vueJsxPlugin(
fxy060608's avatar
fxy060608 已提交
107 108 109 110 111
        initPluginVueJsxOptions(options, uniPluginOptions.compilerOptions)
      )
    )
  }

112
  plugins.push({
113
    name: 'vite:uni',
fxy060608's avatar
fxy060608 已提交
114
    config: createConfig(options, uniPlugins),
fxy060608's avatar
fxy060608 已提交
115
    // resolveId: createResolveId(options),
116
    configResolved: createConfigResolved(options),
117
  })
fxy060608's avatar
fxy060608 已提交
118
  plugins.push(...uniPlugins)
fxy060608's avatar
fxy060608 已提交
119

120 121 122 123
  // 执行 build 命令时,vite 强制了 NODE_ENV
  // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/build.ts#L405
  // const config = await resolveConfig(inlineConfig, 'build', 'production')
  // 在 @vitejs/plugin-vue 之前校正回来
fxy060608's avatar
fxy060608 已提交
124 125 126 127
  if (
    process.env.UNI_NODE_ENV &&
    process.env.UNI_NODE_ENV !== process.env.NODE_ENV
  ) {
128 129 130
    process.env.NODE_ENV = process.env.UNI_NODE_ENV
  }

fxy060608's avatar
fxy060608 已提交
131 132 133
  plugins.unshift(
    vuePlugin(initPluginVueOptions(options, uniPlugins, uniPluginOptions))
  )
fxy060608's avatar
fxy060608 已提交
134

fxy060608's avatar
fxy060608 已提交
135 136 137 138 139 140 141
  plugins.push(
    uniCopyPlugin({
      outputDir: process.env.UNI_OUTPUT_DIR,
      copyOptions: options.copyOptions,
    })
  )

142
  return plugins
143
}