• 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 fs from 'fs';
17import path from 'path';
18import {
19  COMMONJS,
20  ESM,
21  EXTNAME_PROTO_BIN,
22  EXTNAME_JS,
23  EXTNAME_TS,
24  EXTNAME_ETS
25} from '../../../../lib/fast_build/ark_compiler/common/ark_define';
26import {
27  ModuleMode,
28  PackageEntryInfo
29} from '../../../../lib/fast_build/ark_compiler/module/module_mode';
30import {
31  getNormalizedOhmUrlByFilepath,
32  transformOhmurlToPkgName,
33  transformOhmurlToRecordName
34} from '../../../../lib/ark_utils';
35import { changeFileExtension } from '../../../../lib/fast_build/ark_compiler/utils';
36import { toUnixPath } from '../../../../lib/utils';
37import { META } from '../rollup_mock/common';
38import { sharedModuleSet } from '../../../../lib/fast_build/ark_compiler/check_shared_module';
39import { SourceMapGenerator } from '../../../../lib/fast_build/ark_compiler/generate_sourcemap';
40class ModuleModeMock extends ModuleMode {
41  collectModuleFileListMock(rollupObject: object) {
42    const fileList = Array.from(rollupObject.getModuleIds());
43    this.collectModuleFileList(rollupObject, fileList);
44  }
45
46  addModuleInfoItemMock(rollupObject: object, isCommonJs: boolean, extName: string) {
47    const mockFileList = rollupObject.getModuleIds();
48    for (const filePath of mockFileList) {
49      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
50        const moduleInfo: object = rollupObject.getModuleInfo(filePath);
51        const metaInfo: object = moduleInfo[META];
52        this.addModuleInfoItem(filePath, isCommonJs, extName, metaInfo, this.moduleInfos);
53      }
54    }
55  }
56
57  addSourceMapMock(rollupObject: object, sourceMapGenerator: SourceMapGenerator) {
58    for (const filePath of rollupObject.getModuleIds()) {
59      const isValidSuffix: boolean =
60        filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS);
61      if (!isValidSuffix)
62        continue;
63      if (sourceMapGenerator.isNewSourceMaps()) {
64        sourceMapGenerator.updateSourceMap(filePath, {});
65      } else {
66        const filePathCache: string = this.genFileCachePath(
67          filePath, this.projectConfig.projectRootPath, this.projectConfig.cachePath)
68        sourceMapGenerator.updateSourceMap(
69          filePathCache.replace(this.projectConfig.projectRootPath + path.sep, ''), {});
70      }
71    }
72  }
73
74  static getModuleInfosAndSourceMapMock(rollupObject: object, sourceMapGenerator: SourceMapGenerator) {
75    const moduleMode = new ModuleModeMock(rollupObject);
76    moduleMode.addSourceMapMock(rollupObject, sourceMapGenerator);
77    moduleMode.addModuleInfoItemMock(rollupObject, false, '');
78    return { moduleInfos: moduleMode.moduleInfos, sourceMap: sourceMapGenerator.getSourceMaps() };
79  }
80
81  generateCompileFilesInfoMock(includeByteCodeHarInfo: boolean) {
82    this.generateCompileFilesInfo(includeByteCodeHarInfo);
83  }
84
85  generateNpmEntriesInfoMock() {
86    this.generateNpmEntriesInfo();
87  }
88
89  generateAbcCacheFilesInfoMock() {
90    this.generateAbcCacheFilesInfo();
91  }
92
93  generateCompileContextInfoMock(rollupObject: object): void {
94      this.compileContextInfoPath = this.generateCompileContextInfo(rollupObject);
95  }
96
97  checkGenerateCompileContextInfo(rollupObject: object): boolean {
98    const cacheCompileContextInfo = fs.readFileSync(this.compileContextInfoPath, 'utf-8');
99
100    let compileContextInfo: Object = {};
101    let hspPkgNames: Array<string> = [];
102    for (const hspName in rollupObject.share.projectConfig.hspNameOhmMap) {
103      let hspPkgName: string = hspName;
104      if (rollupObject.share.projectConfig.dependencyAliasMap.has(hspName)) {
105        hspPkgName = rollupObject.share.projectConfig.dependencyAliasMap.get(hspName);
106      }
107      hspPkgNames.push(toUnixPath(hspPkgName));
108    }
109    compileContextInfo.hspPkgNames = hspPkgNames;
110    let compileEntries: Set<string> = new Set();
111    let entryObj: Object = this.projectConfig.entryObj;
112    if (this.projectConfig.widgetCompile) {
113      entryObj = this.projectConfig.cardEntryObj;
114    }
115    for (const key in entryObj) {
116      let moduleId: string = entryObj[key];
117      let moduleInfo: Object = rollupObject.getModuleInfo(moduleId);
118      let metaInfo: Object = moduleInfo.meta;
119      const pkgParams = {
120        pkgName: metaInfo.pkgName,
121        pkgPath: metaInfo.pkgPath,
122        isRecordName: true
123      };
124      let recordName: string = getNormalizedOhmUrlByFilepath(moduleId, rollupObject.share.projectConfig,
125        rollupObject.share.logger, pkgParams, undefined);
126      compileEntries.add(recordName);
127    }
128    this.collectDeclarationFilesEntry(compileEntries, hspPkgNames);
129    compileContextInfo.compileEntries = Array.from(compileEntries);
130    if (rollupObject.share.projectConfig.updateVersionInfo) {
131      compileContextInfo.updateVersionInfo = this.projectConfig.updateVersionInfo;
132    } else if (rollupObject.share.projectConfig.pkgContextInfo) {
133      compileContextInfo.pkgContextInfo = this.projectConfig.pkgContextInfo;
134    }
135    if (JSON.stringify(compileContextInfo) === cacheCompileContextInfo) {
136      return true;
137    }
138    return false;
139  }
140
141  checkGenerateCompileFilesInfo(includeByteCodeHarInfo: boolean): boolean {
142    let mockfilesInfo: string = '';
143    const filesInfo = fs.readFileSync(this.filesInfoPath, 'utf-8');
144    this.moduleInfos.forEach((info) => {
145      const moduleType: string = info.isCommonJs ? COMMONJS : ESM;
146      const isSharedModule: boolean = sharedModuleSet.has(info.filePath);
147      mockfilesInfo +=
148        `${info.cacheFilePath};${info.recordName};${moduleType};${info.sourceFile};${info.packageName};` +
149        `${isSharedModule}\n`;
150    });
151    if (includeByteCodeHarInfo) {
152      Object.entries(this.projectConfig.byteCodeHarInfo).forEach(([pkgName, abcInfo]) => {
153        const abcPath: string = toUnixPath(abcInfo.abcPath);
154        mockfilesInfo += `${abcPath};;;;${pkgName};\n`;
155      });
156    }
157    if (filesInfo === mockfilesInfo) {
158      return true;
159    }
160    return false;
161  }
162
163  checkGenerateNpmEntriesInfo(): boolean {
164    let mockentriesInfo: string = '';
165    const filesInfo = fs.readFileSync(this.npmEntriesInfoPath, 'utf-8');
166    for (const value of this.pkgEntryInfos.values()) {
167      mockentriesInfo += `${value.pkgEntryPath}:${value.pkgBuildPath}\n`;
168    }
169    if (filesInfo === mockentriesInfo) {
170      return true;
171    }
172    return false;
173  }
174
175  checkGenerateAbcCacheFilesInfo(): boolean {
176    let mockabcCacheFilesInfo: string = '';
177    const filesInfo = fs.readFileSync(this.cacheFilePath, 'utf-8');
178    this.moduleInfos.forEach((info) => {
179      const abcCacheFilePath: string = changeFileExtension(info.cacheFilePath, EXTNAME_PROTO_BIN);
180      mockabcCacheFilesInfo += `${info.cacheFilePath};${abcCacheFilePath}\n`;
181    });
182
183    const npmEntriesCacheFilePath: string = changeFileExtension(this.npmEntriesInfoPath, EXTNAME_PROTO_BIN);
184    mockabcCacheFilesInfo += `${this.npmEntriesInfoPath};${npmEntriesCacheFilePath}\n`;
185
186    if (filesInfo === mockabcCacheFilesInfo) {
187      return true;
188    }
189    return false;
190  }
191
192  checkGetPackageEntryInfo(rollup: object, isTestErrorLog: boolean = false) {
193    this.pkgEntryInfos = new Map<String, PackageEntryInfo>();
194    const mockFileList = rollup.getModuleIds();
195    for (const filePath of mockFileList) {
196      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
197        const moduleInfos = rollup.getModuleInfo(filePath);
198        moduleInfos.setIsLocalDependency(false);
199        moduleInfos.setIsNodeEntryFile(true);
200        const metaInfo: object = moduleInfos[META];
201        if (isTestErrorLog) {
202          metaInfo['pkgPath'] = '';
203        }
204        this.getPackageEntryInfo(filePath, metaInfo, this.pkgEntryInfos);
205      }
206    }
207  }
208
209  updateCachedSourceMapsMock(sourceMapGenerator: Object) {
210    sourceMapGenerator.updateCachedSourceMaps();
211  }
212
213  buildModuleSourceMapInfoMock(sourceMapGenerator: Object) {
214    sourceMapGenerator.buildModuleSourceMapInfo();
215  }
216
217  checkModuleSourceMapInfoMock(): boolean {
218    const readSourceMap = fs.readFileSync(this.sourceMapPath, 'utf-8');
219    const readCacheSourceMap = fs.readFileSync(this.cacheSourceMapPath, 'utf-8');
220    if (readSourceMap.length == 0 && readCacheSourceMap.length == 0) {
221      return true;
222    } else if (readSourceMap === readCacheSourceMap) {
223      return true;
224    } else {
225      return false;
226    }
227  }
228
229  generateMergedAbcOfEs2AbcMock(parentEvent: Object) {
230    this.generateMergedAbcOfEs2Abc(parentEvent)
231  }
232
233  filterModulesByHashJsonMock() {
234    this.filterModulesByHashJson();
235  }
236
237  invokeTs2AbcWorkersToGenProtoMock(splittedModules: Object) {
238    this.invokeTs2AbcWorkersToGenProto(splittedModules)
239  }
240
241
242}
243
244export default ModuleModeMock;