1/* 2* Copyright (c) 2023 Huawei Device Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use rollupObject 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 16import { 17 MODULE_ID_ROLLUP_PLACEHOLDER, 18 PROJECT_ROOT, 19 DEFAULT_PROJECT, 20 DEFAULT_ENTRY 21} from '../rollup_mock/path_config'; 22 23class Meta { 24 belongModulePath: string; 25 hostModulesInfo: Array<object>; 26 moduleName: string; 27 pkgName: string; 28 isLocalDependency: boolean; 29 isNodeEntryFile: boolean; 30 pkgPath: string; 31 dependencyPkgInfo: Object; 32 belongProjectPath: string; 33 34 constructor(entryModuleName: string, modulePath: string) { 35 this.belongModulePath = `${PROJECT_ROOT}/${DEFAULT_PROJECT}/${DEFAULT_ENTRY}`; 36 this.hostModulesInfo = []; 37 this.moduleName = entryModuleName; 38 this.pkgName = ''; 39 this.isLocalDependency = true; 40 this.isNodeEntryFile = false; 41 this.pkgPath = modulePath; 42 this.dependencyPkgInfo = undefined; 43 this.belongProjectPath = `${PROJECT_ROOT}/${DEFAULT_PROJECT}`; 44 } 45}; 46 47export class ModuleInfo { 48 meta: Meta; 49 id: string; 50 importedIdMaps: object = {}; 51 importCache = []; 52 53 constructor(id: string, entryModuleName: string, modulePath: string) { 54 this.meta = new Meta(entryModuleName, modulePath); 55 this.id = id; 56 } 57 58 setIsLocalDependency(value: boolean) { 59 this.meta.isLocalDependency = value; 60 } 61 setIsNodeEntryFile(value: boolean) { 62 this.meta.isNodeEntryFile = value; 63 } 64 65 setImportedIdMaps(path?: string) { 66 if (path) { 67 this.importedIdMaps = { 68 'requestFile': path 69 }; 70 } else { 71 this.importedIdMaps = { 72 '@ohos.app.ability.UIAbility': MODULE_ID_ROLLUP_PLACEHOLDER, 73 '@ohos.hilog': MODULE_ID_ROLLUP_PLACEHOLDER 74 }; 75 }; 76 77 } 78 79 setNodeImportDeclaration() { 80 this.importCache.push( 81 { 82 "type": "ImportDeclaration", 83 "start": 0, 84 "end": 52, 85 "specifiers": [ 86 { 87 "type": "ImportDefaultSpecifier", 88 "start": 7, 89 "end": 16, 90 "local": { 91 "type": "Identifier", 92 "start": 7, 93 "end": 16, 94 "name": "UIAbility" 95 } 96 } 97 ], 98 "source": { 99 "type": "Literal", 100 "start": 22, 101 "end": 51, 102 "value": "@ohos.app.ability.UIAbility", 103 "raw": "'@ohos.app.ability.UIAbility'" 104 } 105 }, 106 { 107 "type": "ImportDeclaration", 108 "start": 54, 109 "end": 86, 110 "specifiers": [ 111 { 112 "type": "ImportDefaultSpecifier", 113 "start": 61, 114 "end": 66, 115 "local": { 116 "type": "Identifier", 117 "start": 61, 118 "end": 66, 119 "name": "hilog" 120 } 121 } 122 ], 123 "source": { 124 "type": "Literal", 125 "start": 72, 126 "end": 85, 127 "value": "@ohos.hilog", 128 "raw": "'@ohos.hilog'" 129 } 130 } 131 ); 132 } 133 134 setNodeImportExpression() { 135 this.importCache.push( 136 { 137 "type": "ImportExpression", 138 "start": 0, 139 "end": 52, 140 "specifiers": [ 141 { 142 "type": "ImportDefaultSpecifier", 143 "start": 7, 144 "end": 16, 145 "local": { 146 "type": "Identifier", 147 "start": 7, 148 "end": 16, 149 "name": "UIAbility" 150 } 151 } 152 ], 153 "source": { 154 "type": "Literal", 155 "start": 22, 156 "end": 51, 157 "value": "@ohos/sharedLibrary", 158 "raw": "@ohos/sharedLibrary" 159 } 160 } 161 ) 162 } 163 164 getNodeByType(IMPORT_NODE: string, EXPORTNAME_NODE: string, EXPORTALL_NODE: string, DYNAMICIMPORT_NODE: string) { 165 const nodeByType = new Map(); 166 this.importCache.forEach(node => { 167 if (node.type === IMPORT_NODE) { 168 if (!nodeByType.has(node.type)) { 169 nodeByType.set(node.type, []); 170 } 171 nodeByType.get(node.type).push(node); 172 } 173 174 if (node.type === DYNAMICIMPORT_NODE) { 175 if (!nodeByType.has(node.type)) { 176 nodeByType.set(node.type, []); 177 } 178 nodeByType.get(node.type).push(node); 179 } 180 }); 181 return nodeByType; 182 } 183} 184 185export default ModuleInfo;