uts.ts 991 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import type { Plugin } from 'vite'
import fs from 'fs'
import path from 'path'
import { parseJson } from '@dcloudio/uni-cli-shared'

// 需要区分 android,iOS
export function uniUtsPlugin(): Plugin {
  // TODO 1.0 版本,解析到 uts module 时,动态编译 uts ?
  return {
    name: 'uts',
    apply: 'build',
    enforce: 'pre',
    load(id, opts) {
      if (opts && opts.ssr) {
        return id
      }
      if (!id.includes('uni_modules')) {
        return
      }
      const pkgPath = path.join(id, 'package.json')
      if (!fs.existsSync(pkgPath)) {
        return
      }
      const pkg = parseJson(fs.readFileSync(pkgPath, 'utf-8'))
      if (pkg.uni_modules?.type !== 'uts') {
        return
      }
fxy060608's avatar
fxy060608 已提交
28 29
      // 加载接口类
      return path.join(id, pkg.main || 'interface.uts')
fxy060608's avatar
fxy060608 已提交
30 31 32 33 34 35 36 37 38 39 40
    },
    transform(code, id, opts) {
      if (opts && opts.ssr) {
        return
      }
      if (path.extname(id.split('?')[0]) !== '.uts') {
        return
      }
    },
  }
}