Skip to content

Commit fbfb50e

Browse files
committed
fix(runner): 优化项目公共路径匹配规则
1 parent 673aa7b commit fbfb50e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/taro-vite-runner/src/harmony/ets.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export default async function (viteCompilerContext: ViteHarmonyCompilerContext):
133133
outputRoot,
134134
targetRoot: path.resolve(appPath, sourceRoot),
135135
resolve: this.resolve,
136+
modifyResolveId: viteCompilerContext.loaderMeta.modifyResolveId,
136137
})
137138

138139
return {

packages/taro-vite-runner/src/utils/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,23 @@ export function resolveAbsoluteRequire ({
297297
})
298298
}
299299

300+
let lastCommonPath = ''
300301
function getCommonPath(a: string, b: string) {
301302
const aArr = path.normalize(a).split(/[\\/]/)
302303
const bArr = path.normalize(b).split(/[\\/]/)
303304
let i = 0
304305
while (aArr[i] === bArr[i]) {
305306
i++
306307
}
307-
return aArr.slice(0, i).join('/')
308+
309+
if (aArr.length > i) {
310+
// Note: 项目外部文件,仅返回所有外部文件的最短公共路径
311+
if (!lastCommonPath || lastCommonPath.split(/[\\/]/).length > i) {
312+
lastCommonPath = aArr.slice(0, i).join('/')
313+
}
314+
return lastCommonPath
315+
} else {
316+
// Note: 项目内部文件,返回项目根路径
317+
return a
318+
}
308319
}

0 commit comments

Comments
 (0)