Skip to content

Commit

Permalink
fix(uts): 修复 uts插件三方依赖的文件变更不触发编译
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Jan 11, 2025
1 parent c454973 commit 4a3b76c
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions packages/uni-cli-shared/src/vite/plugins/uts/uni_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import fs from 'fs-extra'
import path from 'path'
import { once } from '@dcloudio/uni-shared'
import { dataToEsm } from '@rollup/pluginutils'
import type { ChangeEvent } from 'rollup'
import type { SyncUniModulesFilePreprocessor } from '@dcloudio/uni-uts-v1'
import type { ChangeEvent, PluginContext } from 'rollup'
import type {
CompileResult,
SyncUniModulesFilePreprocessor,
} from '@dcloudio/uni-uts-v1'
import {
genUniExtApiDeclarationFileOnce,
initUTSKotlinAutoImportsOnce,
Expand Down Expand Up @@ -170,15 +173,7 @@ const createUniXAppHarmonyUniModulesSyncFilePreprocessorOnce = once(() => {
return createUniModulesSyncFilePreprocessor('app', 'app-harmony', true)
})

const utsModuleCaches = new Map<
string,
() => Promise<void | {
code: string
deps: string[]
encrypt: boolean
meta?: any
}>
>()
const utsModuleCaches = new Map<string, () => Promise<void | CompileResult>>()

interface UniUTSPluginOptions {
x?: boolean
Expand Down Expand Up @@ -227,6 +222,17 @@ const emptyHarmonyCacheDirOnce = once(() => {
emptyCacheDir('app-harmony')
})

const handleCompileResult = (
result: CompileResult,
pluginContext?: PluginContext
) => {
if (pluginContext) {
result.deps.forEach((dep) => {
pluginContext.addWatchFile(dep)
})
}
}

// 该插件仅限app-android、app-ios、app-harmony
export function uniUTSAppUniModulesPlugin(
options: UniUTSPluginOptions = {}
Expand Down Expand Up @@ -314,7 +320,10 @@ export function uniUTSAppUniModulesPlugin(
}[]
>()

const compilePlugin = async (pluginDir: string) => {
const compilePlugin = async (
pluginDir: string,
pluginContext?: PluginContext
) => {
const pluginId = path.basename(pluginDir)

if (uniXKotlinCompiler) {
Expand Down Expand Up @@ -424,9 +433,20 @@ export function uniUTSAppUniModulesPlugin(
// 本次编译流程中已编译过该插件,直接使用缓存
const depPluginDir = normalizePath(path.resolve(uniModulesDir, dep))
if (utsModuleCaches.get(depPluginDir)) {
await utsModuleCaches.get(depPluginDir)!()
await utsModuleCaches.get(depPluginDir)!().then((result) => {
if (result) {
handleCompileResult(result, pluginContext)
}
})
} else {
await compilePlugin(path.resolve(inputDir, 'uni_modules', dep))
await compilePlugin(
path.resolve(inputDir, 'uni_modules', dep),
pluginContext
).then((result) => {
if (result) {
handleCompileResult(result, pluginContext)
}
})
}
}
}
Expand Down Expand Up @@ -635,7 +655,7 @@ export function uniUTSAppUniModulesPlugin(
})
}
const compile = once(() => {
return compilePlugin(pluginDir)
return compilePlugin(pluginDir, this)
})
utsModuleCaches.set(pluginDir, compile)
const result = await compile()
Expand Down

0 comments on commit 4a3b76c

Please sign in to comment.