Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions packages/vite-plugin-uni/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os from 'os'
import path from 'path'
import fs from 'fs-extra'
import type { PluginContext } from 'rollup'

import { customResolver } from '../src/config/resolve'

describe('resolve', () => {
const originalEnv = {
UNI_PLATFORM: process.env.UNI_PLATFORM,
UNI_UTS_PLATFORM: process.env.UNI_UTS_PLATFORM,
UNI_INPUT_DIR: process.env.UNI_INPUT_DIR,
UNI_APP_X: process.env.UNI_APP_X,
}

let inputDir = ''

beforeEach(() => {
inputDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'vite-plugin-uni-resolve-')
)
process.env.UNI_PLATFORM = 'app'
process.env.UNI_UTS_PLATFORM = 'app-android'
process.env.UNI_INPUT_DIR = inputDir
process.env.UNI_APP_X = 'false'

fs.outputFileSync(
path.join(
inputDir,
'uni_modules/uts-button/utssdk/app-android/index.uts'
),
'export const utsButton = 1'
)
})

afterEach(() => {
Object.entries(originalEnv).forEach(([key, value]) => {
if (value === undefined) {
Reflect.deleteProperty(process.env, key)
} else {
process.env[key] = value
}
})
if (inputDir) {
fs.removeSync(inputDir)
}
})

test('customResolver prefixes app uts absolute paths with /@fs/ on Windows', () => {
const utsModuleDir = path.join(inputDir, 'uni_modules/uts-button')
const pluginContext = {} as PluginContext
const resolved = customResolver.call(
pluginContext,
utsModuleDir,
undefined,
{
attributes: {},
isEntry: false,
}
)

expect(resolved).toBe(
'/@fs/' + utsModuleDir.replace(/\\/g, '/') + '?uts-proxy'
)
})
})
10 changes: 7 additions & 3 deletions packages/vite-plugin-uni/src/config/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import type { Alias, ResolverFunction, UserConfig } from 'vite'
import {
FS_PREFIX,
extensions,
isNormalCompileTarget,
isWindows,
Expand All @@ -16,11 +17,14 @@ import type { VitePluginUniResolvedOptions } from '..'
function resolveUTSModuleProxyFile(id: string, importer: string) {
const file = resolveUTSAppModule(process.env.UNI_UTS_PLATFORM, id, importer)
if (file) {
const normalizedFile = isWindows ? normalizePath(file) : file
// app-js 会返回完整路径,不需要 uts-proxy
if (file.endsWith('.uts')) {
return file
if (normalizedFile.endsWith('.uts')) {
return isWindows ? FS_PREFIX + normalizedFile : normalizedFile
}
return file + '?uts-proxy'
return isWindows
? `${FS_PREFIX + normalizedFile}?uts-proxy`
: normalizedFile + '?uts-proxy'
}
}

Expand Down