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 753f1556ea91f329bcb71371d934f9f10d559177..1375030e3acdfadfe3444064c3d92dd167310eaf 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_mode.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_mode.ts @@ -34,6 +34,7 @@ import { MODULES_CACHE, NPMENTRIES_TXT, SOURCEMAPS, + SOURCEMAPS_JSON, TEMPORARY, WIDGETS_ABC, HAP_PACKAGE, @@ -61,6 +62,7 @@ import { CommonMode } from '../common/common_mode'; import { newSourceMaps } from '../transform'; import { changeFileExtension, + genCachePath, getEs2abcFileThreadNumber, isCommonJsPluginVirtualFile, isCurrentProjectFiles @@ -121,11 +123,13 @@ export class ModuleMode extends CommonMode { moduleInfos: Map; pkgEntryInfos: Map; hashJsonObject: any; + cacheSourceMapObject: any; filesInfoPath: string; npmEntriesInfoPath: string; moduleAbcPath: string; sourceMapPath: string; cacheFilePath: string; + cacheSourceMapPath: string; workerNumber: number; npmEntriesProtoFilePath: string; protoFilePath: string; @@ -136,12 +140,14 @@ export class ModuleMode extends CommonMode { this.moduleInfos = new Map(); this.pkgEntryInfos = new Map(); this.hashJsonObject = {}; + this.cacheSourceMapObject = {}; this.filesInfoPath = path.join(this.projectConfig.cachePath, FILESINFO_TXT); this.npmEntriesInfoPath = path.join(this.projectConfig.cachePath, NPMENTRIES_TXT); const outPutABC: string = this.projectConfig.widgetCompile ? WIDGETS_ABC : MODULES_ABC; this.moduleAbcPath = path.join(this.projectConfig.aceModuleBuild, outPutABC); this.sourceMapPath = path.join(this.projectConfig.aceModuleBuild, SOURCEMAPS); this.cacheFilePath = path.join(this.projectConfig.cachePath, MODULES_CACHE); + this.cacheSourceMapPath = path.join(this.projectConfig.cachePath, SOURCEMAPS_JSON); this.workerNumber = MAX_WORKER_NUMBER; this.npmEntriesProtoFilePath = path.join(this.projectConfig.cachePath, PROTOS, NPM_ENTRIES_PROTO_BIN); this.protoFilePath = path.join(this.projectConfig.cachePath, PROTOS, PROTO_FILESINFO_TXT); @@ -238,16 +244,48 @@ export class ModuleMode extends CommonMode { moduleInfos.set(filePath, new ModuleInfo(filePath, cacheFilePath, isCommonJs, recordName, sourceFile, packageName)); } + updateCachedSourceMaps(): void { + if (!fs.existsSync(this.cacheSourceMapPath)) { + this.cacheSourceMapObject = newSourceMaps; + return; + } + + this.cacheSourceMapObject = JSON.parse(fs.readFileSync(this.cacheSourceMapPath).toString()); + + // remove unused source files's sourceMap + let unusedFiles = []; + let compileFileList: Set = new Set(); + this.moduleInfos.forEach((moduleInfo: ModuleInfo, moduleId: string) => { + compileFileList.add(toUnixPath(moduleId)); + }) + + Object.keys(this.cacheSourceMapObject).forEach(key => { + const sourceFileAbsolutePath: string = toUnixPath(path.join(this.projectConfig.projectRootPath, key)); + if (!compileFileList.has(sourceFileAbsolutePath)) { + unusedFiles.push(key); + } + }); + unusedFiles.forEach(file => { + delete this.cacheSourceMapObject[file]; + }) + + // update sourceMap + Object.keys(newSourceMaps).forEach(key => { + this.cacheSourceMapObject[key] = newSourceMaps[key]; + }); + } + buildModuleSourceMapInfo() { if (!this.arkConfig.isDebug || this.projectConfig.widgetCompile) { return; } - mkdirsSync(this.projectConfig.aceModuleBuild); - const sourceMapFilePath: string = path.join(this.projectConfig.aceModuleBuild, SOURCEMAPS); - fs.writeFile(sourceMapFilePath, JSON.stringify(newSourceMaps, null, 2), 'utf-8', (err) => { + + this.updateCachedSourceMaps(); + fs.writeFile(this.sourceMapPath, JSON.stringify(this.cacheSourceMapObject, null, 2), 'utf-8', (err) => { if (err) { - this.throwArkTsCompilerError('ArkTS:ERROR failed to write cache sourceMaps json'); + this.throwArkTsCompilerError('ArkTS:ERROR failed to write sourceMaps'); } + fs.copyFileSync(this.sourceMapPath, this.cacheSourceMapPath); }); }