From 7fc5a529dc164559c4443981f54005ca1e8bac8b Mon Sep 17 00:00:00 2001 From: lihong Date: Mon, 15 Aug 2022 17:42:56 +0800 Subject: [PATCH] lihong67@huawei.com optimize ts check increment. Signed-off-by: lihong Change-Id: Ib33c2d3aeed8ca28add1e98fb7c7d254c66136e4 --- compiler/main.js | 5 ++- compiler/src/compile_info.ts | 73 +++++++++++++++++++++++++++++++++--- compiler/src/ets_checker.ts | 29 +++++++++++++- compiler/webpack.config.js | 2 +- 4 files changed, 99 insertions(+), 10 deletions(-) diff --git a/compiler/main.js b/compiler/main.js index 436a0bf..f49eae5 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -58,13 +58,14 @@ function initProjectConfig(projectConfig) { projectConfig.aceProfilePath = projectConfig.aceProfilePath || process.env.aceProfilePath; projectConfig.aceModuleJsonPath = projectConfig.aceModuleJsonPath || process.env.aceModuleJsonPath; projectConfig.aceSuperVisualPath = projectConfig.aceSuperVisualPath || - process.env.aceSuperVisualPath + process.env.aceSuperVisualPath; projectConfig.hashProjectPath = projectConfig.hashProjectPath || - hashProjectPath(projectConfig.projectPath) + hashProjectPath(projectConfig.projectPath); projectConfig.aceBuildJson = projectConfig.aceBuildJson || process.env.aceBuildJson; projectConfig.cachePath = projectConfig.cachePath || process.env.cachePath || path.resolve(__dirname, 'node_modules/.cache'); projectConfig.aceSoPath = projectConfig.aceSoPath || process.env.aceSoPath; + projectConfig.xtsMode = /ets_loader_ark$/.test(__dirname); } function loadEntryObj(projectConfig) { diff --git a/compiler/src/compile_info.ts b/compiler/src/compile_info.ts index 965a083..b46aef6 100644 --- a/compiler/src/compile_info.ts +++ b/compiler/src/compile_info.ts @@ -32,23 +32,23 @@ import { useOSFiles, sourcemapNamesCollection } from './validate_ui_syntax'; -import { projectConfig } from '../main'; import { circularFile, mkDir, - writeFileSync } from './utils'; import { MODULE_ETS_PATH, MODULE_SHARE_PATH, BUILD_SHARE_PATH, - ARK } from './pre_define'; import { createLanguageService, createWatchCompilerHost } from './ets_checker'; -import { globalProgram } from '../main'; +import { + globalProgram, + projectConfig +} from '../main'; import cluster from 'cluster'; configure({ @@ -68,6 +68,19 @@ interface Info { }; } +export interface CacheFileName { + mtimeMs: number, + children: string[], + error: boolean +} + +interface NeedUpdateFlag { + flag: boolean; +} + +export let cache: Cache; +type Cache = Record; + export class ResultStates { private mStats: Stats; private mErrorCount: number = 0; @@ -186,7 +199,16 @@ export class ResultStates { createWatchCompilerHost(rootFileNames, this.printDiagnostic.bind(this), this.delayPrintLogCount.bind(this))); } else { - const languageService: ts.LanguageService = createLanguageService(rootFileNames); + let languageService: ts.LanguageService = null; + let cacheFile: string = null; + if (projectConfig.xtsMode) { + languageService = createLanguageService(rootFileNames); + } else { + cacheFile = path.resolve(projectConfig.cachePath, '../.ts_checker_cache'); + cache = fs.existsSync(cacheFile) ? JSON.parse(fs.readFileSync(cacheFile).toString()) : {}; + const filterFiles: string[] = filterInput(rootFileNames); + languageService = createLanguageService(filterFiles); + } globalProgram.program = languageService.getProgram(); const allDiagnostics: ts.Diagnostic[] = globalProgram.program .getSyntacticDiagnostics() @@ -195,6 +217,9 @@ export class ResultStates { allDiagnostics.forEach((diagnostic: ts.Diagnostic) => { this.printDiagnostic(diagnostic); }); + if (process.env.watchMode !== 'true' && !projectConfig.xtsMode) { + fs.writeFileSync(cacheFile, JSON.stringify(cache, null, 2)); + } } }); @@ -219,6 +244,9 @@ export class ResultStates { private printDiagnostic(diagnostic: ts.Diagnostic): void { const message: string = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); if (this.validateError(message)) { + if (process.env.watchMode !== 'true' && !projectConfig.xtsMode) { + updateErrorFileCache(diagnostic); + } this.mErrorCount += 1; if (diagnostic.file) { const { line, character }: ts.LineAndCharacter = @@ -411,3 +439,38 @@ export class ResultStates { return message; } } + +function updateErrorFileCache(diagnostic: ts.Diagnostic): void { + if (diagnostic.file && cache[path.resolve(diagnostic.file.fileName)]) { + cache[path.resolve(diagnostic.file.fileName)].error = true; + } +} + +function filterInput(rootFileNames: string[]): string[] { + return rootFileNames.filter((file: string) => { + const needUpdate: NeedUpdateFlag = { flag: false }; + checkNeedUpdateFiles(path.resolve(file), needUpdate); + return needUpdate.flag; + }); +} + +function checkNeedUpdateFiles(file: string, needUpdate: NeedUpdateFlag): void { + if (needUpdate.flag) { + return; + } + + const value: CacheFileName = cache[file]; + const mtimeMs: number = fs.statSync(file).mtimeMs; + if (value) { + if (value.error || value.mtimeMs !== mtimeMs) { + needUpdate.flag = true; + return; + } + for (let i = 0; i < value.children.length; ++i) { + checkNeedUpdateFiles(value.children[i], needUpdate); + } + } else { + cache[file] = { mtimeMs, children: [], error: false }; + needUpdate.flag = true; + } +} diff --git a/compiler/src/ets_checker.ts b/compiler/src/ets_checker.ts index 587da55..8326ecc 100644 --- a/compiler/src/ets_checker.ts +++ b/compiler/src/ets_checker.ts @@ -28,7 +28,6 @@ import { } from './validate_ui_syntax'; import { INNER_COMPONENT_MEMBER_DECORATORS, - COMPONENT_IF, COMPONENT_DECORATORS_PARAMS, COMPONENT_BUILD_FUNCTION, STYLE_ADD_DOUBLE_DOLLAR, @@ -36,11 +35,14 @@ import { PROPERTIES_ADD_DOUBLE_DOLLAR, $$_BLOCK_INTERFACE } from './pre_define'; -import { JS_BIND_COMPONENTS } from './component_map'; import { getName } from './process_component_build'; import { INNER_COMPONENT_NAMES } from './component_map'; import { props } from './compile_info'; import { resolveSourceFile } from './resolve_ohm_url'; +import { + CacheFileName, + cache +} from './compile_info'; function readDeaclareFiles(): string[] { const declarationsFileNames: string[] = []; @@ -182,9 +184,32 @@ function resolveModuleNames(moduleNames: string[], containingFile: string): ts.R } } } + if (process.env.watchMode !== 'true' && !projectConfig.xtsMode) { + createOrUpdateCache(resolvedModules, containingFile); + } return resolvedModules; } +function createOrUpdateCache(resolvedModules: ts.ResolvedModuleFull[], containingFile: string): void { + const children: string[] = []; + const error: boolean = false; + resolvedModules.forEach(moduleObj => { + if (moduleObj && moduleObj.resolvedFileName && /(? { diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 269a1e6..793cb95 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -137,7 +137,7 @@ function initConfig(config) { new ResultStates() ] }); - if (!/ets_loader_ark$/.test(__dirname)) { + if (!projectConfig.xtsMode) { config.cache = { type: "filesystem", cacheDirectory: path.resolve(projectConfig.cachePath, '.ets_cache')