index.ts 2.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
import path from 'path'
import debug from 'debug'
import fs from 'fs-extra'
fxy060608's avatar
fxy060608 已提交
4
import { AliasOptions } from 'vite'
fxy060608's avatar
fxy060608 已提交
5
import {
fxy060608's avatar
fxy060608 已提交
6
  CopyOptions,
fxy060608's avatar
fxy060608 已提交
7 8 9 10 11
  EXTNAME_VUE_RE,
  normalizeNodeModules,
  resolveBuiltIn,
  UniVitePlugin,
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
12 13 14

import { uniOptions } from './uni'
import { buildOptions } from './build'
fxy060608's avatar
fxy060608 已提交
15 16
import { createConfigResolved } from './configResolved'
import { EmittedFile } from 'rollup'
fxy060608's avatar
fxy060608 已提交
17

fxy060608's avatar
fxy060608 已提交
18
const debugMp = debug('vite:uni:mp')
fxy060608's avatar
fxy060608 已提交
19
export interface UniMiniProgramPluginOptions {
fxy060608's avatar
fxy060608 已提交
20 21
  vite: {
    alias: AliasOptions
fxy060608's avatar
fxy060608 已提交
22
    copyOptions: CopyOptions
fxy060608's avatar
fxy060608 已提交
23
  }
fxy060608's avatar
fxy060608 已提交
24
  global: string
fxy060608's avatar
fxy060608 已提交
25 26 27 28 29 30
  app: {
    darkmode: boolean
    subpackages: boolean
  }
  project: {
    filename: string
fxy060608's avatar
fxy060608 已提交
31
    source: Record<string, unknown>
fxy060608's avatar
fxy060608 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  }
  template: {
    extname: string
  }
  style: {
    extname: string
    cssVars: {
      '--status-bar-height': string
      '--window-top': string
      '--window-bottom': string
      '--window-left': string
      '--window-right': string
    }
  }
  filter?: {
    extname: string
    tag: string
  }
fxy060608's avatar
fxy060608 已提交
50 51
}

fxy060608's avatar
fxy060608 已提交
52 53 54 55
export function uniMiniProgramPlugin(
  options: UniMiniProgramPluginOptions
): UniVitePlugin {
  const {
fxy060608's avatar
fxy060608 已提交
56
    vite: { alias, copyOptions },
fxy060608's avatar
fxy060608 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    template,
  } = options
  const emitFile: (emittedFile: EmittedFile) => string = (emittedFile) => {
    if (emittedFile.type === 'asset') {
      const filename = emittedFile.fileName!
      const outputFilename = normalizeNodeModules(
        path.resolve(
          process.env.UNI_OUTPUT_DIR,
          path.relative(process.env.UNI_INPUT_DIR, filename)
        )
      ).replace(EXTNAME_VUE_RE, template.extname)
      debugMp(outputFilename)
      fs.outputFileSync(outputFilename, emittedFile.source!)
      return outputFilename
    }
    return ''
  }
fxy060608's avatar
fxy060608 已提交
74 75
  return {
    name: 'vite:uni-mp',
fxy060608's avatar
fxy060608 已提交
76 77 78 79
    uni: uniOptions({
      copyOptions,
      miniProgram: { emitFile },
    }),
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84 85 86
    config() {
      return {
        resolve: {
          alias: {
            vue: resolveBuiltIn('@dcloudio/uni-mp-vue'),
            ...alias,
          },
fxy060608's avatar
fxy060608 已提交
87
        },
fxy060608's avatar
fxy060608 已提交
88 89 90
        build: buildOptions(),
      }
    },
fxy060608's avatar
fxy060608 已提交
91
    configResolved: createConfigResolved(options),
fxy060608's avatar
fxy060608 已提交
92
  }
fxy060608's avatar
fxy060608 已提交
93
}