From 29a4eef1b1d94e81fc2bbc63d478e31bc231677c Mon Sep 17 00:00:00 2001 From: zhangbingce Date: Mon, 25 Jul 2022 22:47:04 +0800 Subject: [PATCH] [DFX] add varible names to sourcemap for dfx analyzing Signed-off-by: zhangbingce Change-Id: Ib82f9773e908d8535de0c78387ed2431e91bfcb6 --- compiler/src/compile_info.ts | 14 +++++++- compiler/src/process_system_module.ts | 6 +++- compiler/src/result_process.ts | 2 +- compiler/src/validate_ui_syntax.ts | 50 +++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/compiler/src/compile_info.ts b/compiler/src/compile_info.ts index a404124..8ac58dd 100644 --- a/compiler/src/compile_info.ts +++ b/compiler/src/compile_info.ts @@ -29,7 +29,8 @@ import ConcatSource from 'webpack-sources/lib/ConcatSource'; import { transformLog } from './process_ui_syntax'; import { - useOSFiles + useOSFiles, + sourcemapNamesCollection } from './validate_ui_syntax'; import { projectConfig } from '../main'; import { @@ -86,6 +87,17 @@ export class ResultStates { if (/\.map$/.test(key.toString()) && assets[key]._value) { assets[key]._value = assets[key]._value.toString().replace('.ets?entry', '.ets'); assets[key]._value = assets[key]._value.toString().replace('.ts?entry', '.ts'); + + let absPath: string = path.resolve(projectConfig.projectPath, key.toString().replace('.js.map','.js')); + if (sourcemapNamesCollection && absPath) { + let map: Map = sourcemapNamesCollection.get(absPath); + if (map && map.size != 0) { + let names: Array = Array.from(map).flat(); + let sourcemapObj: any = JSON.parse(assets[key]._value); + sourcemapObj.names = names; + assets[key]._value = JSON.stringify(sourcemapObj); + } + } } }); } diff --git a/compiler/src/process_system_module.ts b/compiler/src/process_system_module.ts index 858f2bf..bb15b87 100644 --- a/compiler/src/process_system_module.ts +++ b/compiler/src/process_system_module.ts @@ -13,7 +13,10 @@ * limitations under the License. */ -import { processSystemApi } from './validate_ui_syntax'; +import { + processSystemApi, + CollectImportNames +} from './validate_ui_syntax'; import { systemModules } from '../main'; module.exports = function processSystemModule(source: string): string { @@ -31,5 +34,6 @@ module.exports = function processSystemModule(source: string): string { return item; }); source = processSystemApi(source, false, this.resourcePath, true); + CollectImportNames(source, this.resourcePath); return source; }; diff --git a/compiler/src/result_process.ts b/compiler/src/result_process.ts index dfe06e9..9a7bf85 100644 --- a/compiler/src/result_process.ts +++ b/compiler/src/result_process.ts @@ -35,7 +35,7 @@ import { abilityConfig } from '../main'; module.exports = function resultProcess(source: string, map: any): void { process.env.compiler = BUILD_OFF; - source = processSystemApi(source, true); + source = processSystemApi(source, true, this.resourcePath); if (/\.(ets|ts)$/.test(this.resourcePath)) { componentInfo.id = 0; propertyCollection.clear(); diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 7fb3a8d..17e1032 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -137,6 +137,8 @@ export const isStaticViewCollection: Map = new Map(); export const packageCollection: Map> = new Map(); export const useOSFiles: Set = new Set(); +export const sourcemapNamesCollection: Map> = new Map(); +export const originalImportNamesMap: Map = new Map(); export function validateUISyntax(source: string, content: string, filePath: string, fileQuery: string): LogInfo[] { @@ -809,6 +811,7 @@ export function sourceReplace(source: string, sourcePath: string): ReplaceResult content = preprocessNewExtend(content); // process @system. content = processSystemApi(content, false, sourcePath); + CollectImportNames(content, sourcePath); return { content: content, @@ -1006,8 +1009,10 @@ export function processSystemApi(content: string, isProcessAllowList: boolean = if (isProcessAllowList) { systemValueCollection.add(importValue); if (projectConfig.compileMode !== ESMODULE) { + collectSourcemapNames(sourcePath, importValue, item5); return replaceSystemApi(item, importValue, item4, item5); } + collectSourcemapNames(sourcePath, importValue, item3); return replaceSystemApi(item, importValue, item2, item3); } @@ -1036,6 +1041,51 @@ export function processSystemApi(content: string, isProcessAllowList: boolean = return processInnerModule(processedContent, systemValueCollection); } +function collectSourcemapNames(sourcePath: string, changedName: string, originalName: string): void { + const cleanSourcePath: string = sourcePath.replace('.ets', '.js').replace('.ts', '.js'); + if (!sourcemapNamesCollection.has(cleanSourcePath)) { + return; + } + + let map: Map = sourcemapNamesCollection.get(cleanSourcePath); + if (map.has(changedName)) { + return; + } + + for (let entry of originalImportNamesMap.entries()) { + const key: string = entry[0]; + const value: string = entry[1]; + if (value === '@ohos.' + originalName || value === '@system.' + originalName) { + map.set(changedName.trim(), key); + sourcemapNamesCollection.set(cleanSourcePath, map); + originalImportNamesMap.delete(key); + break; + } + } +} + +export function CollectImportNames(content: string, sourcePath: string = null): void { + const REG_IMPORT_DECL: RegExp = + /(import|export)\s+(.+)\s+from\s+['"](\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"](\S+)['"]\s*\)/g; + + const decls: string[] = content.match(REG_IMPORT_DECL); + if (decls != undefined) { + decls.forEach(decl => { + const parts: string[] = decl.split(' '); + if (parts.length === 4 && parts[0] === 'import' && parts[2] === 'from' && !parts[3].includes('.so')) { + originalImportNamesMap.set(parts[1], parts[3].replace(/'/g, '')); + } + }) + } + + if (sourcePath && sourcePath != null) { + const cleanSourcePath: string = sourcePath.replace('.ets', '.js').replace('.ts', '.js'); + if (!sourcemapNamesCollection.has(cleanSourcePath)) { + sourcemapNamesCollection.set(cleanSourcePath, new Map()); + } + } +} + function processInnerModule(content: string, systemValueCollection: Set): string { systemValueCollection.forEach(element => { const target: string = element.trim() + '.default';