From 00640d840d7f73b8e92f124814ce1760f212108b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=9B=B9=E5=AE=87?= Date: Thu, 4 Sep 2025 17:19:37 +0800 Subject: [PATCH] =?UTF-8?q?kit=E6=B7=BB=E5=8A=A0=E5=AF=B9export=20*=20from?= =?UTF-8?q?=20=E8=AF=AD=E6=B3=95=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王曹宇 --- build-tools/delete_systemapi_plugin.js | 78 +++++++++++++++++++++----- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/build-tools/delete_systemapi_plugin.js b/build-tools/delete_systemapi_plugin.js index 0a1e65a3a9..2f584e4f5e 100644 --- a/build-tools/delete_systemapi_plugin.js +++ b/build-tools/delete_systemapi_plugin.js @@ -21,6 +21,7 @@ let sourceFile = null; let etsType = 'ets'; let componentEtsFiles = []; const componentEtsDeleteFiles = []; +const etsDeleteFiles = []; const referencesMap = new Map(); const referencesModuleMap = new Map(); const kitFileNeedDeleteMap = new Map(); @@ -156,12 +157,9 @@ function getKitNewSourceFile(sourceFile, kitName) { copyrightMessage = sourceFile.getFullText().replace(sourceFile.getText(), ''); } } else if (ts.isExportDeclaration(statement)) { - const exportSpecifiers = statement.exportClause?.elements?.filter((item) => { - return !needDeleteExportName.has(item.name.escapedText.toString()); - }); - if (exportSpecifiers && exportSpecifiers.length !== 0) { - statement.exportClause = factory.updateNamedExports(statement.exportClause, exportSpecifiers); - newStatements.push(statement); + const newStatement = processKitExportDeclaration(statement, needDeleteExportName); + if (newStatement) { + newStatements.push(newStatement); } } }); @@ -184,10 +182,10 @@ function addImportToNeedDeleteExportName(importClause, needDeleteExportName) { }); } } + /** * 根据节点和需要删除的节点数据生成新节点 * @param { ts.ImportDeclaration } statement 需要处理的import节点 - * @param { Map} needDeleteMap 需要删除的节点数据 * @param { Map} needDeleteExportName 需要删除的导出节点 * @returns { ts.ImportDeclaration | undefined } 返回新的import节点,全部删除为undefined */ @@ -198,10 +196,10 @@ function processKitImportDeclaration(statement, needDeleteExportName) { if (!ts.isImportClause(importClause)) { return statement; } - const importPath = statement.moduleSpecifier.text.replace('../', ''); + const importPath = statement.moduleSpecifier.text.toString(); if (kitFileNeedDeleteMap === undefined || !kitFileNeedDeleteMap.has(importPath)) { const hasFilePath = hasFileByImportPath(inputDir, importPath); - if (hasFilePath) { + if (hasFilePath && !etsDeleteFiles.includes(importPath)) { return statement; } addImportToNeedDeleteExportName(importClause, needDeleteExportName); @@ -247,6 +245,50 @@ function processKitImportDeclaration(statement, needDeleteExportName) { return undefined; } +/** + * 处理Kit的export节点 + * @param { ts.ExportDeclaration } statement 需要处理的export节点 + * @param { Map} needDeleteExportName 需要删除的导出节点 + * @returns { ts.ExportDeclaration | undefined } 返回新的import节点,全部删除为undefined + */ +function processKitExportDeclaration(statement, needDeleteExportName) { + const moduleSpecifier = statement.moduleSpecifier; + const exportClause = statement.exportClause; + // 初始化ts工厂 + const factory = ts.factory; + // export * from '' export {xx} from '' + if (moduleSpecifier) { + const importPath = moduleSpecifier.text.toString(); + if (kitFileNeedDeleteMap === undefined || !kitFileNeedDeleteMap.has(importPath)) { + const hasFilePath = hasFileByImportPath(inputDir, importPath); + if (hasFilePath && !etsDeleteFiles.includes(importPath)) { + return statement; + } + return undefined; + } + const currNeedDeleteInfo = kitFileNeedDeleteMap.get(importPath); + currNeedDeleteInfo.exportName.forEach(item => { + needDeleteExportName.add(item); + }); + } + if (ts.isNamedExports(exportClause)) { + // export {} + const exportSpecifiers = exportClause.elements.filter((element) => { + const exportName = element.propertyName ? + element.propertyName.escapedText.toString() : + element.name.escapedText.toString(); + return !needDeleteExportName.has(exportName); + }); + if (exportSpecifiers && exportSpecifiers.length !== 0) { + statement.exportClause = factory.updateNamedExports(statement.exportClause, exportSpecifiers); + return statement; + } + } else if (ts.isNamespaceExport(exportClause)) { + return statement; + } + return undefined; +} + /** * 判断文件路径对应的文件是否存在 * @param {string} apiDir 引用接口所在目录 @@ -310,6 +352,7 @@ function processFileName(filePath) { function processFileNameWithoutExt(filePath) { return path .basename(filePath) + .replace(/\.static\.ets$/g, '') .replace(/\.d\.ts$/g, '') .replace(/\.d\.ets$/g, '') .replace(/\.ts$/g, '') @@ -914,7 +957,7 @@ function deleteSystemApi(url) { kitName = RegExp.$1.replace(/\s/g, ''); } sourceFile = node; - const deleteNode = processSourceFile(node, kitName, url); // 处理最外层节点 + const deleteNode = processSourceFile(node, url); // 处理最外层节点 node = processVisitEachChild(context, deleteNode.node); if (!isEmptyFile(node)) { const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); @@ -1032,7 +1075,10 @@ function addNewStatements(node, newStatements, deleteSystemApiSet, needDeleteExp return; } if (ts.isVariableStatement(statement)) { - deleteSystemApiSet.add(variableStatementGetEscapedText(statement)); + const variableName = variableStatementGetEscapedText(statement); + deleteSystemApiSet.add(variableName); + needDeleteExport.fileName = processFileNameWithoutExt(node.fileName); + needDeleteExport.exportName.add(variableName); } else if ( ts.isModuleDeclaration(statement) || ts.isInterfaceDeclaration(statement) || @@ -1388,9 +1434,13 @@ function isEmptyFile(node) { break; } } - const fileName = getPureName(node.fileName.replace('.ts', '').replace('.static.ets', '').replace('.ets', '')); - if (isEmpty && componentEtsFiles.includes(fileName)) { - componentEtsDeleteFiles.push(fileName); + const fileName = node.fileName.replace('.ts', '').replace('.static.ets', '').replace('.ets', ''); + const componentFileName = getPureName(fileName); + if (isEmpty && componentEtsFiles.includes(componentFileName)) { + componentEtsDeleteFiles.push(componentFileName); + } + if (isEmpty) { + etsDeleteFiles.push(fileName); } return isEmpty; } -- Gitee