copy.ts 1.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
import path from 'path'
import debug from 'debug'
import type { Plugin } from 'vite'
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8

import {
  PUBLIC_DIR,
  uniViteCopyPlugin,
  UniViteCopyPluginTarget,
fxy060608's avatar
fxy060608 已提交
9 10
  parseSubpackagesRootOnce,
  normalizePath,
fxy060608's avatar
fxy060608 已提交
11 12 13
} from '@dcloudio/uni-cli-shared'
import { VitePluginUniResolvedOptions } from '..'

fxy060608's avatar
fxy060608 已提交
14 15
const debugCopy = debug('vite:uni:copy')

fxy060608's avatar
fxy060608 已提交
16 17 18 19
export function uniCopyPlugin({
  outputDir,
  copyOptions,
}: Pick<VitePluginUniResolvedOptions, 'outputDir' | 'copyOptions'>): Plugin {
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27 28 29 30
  const staticDir = PUBLIC_DIR + '/**/*'
  const uniModulesStaticDir = 'uni_modules/*/' + PUBLIC_DIR + '/**/*'
  const assets = [staticDir, uniModulesStaticDir]
  const subpackages = parseSubpackagesRootOnce(
    process.env.UNI_INPUT_DIR,
    process.env.UNI_PLATFORM
  )
  subpackages.forEach((root) => {
    assets.push(normalizePath(path.join(root, staticDir)))
    assets.push(normalizePath(path.join(root, uniModulesStaticDir)))
  })
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36 37 38 39 40
  copyOptions!.assets.forEach((asset) => {
    assets.push(asset)
  })
  const targets: UniViteCopyPluginTarget[] = [
    {
      src: assets,
      dest: outputDir,
    },
  ]
  targets.push(...copyOptions!.targets)
fxy060608's avatar
fxy060608 已提交
41
  debugCopy(targets)
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46
  return uniViteCopyPlugin({
    targets,
    verbose: process.env.DEBUG ? true : false,
  })
}