From ae4c40505c9dd6bce6ff3368361ef17c560bc5fc Mon Sep 17 00:00:00 2001 From: sunyichen Date: Fri, 6 Jun 2025 15:53:24 +0800 Subject: [PATCH] Handle outside module imports for singlefile emit Issue: https://gitee.com/openharmony/developtools_ace_ets2bundle/issues/ICAKFP Signed-off-by: sunyichen Change-Id: I6a8d746aaa3690432e6fbedd06ae13b83ef3fdd0 --- compiler/src/process_module_files.ts | 28 ++++++++++++++++------------ compiler/src/utils.ts | 15 +++++++++------ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/compiler/src/process_module_files.ts b/compiler/src/process_module_files.ts index 63b38aa1a..67ed1319f 100644 --- a/compiler/src/process_module_files.ts +++ b/compiler/src/process_module_files.ts @@ -39,7 +39,7 @@ import { CompileEvent, createAndStartEvent, stopEvent, - } from './performance'; +} from './performance'; export const SRC_MAIN: string = 'src/main'; @@ -83,12 +83,12 @@ export async function writeFileSyncByNode(node: ts.SourceFile, projectConfig: Ob if (!isDebug(projectConfig)) { const eventWriteObfuscatedSourceCode = createAndStartEvent(eventWriteFileSyncByNode, 'write obfuscated source code'); await writeObfuscatedSourceCode({ - content: mixedInfo.content, - buildFilePath: temporaryFile, - relativeSourceFilePath: relativeFilePath, - originSourceFilePath: node.fileName, - rollupModuleId: moduleId ? moduleId : undefined - }, logger, projectConfig, sourceMaps); + content: mixedInfo.content, + buildFilePath: temporaryFile, + relativeSourceFilePath: relativeFilePath, + originSourceFilePath: node.fileName, + rollupModuleId: moduleId ? moduleId : undefined + }, logger, projectConfig, sourceMaps); stopEvent(eventWriteObfuscatedSourceCode); return; } @@ -127,11 +127,15 @@ function genContentAndSourceMapInfo(node: ts.SourceFile, moduleId: string | unde ts.getNewLineCharacter({ newLine: ts.NewLineKind.LineFeed, removeComments: false })); printer.writeFile(node, writer, sourceMapGenerator); const sourceMapJson: ts.RawSourceMap = sourceMapGenerator.toJSON(); - sourceMapJson.sources = [ - toUnixPath(fileName).startsWith(toUnixPath(projectConfig.projectRootPath)) ? - toUnixPath(fileName).replace(toUnixPath(projectConfig.projectRootPath) + '/', '') : - toUnixPath(fileName).replace(toUnixPath(metaInfo.belongProjectPath) + '/', '') - ]; + let source: string = fileName; + if (toUnixPath(fileName).startsWith(toUnixPath(projectConfig.projectRootPath))) { + source = toUnixPath(fileName).replace(toUnixPath(projectConfig.projectRootPath) + '/', ''); + } else { + if (metaInfo.belongProjectPath) { + source = toUnixPath(fileName).replace(toUnixPath(metaInfo.belongProjectPath) + '/', ''); + } + } + sourceMapJson.sources = [source]; let content: string = writer.getText(); if (process.env.compileTool !== 'rollup') { content = transformModuleSpecifier(fileName, processSystemApi(content, true), projectConfig); diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index 7b0a287a9..7ee4ced70 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -44,7 +44,7 @@ import { USE_SHARED_STORAGE, STORAGE } from './pre_define'; -import { +import { ERROR_DESCRIPTION, HvigorLogInfo, HvigorErrorInfo @@ -104,13 +104,13 @@ export function emitLogInfo(loader: Object, infos: LogInfo[], fastBuild: boolean resourcePath: string | null = null, hvigorLogger: Object | undefined = undefined): void { if (infos && infos.length) { infos.forEach((item) => { - hvigorLogger ? emitLogInfoFromHvigorLogger(hvigorLogger, item, loader, fastBuild, resourcePath) + hvigorLogger ? emitLogInfoFromHvigorLogger(hvigorLogger, item, loader, fastBuild, resourcePath) : emitLogInfoFromLoader(loader, item, fastBuild, resourcePath); }); } } -function emitLogInfoFromHvigorLogger(hvigorLogger: Object, info: LogInfo, loader: Object, +function emitLogInfoFromHvigorLogger(hvigorLogger: Object, info: LogInfo, loader: Object, fastBuild: boolean, resourcePath: string | null): void { if (!isHvigorLogInfo(info)) { emitLogInfoFromLoader(loader, info, fastBuild, resourcePath); @@ -148,9 +148,9 @@ function emitLogInfoFromLoader(loader: Object, info: LogInfo, fastBuild: boolean } export function addLog( - type: LogType, - message: string, - pos: number, + type: LogType, + message: string, + pos: number, log: LogInfo[], sourceFile: ts.SourceFile, hvigorLogInfo?: HvigorLogInfo @@ -392,6 +392,9 @@ export function genTemporaryPath(filePath: string, projectPath: string, buildPat } else { relativeFilePath = filePath.replace(toUnixPath(projectConfig.projectRootPath), ''); } + if (relativeFilePath === filePath) { + return ''; + } const output: string = path.join(buildPath, relativeFilePath); return output; } -- Gitee