index.ts 4.8 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 8
import vueJsxPlugin from '@vitejs/plugin-vue-jsx'
import legacyPlugin 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
import { createConfig } from './config'
import { createConfigResolved } from './configResolved'
fxy060608's avatar
fxy060608 已提交
18
import { uniCopyPlugin } from './plugins/copy'
fxy060608's avatar
fxy060608 已提交
19
// import { uniMovePlugin } from './plugins/move'
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24
import {
  initExtraPlugins,
  initPluginUniOptions,
  rewriteCompilerSfcParse,
} from './utils'
fxy060608's avatar
fxy060608 已提交
25
import {
fxy060608's avatar
fxy060608 已提交
26
  createPluginVueInstance,
fxy060608's avatar
fxy060608 已提交
27 28 29 30
  initPluginViteLegacyOptions,
  initPluginVueJsxOptions,
  initPluginVueOptions,
} from './vue'
fxy060608's avatar
fxy060608 已提交
31
import { initEnv } from './cli/utils'
fxy060608's avatar
fxy060608 已提交
32

fxy060608's avatar
fxy060608 已提交
33
const debugUni = debug('uni:plugin')
fxy060608's avatar
fxy060608 已提交
34

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

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

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

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

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

fxy060608's avatar
fxy060608 已提交
62
export { runDev, runBuild } from './cli/action'
63 64 65

export default function uniPlugin(
  rawOptions: VitePluginUniOptions = {}
66
): Plugin[] {
fxy060608's avatar
fxy060608 已提交
67 68 69
  // 三方插件(如vitest)可能提供了自己的入口命令,需要补充 env 初始化逻辑
  initEnv('unknown', { platform: process.env.UNI_PLATFORM || 'h5' })

70 71
  const options: VitePluginUniResolvedOptions = {
    ...rawOptions,
fxy060608's avatar
fxy060608 已提交
72
    base: '/',
fxy060608's avatar
fxy060608 已提交
73
    assetsDir: 'assets',
fxy060608's avatar
fxy060608 已提交
74 75
    inputDir: '',
    outputDir: '',
fxy060608's avatar
fxy060608 已提交
76
    command: 'serve',
fxy060608's avatar
fxy060608 已提交
77
    platform: 'h5',
78
  }
fxy060608's avatar
fxy060608 已提交
79 80 81 82

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

fxy060608's avatar
fxy060608 已提交
83
  initPreContext(options.platform, process.env.UNI_CUSTOM_CONTEXT)
fxy060608's avatar
fxy060608 已提交
84

85
  const plugins: Plugin[] = []
fxy060608's avatar
fxy060608 已提交
86 87
  // 仅限 h5
  if (options.viteLegacyOptions && options.platform === 'h5') {
88
    plugins.push(
fxy060608's avatar
fxy060608 已提交
89
      ...(legacyPlugin(
fxy060608's avatar
fxy060608 已提交
90
        initPluginViteLegacyOptions(options)
91 92 93 94
      ) as unknown as Plugin[])
    )
  }

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

  const uniPluginOptions = initPluginUniOptions(uniPlugins)

  options.copyOptions = uniPluginOptions.copyOptions

fxy060608's avatar
fxy060608 已提交
106 107
  if (options.vueJsxOptions) {
    plugins.push(
fxy060608's avatar
fxy060608 已提交
108
      vueJsxPlugin(
fxy060608's avatar
fxy060608 已提交
109 110 111 112 113
        initPluginVueJsxOptions(
          options,
          uniPluginOptions.compilerOptions,
          uniPluginOptions.jsxOptions
        )
fxy060608's avatar
fxy060608 已提交
114 115 116 117
      )
    )
  }

118
  plugins.push({
fxy060608's avatar
fxy060608 已提交
119
    name: 'uni',
fxy060608's avatar
fxy060608 已提交
120
    config: createConfig(options, uniPlugins),
fxy060608's avatar
fxy060608 已提交
121
    // resolveId: createResolveId(options),
122
    configResolved: createConfigResolved(options),
123
  })
fxy060608's avatar
fxy060608 已提交
124
  plugins.push(...uniPlugins)
fxy060608's avatar
fxy060608 已提交
125

126 127 128 129
  // 执行 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 已提交
130 131 132 133
  if (
    process.env.UNI_NODE_ENV &&
    process.env.UNI_NODE_ENV !== process.env.NODE_ENV
  ) {
134 135 136
    process.env.NODE_ENV = process.env.UNI_NODE_ENV
  }

fxy060608's avatar
fxy060608 已提交
137
  plugins.unshift(
fxy060608's avatar
fxy060608 已提交
138 139 140
    createPluginVueInstance(
      initPluginVueOptions(options, uniPlugins, uniPluginOptions)
    )
fxy060608's avatar
fxy060608 已提交
141
  )
fxy060608's avatar
fxy060608 已提交
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156
  let addCopyPlugin = false
  if (options.platform !== 'app') {
    addCopyPlugin = true
  } else {
    // 仅在 vue 或 纯原生 App.vue 编译时做 copy
    if (
      process.env.UNI_COMPILER === 'vue' ||
      (process.env.UNI_RENDERER === 'native' &&
        process.env.UNI_RENDERER_NATIVE === 'appService')
    ) {
      addCopyPlugin = true
    }
  }
  if (addCopyPlugin) {
fxy060608's avatar
fxy060608 已提交
157 158 159 160 161 162 163
    plugins.push(
      uniCopyPlugin({
        outputDir: process.env.UNI_OUTPUT_DIR,
        copyOptions: options.copyOptions,
      })
    )
  }
fxy060608's avatar
fxy060608 已提交
164

fxy060608's avatar
fxy060608 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  // let addSourceMapPlugin = false
  // if (addSourceMapPlugin) {
  //   plugins.push(
  //     uniMovePlugin({
  //       apply: 'build',
  //       enforce: 'post',
  //       cwd: process.env.UNI_OUTPUT_DIR,
  //       pattern: '**/*.js.map',
  //       dest: path.resolve(
  //         process.env.UNI_OUTPUT_DIR,
  //         '../.s/' + process.env.UNI_PLATFORM
  //       ),
  //     })
  //   )
  // }

181
  return plugins
182
}