1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17const path = require('path'); 18 19module.exports = function (babel) { 20 const { types: t } = babel; 21 22 return { 23 name: 'custom-import-plugin', 24 visitor: { 25 ImportDeclaration(pathNode) { 26 const sourceValue = pathNode.node.source.value; 27 if (sourceValue === '@koalaui/libarkts' && pathNode.node.specifiers.length === 1 && t.isImportNamespaceSpecifier(pathNode.node.specifiers[0])) { 28 const currentFileDir = path.dirname(pathNode.hub.file.opts.filename); 29 const configDir = process.cwd(); 30 const relativePath = path.relative(currentFileDir, configDir); 31 const importPath = relativePath ? path.join(relativePath, 'path') : './path'; 32 33 const newImport = t.importDeclaration( 34 [t.importSpecifier(t.identifier('getArktsPath'), t.identifier('getArktsPath'))], 35 t.stringLiteral(importPath) 36 ); 37 38 const requireCall = t.callExpression(t.identifier('require'), [t.callExpression(t.identifier('getArktsPath'), [])]); 39 const arkts = t.variableDeclaration('const', [ 40 t.variableDeclarator(t.identifier('arkts'), requireCall) 41 ]); 42 43 pathNode.replaceWithMultiple([newImport, arkts]); 44 } 45 } 46 } 47 }; 48};