From 65cdb60a3ec2f57e7894b6e4bf59cad069cede70 Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Fri, 10 Jun 2022 23:54:18 +0800 Subject: [PATCH] houhaoyu@huawei.com optimize import Signed-off-by: houhaoyu Change-Id: I15d1935cffab262f6d74a98ea500a9f34a4163a9 --- compiler/src/component_map.ts | 2 ++ compiler/src/process_import.ts | 29 ++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/compiler/src/component_map.ts b/compiler/src/component_map.ts index fe9f600..a1f37a9 100644 --- a/compiler/src/component_map.ts +++ b/compiler/src/component_map.ts @@ -95,6 +95,8 @@ export const NEEDPOP_COMPONENT: Set = new Set(['Blank', 'Search']); export const CUSTOM_BUILDER_PROPERTIES: Set = new Set(['bindPopup', 'bindMenu', 'bindContextMenu', 'title', 'menus', 'toolBar', 'tabBar', 'onDragStart', 'onItemDragStart']); +export const IMPORT_FILE_ASTCACHE: Map = new Map(); + (function initComponent() { Object.keys(COMPONENT_MAP).forEach((componentName) => { INNER_COMPONENT_NAMES.add(componentName); diff --git a/compiler/src/process_import.ts b/compiler/src/process_import.ts index eb77fab..fb12c96 100644 --- a/compiler/src/process_import.ts +++ b/compiler/src/process_import.ts @@ -44,6 +44,7 @@ import { IComponentSet, builderParamObjectCollection } from './validate_ui_syntax'; +import { IMPORT_FILE_ASTCACHE } from './component_map'; import { LogInfo, LogType } from './utils'; import { projectConfig } from '../main'; import { isOhmUrl, resolveSourceFile } from './resolve_ohm_url'; @@ -101,13 +102,18 @@ export default function processImport(node: ts.ImportDeclaration | ts.ImportEqua fileResolvePath = getFileResolvePath(fileResolvePath, pagesDir, filePath, projectConfig.projectPath); } if (fs.existsSync(fileResolvePath) && fs.statSync(fileResolvePath).isFile()) { - const content: string = preprocessNewExtend(preprocessExtend(processSystemApi( - fs.readFileSync(fileResolvePath, { encoding: 'utf-8' }).replace( - new RegExp('\\b' + STRUCT + '\\b.+\\{', 'g'), item => { - return item.replace(new RegExp('\\b' + STRUCT + '\\b', 'g'), `${CLASS} `); - })))); - const sourceFile: ts.SourceFile = ts.createSourceFile(filePath, content, - ts.ScriptTarget.Latest, true, ts.ScriptKind.TS); + let sourceFile: ts.SourceFile + if (IMPORT_FILE_ASTCACHE.has(fileResolvePath)) { + sourceFile = IMPORT_FILE_ASTCACHE.get(fileResolvePath); + } else { + const content: string = preprocessNewExtend(preprocessExtend(processSystemApi( + fs.readFileSync(fileResolvePath, { encoding: 'utf-8' }).replace( + new RegExp('\\b' + STRUCT + '\\b.+\\{', 'g'), item => { + return item.replace(new RegExp('\\b' + STRUCT + '\\b', 'g'), `${CLASS} `); + })))); + sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS); + IMPORT_FILE_ASTCACHE[fileResolvePath] = sourceFile; + } visitAllNode(sourceFile, defaultName, asName, path.dirname(fileResolvePath), log, new Set(), new Set(), new Set(), new Map()); } @@ -212,19 +218,24 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa processImport(node, pagesDir, log, asNameFromParent); } if (ts.isImportDeclaration(node)) { - if (node.importClause && node.importClause.name && ts.isIdentifier(node.importClause.name)) { + if (node.importClause && node.importClause.name && ts.isIdentifier(node.importClause.name) && + asNameFromParent.has(node.importClause.name.getText())) { processImport(node, pagesDir, log, asNameFromParent, false); } else if (node.importClause && node.importClause.namedBindings && ts.isNamedImports(node.importClause.namedBindings) && node.importClause.namedBindings.elements) { + let nested: boolean = false; node.importClause.namedBindings.elements.forEach(item => { if (item.name && ts.isIdentifier(item.name) && asNameFromParent.has(item.name.escapedText.toString())) { + nested = true; if (item.propertyName && ts.isIdentifier(item.propertyName)) { asNameFromParent.set(item.propertyName.escapedText.toString(), asNameFromParent.get(item.name.escapedText.toString())); } } }); - processImport(node, pagesDir, log, asNameFromParent, false); + if (nested) { + processImport(node, pagesDir, log, asNameFromParent, false); + } } } node.getChildren().reverse().forEach((item: ts.Node) => visitAllNode(item, defaultNameFromParent,