From ae2738e094c2d9483cafa087022d7ddfe3bc1696 Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Sun, 21 Aug 2022 22:16:33 +0800 Subject: [PATCH 1/2] houhaoyu@huawei.com output changed files Signed-off-by: houhaoyu Change-Id: I3702dbd32f24058238773dfd5895c63555765953 --- compiler/main.js | 2 ++ compiler/src/compile_info.ts | 18 ++++++++++++++++++ compiler/src/gen_abc_plugin.ts | 14 +++++++------- compiler/src/process_import.ts | 6 +++--- compiler/webpack.config.js | 3 +++ 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/compiler/main.js b/compiler/main.js index d17eeda..186d084 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -65,6 +65,8 @@ function initProjectConfig(projectConfig) { projectConfig.cachePath = projectConfig.cachePath || process.env.cachePath || path.resolve(__dirname, 'node_modules/.cache'); projectConfig.aceSoPath = projectConfig.aceSoPath || process.env.aceSoPath; + projectConfig.outChangedFileList = (projectConfig.watchMode && projectConfig.watchMode.outChangedFileList) ? + projectConfig.watchMode.outChangedFileList : path.join(projectConfig.cachePath, 'changedFileList.json'); projectConfig.xtsMode = /ets_loader_ark$/.test(__dirname); } diff --git a/compiler/src/compile_info.ts b/compiler/src/compile_info.ts index e912ea9..37632e1 100644 --- a/compiler/src/compile_info.ts +++ b/compiler/src/compile_info.ts @@ -223,6 +223,24 @@ export class ResultStates { } }); + compiler.hooks.watchRun.tap('WatchRun', (comp) => { + if (process.env.watchMode && projectConfig.outChangedFileList && (comp.modifiedFiles || comp.removedFiles) && + !(comp.modifiedFiles && [...comp.modifiedFiles].length === 1 && [...comp.modifiedFiles][0] == projectConfig.projectPath)) { + const filesObj = { + modifiedFiles: [...comp.modifiedFiles].filter((file) => { + return fs.statSync(file).isFile(); + }), + removedFiles: [...comp.removedFiles] + }; + if (fs.existsSync(path.dirname(projectConfig.outChangedFileList))) { + fs.writeFileSync(projectConfig.outChangedFileList, JSON.stringify(filesObj)); + } else { + fs.mkdirSync(path.dirname(projectConfig.outChangedFileList)); + fs.writeFileSync(projectConfig.outChangedFileList, JSON.stringify(filesObj)); + } + } + }) + compiler.hooks.done.tap('Result States', (stats: Stats) => { if (projectConfig.isPreview && projectConfig.aceSoPath && useOSFiles && useOSFiles.size > 0) { diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index b0af0f9..90c2234 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -180,7 +180,7 @@ export class GenAbcPlugin { function clearGlobalInfo() { // fix bug of multi trigger - if (!projectConfig.isPreview) { + if (!process.env.watchMode) { intermediateJsBundle = []; moduleInfos = []; } @@ -494,7 +494,7 @@ function invokeCluterModuleToAbc(): void { if (count_ === totalWorkerNumber) { writeModuleHashJson(); clearGlobalInfo(); - if (projectConfig.isPreview) { + if (process.env.watchMode) { console.info(blue, 'COMPILE RESULT:SUCCESS ', reset); } } @@ -628,7 +628,7 @@ function invokeWorkersToGenAbc(): void { if (count_ === workerNumber) { writeHashJson(); clearGlobalInfo(); - if (projectConfig.isPreview) { + if (process.env.watchMode) { console.info(red, 'COMPILE RESULT:SUCCESS ', reset); } } @@ -733,7 +733,7 @@ function writeModuleHashJson(): void { return; } // fix bug of multi trigger - if (!projectConfig.isPreview || delayCount < 1) { + if (!process.env.watchMode || delayCount < 1) { fs.writeFileSync(hashFilePath, JSON.stringify(moduleHashJsonObject)); } } @@ -778,7 +778,7 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { updateJsonObject[input] = hashInputContentData; updateJsonObject[abcPath] = hashAbcContentData; - if (!projectConfig.isPreview) { + if (!process.env.watchMode) { fs.unlinkSync(input); } } else { @@ -806,7 +806,7 @@ function writeHashJson(): void { const hashAbcContentData: any = toHashData(abcPath); hashJsonObject[input] = hashInputContentData; hashJsonObject[abcPath] = hashAbcContentData; - if (!projectConfig.isPreview) { + if (!process.env.watchMode) { fs.unlinkSync(input); } } @@ -815,7 +815,7 @@ function writeHashJson(): void { return; } // fix bug of multi trigger - if (!projectConfig.isPreview || delayCount < 1) { + if (!process.env.watchMode || delayCount < 1) { fs.writeFileSync(hashFilePath, JSON.stringify(hashJsonObject)); } } diff --git a/compiler/src/process_import.ts b/compiler/src/process_import.ts index 7f4a930..ea32b4c 100644 --- a/compiler/src/process_import.ts +++ b/compiler/src/process_import.ts @@ -195,7 +195,7 @@ function visitAllNode(node: ts.Node, sourceFile: ts.SourceFile, defaultNameFromP if (ts.isExportDeclaration(node) && node.exportClause && ts.isNamedExports(node.exportClause) && node.exportClause.elements) { node.exportClause.elements.forEach(item => { - if (projectConfig.isPreview) { + if (process.env.watchMode) { exportCollection.add((item.propertyName ? item.propertyName : item.name).escapedText.toString()); } if (item.name && ts.isIdentifier(item.name)) { @@ -225,7 +225,7 @@ function visitAllNode(node: ts.Node, sourceFile: ts.SourceFile, defaultNameFromP } if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) { - if (projectConfig.isPreview && node.exportClause && ts.isNamedExports(node.exportClause) && + if (process.env.watchMode && node.exportClause && ts.isNamedExports(node.exportClause) && node.exportClause.elements) { node.exportClause.elements.forEach(item => { exportCollection.add((item.propertyName ? item.propertyName : item.name).escapedText.toString()); @@ -293,7 +293,7 @@ function collectSpecialFunctionNode(node: ts.FunctionDeclaration, asNameFromPare function isExportEntry(node: ts.ClassDeclaration, log: LogInfo[], entryCollection: Set, exportCollection: Set, defaultCollection: Set, fileResolvePath: string, sourceFile: ts.SourceFile): void { - if (projectConfig.isPreview && node && node.decorators) { + if (process.env.watchMode && node && node.decorators) { let existExport: boolean = false; let existEntry: boolean = false; if (node.modifiers) { diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 93dad6d..e11515a 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -198,6 +198,9 @@ function setProjectConfig(envArgs) { if (envArgs.cachePath) { projectConfig.cachePath = envArgs.cachePath; } + if (envArgs.watchMode) { + projectConfig.watchMode = envArgs.watchMode; + } } function setReleaseConfig(config) { From 51b047efcb83fdf910a43fcc42910533742bb90b Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Fri, 2 Sep 2022 20:21:28 +0800 Subject: [PATCH 2/2] houhaoyu@huawei.com fix Signed-off-by: houhaoyu Change-Id: I794a458861f86b77bf42a39bc85d706d112acbf0 --- compiler/main.js | 8 +++++-- compiler/src/compile_info.ts | 42 ++++++++++++++++++++---------------- compiler/webpack.config.js | 2 +- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/compiler/main.js b/compiler/main.js index 718439c..b612a82 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -65,13 +65,17 @@ function initProjectConfig(projectConfig) { projectConfig.cachePath = projectConfig.cachePath || process.env.cachePath || path.resolve(__dirname, 'node_modules/.cache'); projectConfig.aceSoPath = projectConfig.aceSoPath || process.env.aceSoPath; - projectConfig.outChangedFileList = (projectConfig.watchMode && projectConfig.watchMode.outChangedFileList) ? - projectConfig.watchMode.outChangedFileList : path.join(projectConfig.cachePath, 'changedFileList.json'); + projectConfig.outChangedFileList = getChangedFileList(projectConfig); projectConfig.xtsMode = /ets_loader_ark$/.test(__dirname); projectConfig.localPropertiesPath = projectConfig.localPropertiesPath || process.env.localPropertiesPath projectConfig.projectProfilePath = projectConfig.projectProfilePath || process.env.projectProfilePath } +function getChangedFileList(projectConfig) { + return (projectConfig.hotReloadWatch && projectConfig.hotReloadWatch.outChangedFileList) ? + projectConfig.hotReloadWatch.outChangedFileList : path.join(projectConfig.cachePath, 'changedFileList.json'); +} + function loadEntryObj(projectConfig) { let manifest = {}; initProjectConfig(projectConfig); diff --git a/compiler/src/compile_info.ts b/compiler/src/compile_info.ts index 3fd90c9..cd35eb3 100644 --- a/compiler/src/compile_info.ts +++ b/compiler/src/compile_info.ts @@ -35,11 +35,13 @@ import { import { circularFile, mkDir, + writeFileSync } from './utils'; import { MODULE_ETS_PATH, MODULE_SHARE_PATH, BUILD_SHARE_PATH, + ESMODULE } from './pre_define'; import { createLanguageService, @@ -224,20 +226,26 @@ export class ResultStates { }); compiler.hooks.watchRun.tap('WatchRun', (comp) => { - if (process.env.watchMode && projectConfig.outChangedFileList && (comp.modifiedFiles || comp.removedFiles) && - !(comp.modifiedFiles && [...comp.modifiedFiles].length === 1 && [...comp.modifiedFiles][0] == projectConfig.projectPath)) { - const filesObj = { + if (comp.modifiedFiles) { + const isTsAndEtsFile: boolean = [...comp.modifiedFiles].some((item: string) => { + return /.(ts|ets)$/.test(item); + }); + if (!isTsAndEtsFile) { + process.env.watchTs = 'end'; + } + } + if (this.shouldWriteChangedList(comp)) { + interface filesObj { + modifiedFiles: string[], + removedFiles: string[] + } + const filesObj: filesObj = { modifiedFiles: [...comp.modifiedFiles].filter((file) => { return fs.statSync(file).isFile(); }), removedFiles: [...comp.removedFiles] }; - if (fs.existsSync(path.dirname(projectConfig.outChangedFileList))) { - fs.writeFileSync(projectConfig.outChangedFileList, JSON.stringify(filesObj)); - } else { - fs.mkdirSync(path.dirname(projectConfig.outChangedFileList)); - fs.writeFileSync(projectConfig.outChangedFileList, JSON.stringify(filesObj)); - } + writeFileSync(projectConfig.outChangedFileList, JSON.stringify(filesObj)); } }) @@ -257,17 +265,13 @@ export class ResultStates { } this.printResult(); }); + } - compiler.hooks.watchRun.tap('Listening State', (compiler: Compiler) => { - if (compiler.modifiedFiles) { - const isTsAndEtsFile: boolean = [...compiler.modifiedFiles].some((item: string) => { - return /.(ts|ets)$/.test(item); - }); - if (!isTsAndEtsFile) { - process.env.watchTs = 'end'; - } - } - }); + private shouldWriteChangedList(comp): boolean { + return projectConfig.compileMode === ESMODULE && process.env.watchMode && !projectConfig.isPreview && + projectConfig.outChangedFileList && (comp.modifiedFiles || comp.removedFiles) && + !(comp.modifiedFiles && [...comp.modifiedFiles].length === 1 && + [...comp.modifiedFiles][0] == projectConfig.projectPath); } private printDiagnostic(diagnostic: ts.Diagnostic): void { diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 9e3d360..3931740 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -200,7 +200,7 @@ function setProjectConfig(envArgs) { projectConfig.cachePath = envArgs.cachePath; } if (envArgs.watchMode) { - projectConfig.watchMode = envArgs.watchMode; + projectConfig.hotReloadWatch = envArgs.watchMode; } }