resolve.ts 1.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import path from 'path'
2
import { UserConfig } from 'vite'
fxy060608's avatar
fxy060608 已提交
3
import { isWindows, EXTNAME_VUE, normalizePath } from '@dcloudio/uni-cli-shared'
4 5
import { VitePluginUniResolvedOptions } from '..'

6 7
export function customResolver(updatedId: string) {
  if (isWindows) {
fxy060608's avatar
fxy060608 已提交
8
    return normalizePath(path.resolve(process.env.UNI_INPUT_DIR, updatedId))
9 10 11 12
  }
  return updatedId
}

13
export function createResolve(
fxy060608's avatar
fxy060608 已提交
14
  options: VitePluginUniResolvedOptions,
fxy060608's avatar
fxy060608 已提交
15
  _config: UserConfig
16 17
): UserConfig['resolve'] {
  return {
fxy060608's avatar
fxy060608 已提交
18 19 20 21 22 23 24
    // 必须使用alias解析,插件定制的resolveId,不会被应用到css等预处理器中
    alias: [
      // @ts-ignore because @rollup/plugin-alias' type doesn't allow function
      // replacement, but its implementation does work with function values.
      {
        find: /^(~@|@)\/(.*)/,
        replacement(_str: string, _$1: string, $2: string) {
fxy060608's avatar
fxy060608 已提交
25
          return normalizePath(path.resolve(options.inputDir, $2))
fxy060608's avatar
fxy060608 已提交
26
        },
27
        customResolver,
fxy060608's avatar
fxy060608 已提交
28 29
      },
    ],
30 31 32
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'].concat(
      EXTNAME_VUE
    ),
33 34
  }
}