From b90326af61552b6a9df7d9e379248695881f83f3 Mon Sep 17 00:00:00 2001 From: huyunhui Date: Tue, 12 Mar 2024 12:24:16 +0000 Subject: [PATCH] fix error in generating sourcemap Issue: https://gitee.com/openharmony/developtools_ace_ets2bundle/issues/I97YMU Signed-off-by: huyunhui Change-Id: I1d527fe70aca31d128ba52dabafc2773ce4f7de7 --- .../ark_compiler/module/module_mode.ts | 11 +++--- .../mock/rollup_mock/common.ts | 6 ++++ .../module/module_mode.test.ts | 34 ++++++++++++++++++- .../entry/build/hotReload/sourceMaps.map | 4 +-- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/compiler/src/fast_build/ark_compiler/module/module_mode.ts b/compiler/src/fast_build/ark_compiler/module/module_mode.ts index 5bfc2151d..f7b3ef978 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_mode.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_mode.ts @@ -338,9 +338,10 @@ export class ModuleMode extends CommonMode { let unusedFiles = []; let compileFileList: Set = new Set(); this.moduleInfos.forEach((moduleInfo: ModuleInfo, moduleId: string) => { + const extName: string = shouldETSOrTSFileTransformToJS(moduleId, this.projectConfig) ? EXTNAME_JS : EXTNAME_TS; moduleId = moduleId.replace(this.projectConfig.projectRootPath, this.projectConfig.cachePath); - if (moduleId.endsWith(EXTNAME_ETS)) { - moduleId = changeFileExtension(moduleId, EXTNAME_TS); + if (moduleId.endsWith(EXTNAME_ETS) || moduleId.endsWith(EXTNAME_TS)) { + moduleId = changeFileExtension(moduleId, extName); } compileFileList.add(toUnixPath(moduleId)); }) @@ -761,8 +762,10 @@ export class ModuleMode extends CommonMode { projectConfig.projectRootPath + path.sep, '')); Object.keys(sourceMap).forEach(key => { let newKey: string = relativeCachePath + '/' + key; - if (newKey.endsWith(EXTNAME_ETS)) { - newKey = changeFileExtension(newKey, EXTNAME_TS); + if (!newKey.endsWith(EXTNAME_JS)) { + const moduleId: string = this.projectConfig.projectRootPath + path.sep + key; + const extName: string = shouldETSOrTSFileTransformToJS(moduleId, this.projectConfig) ? EXTNAME_JS : EXTNAME_TS; + newKey = changeFileExtension(newKey, extName); } sourceMap[newKey] = sourceMap[key]; delete sourceMap[key]; diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/common.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/common.ts index abb03c43c..6083c3399 100644 --- a/compiler/test/ark_compiler_ut/mock/rollup_mock/common.ts +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/common.ts @@ -36,6 +36,12 @@ export const ENTRYABILITY_TS_PATH_DEFAULT: string = '/src/main/entryability/Entr export const ENTRYABILITY_JS_PATH_DEFAULT: string = '/src/main/entryability/EntryAbility.js'; export const INDEX_ETS_PATH_DEFAULT: string = '/src/main/pages/Index.ets'; export const INDEX_JS_PATH_DEFAULT: string = '/src/main/pages/Index.js'; +export const INDEX_SOURCE_PATH: string = 'entry/src/main/pages/Index.ets'; +export const INDEX_JS_CACHE_PATH: string = 'entry/build/entry/src/main/pages/Index.js'; +export const INDEX_TS_CACHE_PATH: string = 'entry/build/entry/src/main/pages/Index.ts'; +export const ENTRYABILITY_SOURCE_PATH: string = 'entry/src/main/pages/EntryAbility.ts'; +export const ENTRYABILITY_JS_CACHE_PATH: string = 'entry/build/entry/src/main/pages/EntryAbility.js'; +export const ENTRYABILITY_TS_CACHE_PATH: string = 'entry/build/entry/src/main/pages/EntryAbility.ts'; export const ENTRYABILITY_TS_RECORDNAME: string = '/entry/src/main/entryability/EntryAbility'; export const ENTRYABILITY_JS_RECORDNAME: string = '/entry/src/main/entryability/EntryAbility'; diff --git a/compiler/test/ark_compiler_ut/module/module_mode.test.ts b/compiler/test/ark_compiler_ut/module/module_mode.test.ts index 2a597b7a0..0a363ee75 100644 --- a/compiler/test/ark_compiler_ut/module/module_mode.test.ts +++ b/compiler/test/ark_compiler_ut/module/module_mode.test.ts @@ -24,6 +24,7 @@ import cluster from 'cluster'; import { toUnixPath } from '../../../lib/utils'; import { newSourceMaps } from '../../../lib/fast_build/ark_compiler/transform'; +import { shouldETSOrTSFileTransformToJS } from '../../../lib/fast_build/ark_compiler/utils'; import { RELEASE, DEBUG, @@ -49,7 +50,13 @@ import { PKG_MODULES, ENTRY_MODULE_NAME_DEFAULT, TEST, - NEWFILE + NEWFILE, + INDEX_SOURCE_PATH, + INDEX_JS_CACHE_PATH, + INDEX_TS_CACHE_PATH, + ENTRYABILITY_SOURCE_PATH, + ENTRYABILITY_JS_CACHE_PATH, + ENTRYABILITY_TS_CACHE_PATH } from '../mock/rollup_mock/common'; import projectConfig from '../utils/processProjectConfig'; import { @@ -417,6 +424,31 @@ mocha.describe('test module_mode file api', function () { } }); + mocha.it('3-5: test updateCachedSourceMaps when ets(ts) file is transformed to js file', function () { + this.rollup.build(); + const moduleMode = new ModuleModeMock(this.rollup); + moduleMode.cacheSourceMapPath = ''; + newSourceMaps[INDEX_SOURCE_PATH] = {}; + moduleMode.updateCachedSourceMapsMock(); + let moduleId: string = moduleMode.projectConfig.projectRootPath + path.sep + INDEX_SOURCE_PATH; + if (shouldETSOrTSFileTransformToJS(moduleId, moduleMode.projectConfig)) { + expect(INDEX_JS_CACHE_PATH in moduleMode.cacheSourceMapObject).to.be.true; + } else { + expect(INDEX_TS_CACHE_PATH in moduleMode.cacheSourceMapObject).to.be.true; + } + newSourceMaps[ENTRYABILITY_SOURCE_PATH] = {}; + moduleMode.updateCachedSourceMapsMock(); + moduleId = moduleMode.projectConfig.projectRootPath + path.sep + ENTRYABILITY_SOURCE_PATH; + if (shouldETSOrTSFileTransformToJS(moduleId, moduleMode.projectConfig)) { + expect(ENTRYABILITY_JS_CACHE_PATH in moduleMode.cacheSourceMapObject).to.be.true; + } else { + expect(ENTRYABILITY_TS_CACHE_PATH in moduleMode.cacheSourceMapObject).to.be.true; + } + for (const key of Object.keys(newSourceMaps)) { + delete newSourceMaps[key]; + } + }) + mocha.it('4-1: test generateCompileFilesInfo under build debug', function () { this.rollup.build(); const moduleMode = new ModuleModeMock(this.rollup); diff --git a/compiler/test/ark_compiler_ut/testdata/testcase_def/entry/build/hotReload/sourceMaps.map b/compiler/test/ark_compiler_ut/testdata/testcase_def/entry/build/hotReload/sourceMaps.map index 318fa7581..e1dd48176 100644 --- a/compiler/test/ark_compiler_ut/testdata/testcase_def/entry/build/hotReload/sourceMaps.map +++ b/compiler/test/ark_compiler_ut/testdata/testcase_def/entry/build/hotReload/sourceMaps.map @@ -1,11 +1,11 @@ { - "entry/build/entry/src/main/entryability/EntryAbility.ts": { + "entry/build/entry/src/main/entryability/EntryAbility.js": { "file": "EntryAbility.ts", "sources": [ "entry/src/main/entryability/EntryAbility.ts" ] }, - "entry/build/entry/src/main/pages/Index.ts": { + "entry/build/entry/src/main/pages/Index.js": { "file": "Index.ets", "sources": [ "entry/src/main/pages/Index.ets" -- Gitee