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
import {
  CopyOptions,
  initModuleAlias,
  initPreContext,
fxy060608's avatar
fxy060608 已提交
14
  resolveSourceMapPath,
fxy060608's avatar
fxy060608 已提交
15
} 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'
D
DCloud_LXH 已提交
20
import { uniMovePlugin } from './plugins/move'
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25
import {
  initExtraPlugins,
  initPluginUniOptions,
  rewriteCompilerSfcParse,
} from './utils'
fxy060608's avatar
fxy060608 已提交
26
import {
fxy060608's avatar
fxy060608 已提交
27
  createPluginVueInstance,
fxy060608's avatar
fxy060608 已提交
28 29 30 31
  initPluginViteLegacyOptions,
  initPluginVueJsxOptions,
  initPluginVueOptions,
} from './vue'
fxy060608's avatar
fxy060608 已提交
32
import { initEnv } from './cli/utils'
fxy060608's avatar
fxy060608 已提交
33

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

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

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

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

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

47
export interface VitePluginUniOptions {
48
  vueOptions?: VueOptions
fxy060608's avatar
fxy060608 已提交
49
  vueJsxOptions?: (VueJSXPluginOptions & { babelPlugins?: any[] }) | 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[] {
fxy060608's avatar
fxy060608 已提交
68 69 70
  // 三方插件(如vitest)可能提供了自己的入口命令,需要补充 env 初始化逻辑
  initEnv('unknown', { platform: process.env.UNI_PLATFORM || 'h5' })

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

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

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

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

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

  const uniPluginOptions = initPluginUniOptions(uniPlugins)

  options.copyOptions = uniPluginOptions.copyOptions

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

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

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

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

144 145 146 147 148 149 150 151 152 153 154 155 156 157
  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 已提交
158 159 160 161 162 163 164
    plugins.push(
      uniCopyPlugin({
        outputDir: process.env.UNI_OUTPUT_DIR,
        copyOptions: options.copyOptions,
      })
    )
  }
fxy060608's avatar
fxy060608 已提交
165

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

181
  return plugins
182
}