• 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 Cache from "./cache";
17import Share from "./share";
18import path from "path";
19import fs from "fs";
20
21import ModuleInfo from "./module_info";
22import {
23  SDK_VERSION,
24  BUNDLE_NAME_DEFAULT,
25  ETS_LOADER_VERSION,
26  RUNTIME_OS_OPENHARMONY,
27  MODULE_NAME_HASH_DEFAULT,
28  SDK_VERSION_STAGE
29} from "./common";
30import {
31  AN_BUILD_OUTPUT_PATH,
32  DEFAULT_PROJECT,
33  MODULE_ID_ROLLUP_PLACEHOLDER,
34  NODE_MODULES_PATH,
35  PROJECT_ROOT
36} from "./path_config";
37import { scanFiles } from "../../utils/utils";
38import { IArkProjectConfig } from "./project_config";
39import {
40  ESMODULE,
41  RELEASE,
42  DEBUG,
43  ARK_COMPILER_META_INFO,
44  IS_CACHE_INVALID
45} from "../../../../lib/fast_build/ark_compiler/common/ark_define";
46class RollUpPluginMock {
47  cache: Cache;
48  meta: object = { rollupVersion: '3.10.0', watchMode: false };
49  moduleIds: object;
50  share: Share;
51  moduleInfos: Array<ModuleInfo>;
52
53  private isPreview: boolean = false;
54
55  constructor() {
56    this.cache = new Cache();
57  }
58
59  public preConfig(buildMode: string = DEBUG) {
60    this.share = new Share(buildMode);
61  }
62
63  public build(buildMode: string = DEBUG) {
64    this.isPreview = false;
65    this.share = new Share(buildMode);
66
67    this.share.projectConfig.setPreview(this.isPreview);
68    this.meta.watchMode = this.isPreview;
69
70    this.doBuild(DEFAULT_PROJECT);
71  }
72
73  public chooseTestData(testcase: string) {
74    if (!this.share) {
75      throw new Error('Call build API first.');
76    }
77    this.doBuild(testcase);
78  }
79
80  public preview(testcase: string = DEFAULT_PROJECT) {
81    this.isPreview = true;
82    this.share = new Share(DEBUG);
83
84    this.share.projectConfig.setPreview(this.isPreview);
85    this.meta.watchMode = this.isPreview;
86
87    this.doBuild(testcase);
88  }
89
90  public coldReload(buildMode: string = DEBUG) {
91    this.isPreview = false;
92    this.share = new Share(buildMode);
93
94    this.share.projectConfig.setPreview(this.isPreview);
95    this.meta.watchMode = false;
96
97    this.doBuild(DEFAULT_PROJECT);
98  }
99
100  public hotReload(testcase: string = DEFAULT_PROJECT) {
101    this.isPreview = false;
102    this.share = new Share(DEBUG);
103
104    this.share.projectConfig.setPreview(this.isPreview);
105    this.meta.watchMode = this.isPreview;
106
107    this.doBuild(testcase);
108  }
109
110  public useNormalizedOHMUrl() {
111    this.share.projectConfig.useNormalizedOHMUrl = true;
112  }
113
114  public mockCompileContextInfo() {
115    this.share.projectConfig.mockCompileContextInfo();
116    let entryObj: object = this.share.projectConfig.entryObj;
117    if (!!this.share.projectConfig.widgetCompile) {
118      entryObj = this.share.projectConfig.cardEntryObj
119    }
120    for(let entry in entryObj) {
121      let filePath: string = entryObj[entry];
122      let moduleInfo: ModuleInfo = new ModuleInfo(filePath, 'entry', `${PROJECT_ROOT}/entry`);
123      moduleInfo.meta.pkgName = 'entry';
124      this.moduleInfos.push(moduleInfo);
125    }
126  }
127
128  private doBuild(testcase: string) {
129    this.share.scan(testcase);
130    this.load();
131
132    // mock ets-loader build start
133    this.share.arkProjectConfig = this.mockArkProjectConfig();
134    this.mockCheckArkCompilerCacheInfo();
135  }
136
137  private mockArkProjectConfig(): IArkProjectConfig {
138    const mode = this.isPreview ? 'preview' : 'build';
139    const projectRootDir = this.share.projectConfig.projectTopDir;
140    const entryName = this.share.projectConfig.entryModuleName;
141
142    return {
143      projectRootPath: projectRootDir,
144      modulePathMap: { entry: `${projectRootDir}/${entryName}` },
145      isOhosTest: undefined,
146      processTs: false,
147      pandaMode: undefined,
148      nodeModulesPath: `${projectRootDir}/${entryName}/${mode}/${NODE_MODULES_PATH}`,
149      harNameOhmMap: {},
150      minPlatformVersion: SDK_VERSION,
151      moduleName: `${entryName}`,
152      bundleName: BUNDLE_NAME_DEFAULT,
153      hotReload: undefined,
154      patchAbcPath: undefined,
155      changedFileList: undefined,
156      compileMode: ESMODULE,
157      anBuildOutPut: `${projectRootDir}/${entryName}/${mode}/${AN_BUILD_OUTPUT_PATH}`,
158      processTs: true
159    }
160  }
161
162  private mockCheckArkCompilerCacheInfo(): void {
163    const metaInfos = [
164      SDK_VERSION,
165      SDK_VERSION,
166      SDK_VERSION_STAGE,
167      RUNTIME_OS_OPENHARMONY,
168      `/OpenHarmony/Sdk/${SDK_VERSION}/ets/build-tools/app`,
169      ETS_LOADER_VERSION,
170      RELEASE,
171      BUNDLE_NAME_DEFAULT,
172      MODULE_NAME_HASH_DEFAULT,
173      'null_aotCompileMode',
174      'null_apPath'
175    ]
176    const metaInfo = metaInfos.join(':');
177    this.cache.set(IS_CACHE_INVALID, true);
178    this.cache.set(ARK_COMPILER_META_INFO, metaInfo);
179  }
180
181  public addWatchFile() { }
182
183  public async(func: Function) {
184    if (func) {
185      func();
186    }
187  }
188
189  public block() { }
190
191  public emitFile() { }
192
193  public error() { }
194
195  public getFileName() { }
196
197  public getModuleIds(): IterableIterator<string> {
198    return this.share.allFiles ? this.share.allFiles.values() : undefined;
199  }
200
201  public getModuleInfo(id: string): ModuleInfo | undefined {
202    for (let i = 0; i < this.moduleInfos.length - 1; i++) {
203      return this.moduleInfos.find(item => item.id === id);
204    }
205  }
206
207  public getWatchFiles() { }
208
209  public load() {
210    // load project files list
211    this.share.allFiles = new Set<string>();
212    if (fs.existsSync(this.share.projectConfig.projectPath)) {
213      scanFiles(this.share.projectConfig.projectPath, this.share.allFiles);
214    } else {
215      const tsFilePath = path.join(this.share.projectConfig.projectPath, '/entryability/EntryAbility.ts');
216      const jsFilePath = path.join(this.share.projectConfig.projectPath, '/entryability/EntryAbility.js');
217      const etsFilePath = path.join(this.share.projectConfig.projectPath, '/pages/Index.ets');
218      this.share.allFiles.add(tsFilePath);
219      this.share.allFiles.add(jsFilePath);
220      this.share.allFiles.add(etsFilePath);
221    }
222    this.share.allFiles.add(MODULE_ID_ROLLUP_PLACEHOLDER);
223
224    // load all files module info
225    const allFiles = Array.from(this.share.allFiles);
226    this.moduleInfos = new Array<ModuleInfo>();
227    for (let i = 0; i < allFiles.length - 1; i++) {
228      this.moduleInfos.push(new ModuleInfo(allFiles[i],
229        this.share.projectConfig.entryModuleName,
230        this.share.projectConfig.modulePath));
231    }
232  }
233
234  public parse() { }
235
236  public resolve() { }
237
238  public setAssetSource() { }
239
240  public signal() { }
241
242  public warn() { }
243
244  public clearCache() {
245    this.cache.set(IS_CACHE_INVALID, false);
246    this.cache.set(ARK_COMPILER_META_INFO, undefined);
247  }
248
249  public mockProjectConfigMockParams() {
250    this.share.setMockParams();
251  }
252}
253
254export default RollUpPluginMock;
255