resolve.ts 3.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
import fs from 'fs'

import path from 'path'
import debug from 'debug'
import resolve from 'resolve'
import { once } from '@dcloudio/uni-shared'

import { normalizePath } from './utils'
fxy060608's avatar
fxy060608 已提交
9
import { isInHBuilderX } from './hbx/env'
10 11 12 13 14
import { extensions } from './constants'

export function requireResolve(filename: string, basedir: string) {
  return resolveWithSymlinks(filename, basedir)
}
fxy060608's avatar
fxy060608 已提交
15 16 17 18

function resolveWithSymlinks(id: string, basedir: string): string {
  return resolve.sync(id, {
    basedir,
19
    extensions,
fxy060608's avatar
fxy060608 已提交
20 21
    // necessary to work with pnpm
    preserveSymlinks: true,
fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    pathFilter(pkg, filepath, relativePath) {
      if (pkg.dcloudext && (pkg.dcloudext as any).type === 'native-uts') {
        if (
          process.env.UNI_APP_PLATFORM === 'app-android' ||
          process.env.UNI_APP_PLATFORM === 'app-ios'
        ) {
          const file = process.env.UNI_APP_PLATFORM + '/index.uts'
          if (
            fs.existsSync(path.join(filepath.replace(relativePath, ''), file))
          ) {
            return file
          }
        }
      }
      return relativePath
    },
fxy060608's avatar
fxy060608 已提交
38 39
  })
}
fxy060608's avatar
fxy060608 已提交
40

fxy060608's avatar
fxy060608 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
export function relativeFile(from: string, to: string) {
  const relativePath = normalizePath(path.relative(path.dirname(from), to))
  return relativePath.startsWith('.') ? relativePath : './' + relativePath
}

export const resolveMainPathOnce = once((inputDir: string) => {
  const mainTsPath = path.resolve(inputDir, 'main.ts')
  if (fs.existsSync(mainTsPath)) {
    return normalizePath(mainTsPath)
  }
  return normalizePath(path.resolve(inputDir, 'main.js'))
})

const ownerModules = ['@dcloudio/uni-app', '@dcloudio/vite-plugin-uni']

const paths: string[] = []

function resolveNodeModulePath(modulePath: string) {
  const nodeModulesPaths: string[] = []
  const nodeModulesPath = path.join(modulePath, 'node_modules')
  if (fs.existsSync(nodeModulesPath)) {
    nodeModulesPaths.push(nodeModulesPath)
  }
  const index = modulePath.lastIndexOf('node_modules')
  if (index > -1) {
fxy060608's avatar
fxy060608 已提交
66
    nodeModulesPaths.push(path.join(modulePath.slice(0, index), 'node_modules'))
fxy060608's avatar
fxy060608 已提交
67 68 69 70 71 72 73 74 75
  }
  return nodeModulesPaths
}

function initPaths() {
  const cliContext = process.env.UNI_CLI_CONTEXT
  if (cliContext) {
    const pathSet = new Set<string>()
    pathSet.add(path.join(cliContext, 'node_modules'))
fxy060608's avatar
fxy060608 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    if (!isInHBuilderX()) {
      ;[`@dcloudio/uni-` + process.env.UNI_PLATFORM, ...ownerModules].forEach(
        (ownerModule) => {
          let pkgPath: string = ''
          try {
            pkgPath = require.resolve(ownerModule + '/package.json', {
              paths: [cliContext],
            })
          } catch (e) {}
          if (pkgPath) {
            resolveNodeModulePath(path.dirname(pkgPath)).forEach(
              (nodeModulePath) => {
                pathSet.add(nodeModulePath)
              }
            )
          }
fxy060608's avatar
fxy060608 已提交
92
        }
fxy060608's avatar
fxy060608 已提交
93 94
      )
    }
fxy060608's avatar
fxy060608 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    paths.push(...pathSet)
    debug('uni-paths')(paths)
  }
}

export function getBuiltInPaths() {
  if (!paths.length) {
    initPaths()
  }
  return paths
}

export function resolveBuiltIn(path: string) {
  return require.resolve(path, { paths: getBuiltInPaths() })
}

fxy060608's avatar
fxy060608 已提交
111 112 113 114 115 116 117
export function resolveVueI18nRuntime() {
  return path.resolve(
    __dirname,
    '../lib/vue-i18n/dist/vue-i18n.runtime.esm-bundler.js'
  )
}

fxy060608's avatar
fxy060608 已提交
118 119 120
let componentsLibPath: string = ''
export function resolveComponentsLibPath() {
  if (!componentsLibPath) {
fxy060608's avatar
fxy060608 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134
    if (isInHBuilderX()) {
      componentsLibPath = path.join(
        resolveBuiltIn('@dcloudio/uni-components/package.json'),
        '../lib'
      )
    } else {
      componentsLibPath = path.join(
        resolveWithSymlinks(
          '@dcloudio/uni-components/package.json',
          process.env.UNI_INPUT_DIR
        ),
        '../lib'
      )
    }
fxy060608's avatar
fxy060608 已提交
135 136 137
  }
  return componentsLibPath
}