From 3fce88ea173004357b32d9137bfd9960d54b94c0 Mon Sep 17 00:00:00 2001 From: ElevenDuan Date: Tue, 11 Jun 2024 16:04:10 +0800 Subject: [PATCH] Merge bytecode har source map Signed-off-by: ElevenDuan Change-Id: I66045da9283eb43d63938b51db6ea498d70de2e1 --- compiler/main.js | 3 +++ .../ark_compiler/common/process_ark_config.ts | 2 +- .../ark_compiler/generate_sourcemap.ts | 23 +++++++++++++++++-- .../mock/rollup_mock/project_config.ts | 14 ++++++++++- .../module/module_mode.test.ts | 11 +++++++++ .../testcase_def/bytecodeHarSourceMap.json | 12 ++++++++++ 6 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 compiler/test/ark_compiler_ut/testdata/testcase_def/bytecodeHarSourceMap.json diff --git a/compiler/main.js b/compiler/main.js index 28088f295..aef028397 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -577,6 +577,9 @@ function loadBuildJson() { } }); } + if (!!aceBuildJson.byteCodeHar) { + projectConfig.useTsHar = true; + } } function initBuildInfo() { diff --git a/compiler/src/fast_build/ark_compiler/common/process_ark_config.ts b/compiler/src/fast_build/ark_compiler/common/process_ark_config.ts index a76eddb44..15e3d8e18 100644 --- a/compiler/src/fast_build/ark_compiler/common/process_ark_config.ts +++ b/compiler/src/fast_build/ark_compiler/common/process_ark_config.ts @@ -134,7 +134,7 @@ export function initArkProjectConfig(share: Object): Object { if (projectConfig.compileMode === ESMODULE) { arkProjectConfig.nodeModulesPath = buildJsonInfo.nodeModulesPath; - // harNameOhmMap stores har packages that are dependent on bytecode har when compile bytecode har. + // harNameOhmMap stores har packages that are dependent for bytecode har when compile bytecode har. arkProjectConfig.harNameOhmMap = buildJsonInfo.harNameOhmMap; arkProjectConfig.hspNameOhmMap = buildJsonInfo.hspNameOhmMap; projectConfig.packageDir = buildJsonInfo.packageManagerType === 'ohpm' ? OH_MODULES : NODE_MODULES; diff --git a/compiler/src/fast_build/ark_compiler/generate_sourcemap.ts b/compiler/src/fast_build/ark_compiler/generate_sourcemap.ts index 2f02c78ad..18e44fd8d 100644 --- a/compiler/src/fast_build/ark_compiler/generate_sourcemap.ts +++ b/compiler/src/fast_build/ark_compiler/generate_sourcemap.ts @@ -20,10 +20,13 @@ import { EXTNAME_ETS, EXTNAME_JS, EXTNAME_TS, + EXTNAME_MJS, + EXTNAME_CJS, + GEN_ABC_PLUGIN_NAME, SOURCEMAPS, SOURCEMAPS_JSON, - EXTNAME_MJS, - EXTNAME_CJS + yellow, + reset } from "./common/ark_define"; import { changeFileExtension, @@ -55,6 +58,7 @@ export class SourceMapGenerator { private sourceMaps: Object = {}; private isNewSourceMap: boolean = true; private keyCache: Map = new Map(); + private logger: Object; public sourceMapKeyMappingForObf: Map = new Map(); @@ -65,6 +69,7 @@ export class SourceMapGenerator { this.cacheSourceMapPath = path.join(this.projectConfig.cachePath, SOURCEMAPS_JSON); this.triggerAsync = rollupObject.async; this.triggerEndSignal = rollupObject.signal; + this.logger = rollupObject.share.getLogger(GEN_ABC_PLUGIN_NAME); } static init(rollupObject: Object): void { @@ -171,6 +176,20 @@ export class SourceMapGenerator { } const eventUpdateCachedSourceMaps = createAndStartEvent(parentEvent, 'update cached source maps'); + if (isDebug(this.projectConfig) && !this.projectConfig.byteCodeHar && !!this.projectConfig.byteCodeHarInfo) { + // Merge bytecode har source maps to hap/hsp. + for (const packageName in this.projectConfig.byteCodeHarInfo) { + const sourceMapsPath = this.projectConfig.byteCodeHarInfo[packageName].sourceMapsPath; + if (!sourceMapsPath) { + if (!!this.logger) { + this.logger.warn(yellow, `ArkTS:WARN Property 'sourceMapsPath' not found in '${packageName}'.`, reset); + } + continue; + } + const bytecodeHarSourceMap = JSON.parse(fs.readFileSync(toUnixPath(sourceMapsPath)).toString()) + Object.assign(this.sourceMaps, bytecodeHarSourceMap); + } + } const cacheSourceMapObject: Object = this.updateCachedSourceMaps(); stopEvent(eventUpdateCachedSourceMaps); diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts index 75b2f5857..aff51e070 100644 --- a/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts @@ -13,7 +13,10 @@ * limitations under the License. */ -import { PROJECT_ROOT } from "./path_config"; +import { + DEFAULT_PROJECT, + PROJECT_ROOT +} from "./path_config"; import { SDK_VERSION, BUNDLE_NAME_DEFAULT, @@ -198,6 +201,15 @@ class ProjectConfig { ]); } + public mockBytecodeHarInfo() { + this.byteCodeHarInfo = { + "har": { + "abcPath": "", + "sourceMapsPath": `${PROJECT_ROOT}/${DEFAULT_PROJECT}/bytecodeHarSourceMap.json`, + } + } + } + private initPath(proPath: string) { // build and preview let mode = this.isPreview ? 'preview' : 'build'; 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 d53064fd7..2ed85eea9 100644 --- a/compiler/test/ark_compiler_ut/module/module_mode.test.ts +++ b/compiler/test/ark_compiler_ut/module/module_mode.test.ts @@ -1261,6 +1261,17 @@ mocha.describe('test module_mode file api', function () { SourceMapGenerator.cleanSourceMapObject(); }); + mocha.it('8-9: test merge bytecode source map of buildModuleSourceMapInfo under build debug', async function () { + this.rollup.build(); + this.rollup.share.projectConfig.mockBytecodeHarInfo(); + const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); + const moduleMode = new ModuleModeMock(this.rollup); + moduleMode.buildModuleSourceMapInfoMock(sourceMapGenerator); + await sleep(1000); + expect(moduleMode.checkModuleSourceMapInfoMock() === true).to.be.true; + SourceMapGenerator.cleanSourceMapObject(); + }); + mocha.it('9-1: test getPkgModulesFilePkgName under build debug', function () { this.rollup.build(); this.rollup.share.projectConfig.packageDir = OH_MODULES; diff --git a/compiler/test/ark_compiler_ut/testdata/testcase_def/bytecodeHarSourceMap.json b/compiler/test/ark_compiler_ut/testdata/testcase_def/bytecodeHarSourceMap.json new file mode 100644 index 000000000..131cfcb65 --- /dev/null +++ b/compiler/test/ark_compiler_ut/testdata/testcase_def/bytecodeHarSourceMap.json @@ -0,0 +1,12 @@ +{ + "har|har|1.0.0|Index.js" :{ + "version": 3, + "sources": [ + "har/Index.ets" + ], + "names": [], + "mappings": "AAAA", + "file": "Index.ets", + "entry-package-info": "har|1.0.0" + } +} \ No newline at end of file -- Gitee