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