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