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 { expect } from 'chai'; 17import mocha from 'mocha'; 18import fs from "fs"; 19import path from "path"; 20import MagicString from 'magic-string'; 21import sinon from 'sinon'; 22 23import { 24 needAotCompiler, 25 shouldETSOrTSFileTransformToJS, 26 changeFileExtension, 27 isAotMode, 28 isDebug, 29 isCommonJsPluginVirtualFile, 30 isCurrentProjectFiles, 31 isSpecifiedExt, 32 isTsOrEtsSourceFile, 33 isJsSourceFile, 34 isJsonSourceFile, 35 utUtils, 36 updateSourceMap, 37 hasTsNoCheckOrTsIgnoreFiles, 38 compilingEtsOrTsFiles, 39 cleanUpFilesList 40} from '../../../lib/fast_build/ark_compiler/utils'; 41import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock'; 42import { ModuleInfo } from '../mock/rollup_mock/module_info'; 43import { 44 AOT_FULL, 45 AOT_PARTIAL, 46 AOT_TYPE 47} from '../../../lib/pre_define'; 48import { 49 ESMODULE, 50 JSBUNDLE, 51 EXTNAME_JS, 52 EXTNAME_TS, 53 EXTNAME_ETS, 54 EXTNAME_JSON, 55 RELEASE, 56 DEBUG, 57 GEN_ABC_PLUGIN_NAME 58} from '../../../lib/fast_build/ark_compiler/common/ark_define'; 59import ModuleSourceFileMock from '../mock/class_mock/module_source_files_mock'; 60import { 61 genTemporaryPath, 62 toUnixPath, 63 getProjectRootPath, 64 getBelongModuleInfo 65} from '../../../lib/utils'; 66import projectConfig from '../utils/processProjectConfig'; 67import { 68 TEST_TS, 69 TEST_JS, 70 TEST_ETS, 71 TEST_JSON, 72 PROJECT_ROOT, 73 DEFAULT_PROJECT 74} from '../mock/rollup_mock/path_config'; 75import { scanFiles } from "../utils/utils"; 76import { SourceMapGenerator } from '../../../lib/fast_build/ark_compiler/generate_sourcemap'; 77import { ModuleSourceFile } from '../../../lib/fast_build/ark_compiler/module/module_source_file'; 78import { 79 ENTRY_PACKAGE_INFO, 80 FILE, 81 SOURCE, 82 DYNAMICIMPORT_ETS, 83 UPDATESOURCEMAP 84} from '../mock/rollup_mock/common'; 85import { 86 moduleResolutionHostTest 87} from '../../../lib/ets_checker'; 88import { 89 getRollupCache, 90 setRollupCache 91} from '../../../lib/utils'; 92import { 93 ArkObfuscator 94} from 'arkguard'; 95 96mocha.describe('test utils file api', function () { 97 mocha.before(function () { 98 this.rollup = new RollUpPluginMock(); 99 }); 100 101 mocha.after(() => { 102 delete this.rollup; 103 }); 104 105 mocha.it('1-1: test needAotCompiler under build debug', function () { 106 this.rollup.build(); 107 const returnInfo = needAotCompiler(this.rollup.share.projectConfig); 108 expect(returnInfo).to.be.false; 109 }); 110 111 mocha.it('1-2: test needAotCompiler under build debug and anBuildMode is AOT_PARTIAL', function () { 112 this.rollup.build(); 113 this.rollup.share.projectConfig.compileMode = ESMODULE; 114 this.rollup.share.projectConfig.anBuildMode = AOT_PARTIAL; 115 const returnInfo = needAotCompiler(this.rollup.share.projectConfig); 116 expect(returnInfo).to.be.true; 117 }); 118 119 mocha.it('1-3: test needAotCompiler under build release', function () { 120 this.rollup.build(RELEASE); 121 this.rollup.share.projectConfig.compileMode = ESMODULE; 122 this.rollup.share.projectConfig.anBuildMode = AOT_FULL; 123 const returnInfo = needAotCompiler(this.rollup.share.projectConfig); 124 expect(returnInfo).to.be.true; 125 }); 126 127 mocha.it('1-4: test needAotCompiler under build release and compileMode is JSBUNDLE', function () { 128 this.rollup.build(RELEASE); 129 this.rollup.share.projectConfig.compileMode = JSBUNDLE; 130 this.rollup.share.projectConfig.anBuildMode = AOT_FULL; 131 const returnInfo = needAotCompiler(this.rollup.share.projectConfig); 132 expect(returnInfo).to.be.false; 133 }); 134 135 mocha.it('1-5: test needAotCompiler under preview debug', function () { 136 this.rollup.preview(); 137 this.rollup.share.projectConfig.compileMode = JSBUNDLE; 138 this.rollup.share.projectConfig.anBuildMode = AOT_PARTIAL; 139 const buildModeIsAotFull = needAotCompiler(this.rollup.share.projectConfig); 140 expect(buildModeIsAotFull === false).to.be.true; 141 }); 142 143 mocha.it('1-6: test needAotCompiler under preview debug and anBuildMode is AOT_TYPE', function () { 144 this.rollup.preview(); 145 this.rollup.share.projectConfig.compileMode = JSBUNDLE; 146 this.rollup.share.projectConfig.anBuildMode = AOT_TYPE; 147 const buildModeIsAotFull = needAotCompiler(this.rollup.share.projectConfig); 148 expect(buildModeIsAotFull === false).to.be.true; 149 }); 150 151 mocha.it('1-7: test needAotCompiler under hot reload debug', function () { 152 this.rollup.hotReload(); 153 this.rollup.share.projectConfig.compileMode = ESMODULE; 154 this.rollup.share.projectConfig.anBuildMode = AOT_TYPE; 155 const buildModeIsAotType = needAotCompiler(this.rollup.share.projectConfig); 156 expect(buildModeIsAotType === false).to.be.true; 157 }); 158 159 mocha.it('2-1-1: test shouldETSOrTSFileTransformToJS under build debug: arkProjectConfig.processTs is false', function () { 160 this.rollup.build(); 161 this.mockfileList = this.rollup.getModuleIds(); 162 this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath; 163 this.rollup.share.arkProjectConfig.processTs = false; 164 for (const filePath of this.mockfileList) { 165 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 166 expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true; 167 } 168 } 169 this.rollup.share.arkProjectConfig.processTs = true; 170 }); 171 172 mocha.it('2-1-2: test shouldETSOrTSFileTransformToJS under build debug: arkProjectConfig.processTs is true', function () { 173 this.rollup.build(); 174 this.mockfileList = this.rollup.getModuleIds(); 175 this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath; 176 for (const filePath of this.mockfileList) { 177 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 178 compilingEtsOrTsFiles.push(filePath); 179 hasTsNoCheckOrTsIgnoreFiles.push(filePath); 180 expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true; 181 } 182 } 183 }); 184 185 mocha.it('2-2: test shouldETSOrTSFileTransformToJS under build release', function () { 186 this.rollup.build(RELEASE); 187 this.mockfileList = this.rollup.getModuleIds(); 188 this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath; 189 for (const filePath of this.mockfileList) { 190 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 191 expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true; 192 } 193 } 194 }); 195 196 mocha.it('2-3: test shouldETSOrTSFileTransformToJS under preview debug', function () { 197 this.rollup.preview(); 198 this.mockfileList = this.rollup.getModuleIds(); 199 this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath; 200 for (const filePath of this.mockfileList) { 201 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 202 expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true; 203 } 204 } 205 }); 206 207 mocha.it('2-4: test shouldETSOrTSFileTransformToJS under hot reload debug', function () { 208 this.rollup.hotReload(); 209 this.mockfileList = this.rollup.getModuleIds(); 210 this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath; 211 for (const filePath of this.mockfileList) { 212 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 213 expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true; 214 } 215 } 216 }); 217 218 mocha.it('2-5: test shouldETSOrTSFileTransformToJS under build release and enable obuscate file name', function () { 219 this.rollup.build(RELEASE); 220 this.mockfileList = this.rollup.getModuleIds(); 221 this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath; 222 223 this.rollup.share.projectConfig = { 224 buildMode: 'Release' 225 } 226 this.rollup.share.projectConfig.obfuscationMergedObConfig = { 227 options: { 228 enableFileNameObfuscation: true 229 } 230 }; 231 this.rollup.share.arkProjectConfig.processTs = true; 232 const projectConfig: Object = Object.assign(this.rollup.share.arkProjectConfig, this.rollup.share.projectConfig); 233 234 const currentDir = path.dirname(__dirname); 235 const testMainDir = path.join(currentDir, "../../test/ark_compiler_ut/testdata/testcase_def/entry/build/entry/src/main"); 236 let reservedFileNamesDirectories = testMainDir.split("/").filter(directory => directory !== ""); 237 reservedFileNamesDirectories = reservedFileNamesDirectories.concat(["pages", "entryability"]); 238 239 const arkguardConfig = { 240 mRenameFileName: { 241 mEnable: true, 242 mNameGeneratorType: 1, 243 mReservedFileNames: reservedFileNamesDirectories 244 }, 245 mPerformancePrinter: [] 246 } 247 248 let arkObfuscator: ArkObfuscator = new ArkObfuscator(); 249 arkObfuscator.init(arkguardConfig); 250 251 let index = 0 252 for (const filePath of this.mockfileList) { 253 cleanUpFilesList(); 254 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 255 if (index == 0) { 256 const directory = path.dirname(filePath); 257 const tempPath = path.join(directory, "../../../build/entry/src/main/entryability") 258 const bFilePath = path.join(tempPath, 'b.js'); 259 fs.writeFileSync(bFilePath, 'console.log("b.js created");'); 260 expect(bFilePath !== filePath).to.be.true; 261 expect(shouldETSOrTSFileTransformToJS(filePath, projectConfig) === true).to.be.true; 262 fs.unlinkSync(bFilePath); 263 } 264 } 265 index++; 266 } 267 }); 268 269 mocha.it('3-1: test writeFileContent under build debug', function () { 270 this.rollup.build(); 271 const mockFileList: object = this.rollup.getModuleIds(); 272 for (const moduleId of mockFileList) { 273 if (moduleId.endsWith(EXTNAME_TS) || moduleId.endsWith(EXTNAME_ETS) || moduleId.endsWith(EXTNAME_JS)) { 274 const code: string = fs.readFileSync(moduleId, 'utf-8'); 275 const metaInfo: Object = this.rollup.getModuleInfo(moduleId).meta; 276 const moduleSource = new ModuleSourceFileMock(moduleId, code, metaInfo); 277 moduleSource.initPluginEnvMock(this.rollup); 278 const filePath = genTemporaryPath(moduleSource.moduleId, moduleSource.projectConfig.projectPath, 279 moduleSource.projectConfig.cachePath, moduleSource.projectConfig, moduleSource.metaInfo); 280 utUtils.writeFileContent(moduleSource.moduleId, filePath, moduleSource.source, 281 moduleSource.projectConfig, moduleSource.logger); 282 const newFilePath = changeFileExtension(filePath, EXTNAME_JS); 283 const readFilecontent = fs.readFileSync(newFilePath, 'utf-8'); 284 expect(readFilecontent === moduleSource.source).to.be.true; 285 } 286 } 287 }); 288 289 mocha.it('3-2: test writeFileContent under build release', function () { 290 this.rollup.build(RELEASE); 291 SourceMapGenerator.initInstance(this.rollup); 292 const mockFileList: object = this.rollup.getModuleIds(); 293 for (const moduleId of mockFileList) { 294 if (moduleId.endsWith(EXTNAME_TS) || moduleId.endsWith(EXTNAME_ETS) || moduleId.endsWith(EXTNAME_JS)) { 295 const code: string = fs.readFileSync(moduleId, 'utf-8'); 296 const metaInfo: Object = this.rollup.getModuleInfo(moduleId).meta; 297 const moduleSource = new ModuleSourceFileMock(moduleId, code, metaInfo); 298 moduleSource.initPluginEnvMock(this.rollup); 299 const filePath = genTemporaryPath(moduleSource.moduleId, moduleSource.projectConfig.projectPath, 300 moduleSource.projectConfig.cachePath, moduleSource.projectConfig, moduleSource.metaInfo); 301 utUtils.writeFileContent(moduleSource.moduleId, filePath, moduleSource.source, 302 moduleSource.projectConfig, moduleSource.logger); 303 const newFilePath = changeFileExtension(filePath, EXTNAME_JS); 304 const readFilecontent = fs.readFileSync(newFilePath, 'utf-8'); 305 expect(readFilecontent === moduleSource.source).to.be.true; 306 } 307 } 308 SourceMapGenerator.cleanSourceMapObject(); 309 }); 310 311 mocha.it('3-3: test writeFileContent under preview debug', function () { 312 this.rollup.preview(); 313 const mockFileList: object = this.rollup.getModuleIds(); 314 for (const moduleId of mockFileList) { 315 if (moduleId.endsWith(EXTNAME_TS) || moduleId.endsWith(EXTNAME_ETS) || moduleId.endsWith(EXTNAME_JS)) { 316 const code: string = fs.readFileSync(moduleId, 'utf-8'); 317 const metaInfo: Object = this.rollup.getModuleInfo(moduleId).meta; 318 const moduleSource = new ModuleSourceFileMock(moduleId, code, metaInfo); 319 moduleSource.initPluginEnvMock(this.rollup); 320 const filePath = genTemporaryPath(moduleSource.moduleId, moduleSource.projectConfig.projectPath, 321 moduleSource.projectConfig.cachePath, moduleSource.projectConfig, moduleSource.metaInfo); 322 utUtils.writeFileContent(moduleSource.moduleId, filePath, moduleSource.source, 323 moduleSource.projectConfig, moduleSource.logger); 324 const newFilePath = changeFileExtension(filePath, EXTNAME_JS); 325 const readFilecontent = fs.readFileSync(newFilePath, 'utf-8'); 326 expect(readFilecontent === moduleSource.source).to.be.true; 327 } 328 } 329 }); 330 331 mocha.it('3-4: test writeFileContent under hot reload debug', function () { 332 this.rollup.hotReload(); 333 const mockFileList: object = this.rollup.getModuleIds(); 334 for (const moduleId of mockFileList) { 335 if (moduleId.endsWith(EXTNAME_TS) || moduleId.endsWith(EXTNAME_ETS) || moduleId.endsWith(EXTNAME_JS)) { 336 const code: string = fs.readFileSync(moduleId, 'utf-8'); 337 const metaInfo: Object = this.rollup.getModuleInfo(moduleId).meta; 338 const moduleSource = new ModuleSourceFileMock(moduleId, code, metaInfo); 339 moduleSource.initPluginEnvMock(this.rollup); 340 const filePath = genTemporaryPath(moduleSource.moduleId, moduleSource.projectConfig.projectPath, 341 moduleSource.projectConfig.cachePath, moduleSource.projectConfig, moduleSource.metaInfo); 342 utUtils.writeFileContent(moduleSource.moduleId, filePath, moduleSource.source, 343 moduleSource.projectConfig, moduleSource.logger); 344 const newFilePath = changeFileExtension(filePath, EXTNAME_JS); 345 const readFilecontent = fs.readFileSync(newFilePath, 'utf-8'); 346 expect(readFilecontent === moduleSource.source).to.be.true; 347 } 348 } 349 }); 350 351 mocha.it('4-1-1: test updateSourceMap under build debug: originMap is null', async function () { 352 this.rollup.build(); 353 const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath; 354 this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath, 355 this.rollup.share.projectConfig.entryModuleName, 356 this.rollup.share.projectConfig.modulePath) 357 ); 358 const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 359 360 const relativeSourceFilePath = 361 toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, '')); 362 const code: string = fs.readFileSync(dynamicImportpath, 'utf-8'); 363 const moduleSource = new ModuleSourceFile(dynamicImportpath, code); 364 const sourceCode: MagicString = new MagicString(<string>moduleSource.source); 365 const updatedMap: object = sourceCode.generateMap({ 366 source: relativeSourceFilePath, 367 file: `${path.basename(moduleSource.moduleId)}`, 368 includeContent: false, 369 hires: true 370 }); 371 372 updatedMap[SOURCE] = [relativeSourceFilePath]; 373 updatedMap[FILE] = path.basename(relativeSourceFilePath); 374 updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0'; 375 376 sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(undefined, updatedMap)); 377 expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true; 378 // pop dynamicImportpath moduleInfo 379 this.rollup.moduleInfos.pop(); 380 SourceMapGenerator.cleanSourceMapObject(); 381 }); 382 383 mocha.it('4-1-2: test updateSourceMap under build debug: newMap is null', async function () { 384 this.rollup.build(); 385 const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath; 386 this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath, 387 this.rollup.share.projectConfig.entryModuleName, 388 this.rollup.share.projectConfig.modulePath) 389 ); 390 const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 391 392 const relativeSourceFilePath = 393 toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, '')); 394 const code: string = fs.readFileSync(dynamicImportpath, 'utf-8'); 395 const moduleSource = new ModuleSourceFile(dynamicImportpath, code); 396 const sourceCode: MagicString = new MagicString(<string>moduleSource.source); 397 const updatedMap: object = sourceCode.generateMap({ 398 source: relativeSourceFilePath, 399 file: `${path.basename(moduleSource.moduleId)}`, 400 includeContent: false, 401 hires: true 402 }); 403 404 updatedMap[SOURCE] = [relativeSourceFilePath]; 405 updatedMap[FILE] = path.basename(relativeSourceFilePath); 406 updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0'; 407 408 sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(updatedMap)); 409 expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true; 410 // pop dynamicImportpath moduleInfo 411 this.rollup.moduleInfos.pop(); 412 SourceMapGenerator.cleanSourceMapObject(); 413 }); 414 415 mocha.it('4-1-3: test updateSourceMap under build debug: originMap and newMap is not null', async function () { 416 this.rollup.build(); 417 const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath; 418 this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath, 419 this.rollup.share.projectConfig.entryModuleName, 420 this.rollup.share.projectConfig.modulePath) 421 ); 422 const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 423 424 const relativeSourceFilePath = 425 toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, '')); 426 const code: string = fs.readFileSync(dynamicImportpath, 'utf-8'); 427 const moduleSource = new ModuleSourceFile(dynamicImportpath, code); 428 const sourceCode: MagicString = new MagicString(<string>moduleSource.source); 429 const updatedMap: object = sourceCode.generateMap({ 430 source: relativeSourceFilePath, 431 file: `${path.basename(moduleSource.moduleId)}`, 432 includeContent: false, 433 hires: true 434 }); 435 const arraylist = Array.from(this.rollup.share.allFiles); 436 const syncCode: string = fs.readFileSync(arraylist[2].toString(), 'utf-8'); 437 const dynamicModuleSource = new ModuleSourceFile(dynamicImportpath, syncCode); 438 const codeString: MagicString = new MagicString(<string>dynamicModuleSource.source); 439 const sourceMap: object = codeString.generateMap({ 440 source: relativeSourceFilePath, 441 file: `${path.basename(dynamicModuleSource.moduleId)}`, 442 includeContent: false, 443 hires: true 444 }); 445 446 delete sourceMap.sourcesContent; 447 updatedMap[SOURCE] = [relativeSourceFilePath]; 448 updatedMap[FILE] = path.basename(relativeSourceFilePath); 449 450 sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(sourceMap, updatedMap)); 451 const readSourceMap = 452 JSON.parse(fs.readFileSync(`${this.rollup.share.projectConfig.projectTopDir}/${UPDATESOURCEMAP}`, 'utf-8')); 453 expect(sourceMapGenerator.getSourceMap(dynamicImportpath).file === DYNAMICIMPORT_ETS).to.be.true; 454 expect(sourceMapGenerator.getSourceMap(dynamicImportpath).mappings === readSourceMap.mappings).to.be.true; 455 // pop dynamicImportpath moduleInfo 456 this.rollup.moduleInfos.pop(); 457 SourceMapGenerator.cleanSourceMapObject(); 458 }); 459 460 mocha.it('4-2: test updateSourceMap under build release', async function () { 461 this.rollup.build(RELEASE); 462 const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath; 463 this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath, 464 this.rollup.share.projectConfig.entryModuleName, 465 this.rollup.share.projectConfig.modulePath) 466 ); 467 const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 468 469 const relativeSourceFilePath = 470 toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, '')); 471 const code: string = fs.readFileSync(dynamicImportpath, 'utf-8'); 472 const moduleSource = new ModuleSourceFile(dynamicImportpath, code); 473 const sourceCode: MagicString = new MagicString(<string>moduleSource.source); 474 const updatedMap: object = sourceCode.generateMap({ 475 source: relativeSourceFilePath, 476 file: `${path.basename(moduleSource.moduleId)}`, 477 includeContent: false, 478 hires: true 479 }); 480 481 updatedMap[SOURCE] = [relativeSourceFilePath]; 482 updatedMap[FILE] = path.basename(relativeSourceFilePath); 483 updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0'; 484 485 sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(undefined, updatedMap)); 486 expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true; 487 // pop dynamicImportpath moduleInfo 488 this.rollup.moduleInfos.pop(); 489 SourceMapGenerator.cleanSourceMapObject(); 490 }); 491 492 mocha.it('4-3: test updateSourceMap under preview debug', async function () { 493 this.rollup.preview(); 494 const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath; 495 this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath, 496 this.rollup.share.projectConfig.entryModuleName, 497 this.rollup.share.projectConfig.modulePath) 498 ); 499 const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 500 501 const relativeSourceFilePath = 502 toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, '')); 503 const code: string = fs.readFileSync(dynamicImportpath, 'utf-8'); 504 const moduleSource = new ModuleSourceFile(dynamicImportpath, code); 505 const sourceCode: MagicString = new MagicString(<string>moduleSource.source); 506 const updatedMap: object = sourceCode.generateMap({ 507 source: relativeSourceFilePath, 508 file: `${path.basename(moduleSource.moduleId)}`, 509 includeContent: false, 510 hires: true 511 }); 512 513 updatedMap[SOURCE] = [relativeSourceFilePath]; 514 updatedMap[FILE] = path.basename(relativeSourceFilePath); 515 updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0'; 516 517 sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(undefined, updatedMap)); 518 expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true; 519 // pop dynamicImportpath moduleInfo 520 this.rollup.moduleInfos.pop(); 521 SourceMapGenerator.cleanSourceMapObject(); 522 }); 523 524 mocha.it('4-4: test updateSourceMap under hot reload debug', async function () { 525 this.rollup.hotReload(); 526 const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath; 527 this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath, 528 this.rollup.share.projectConfig.entryModuleName, 529 this.rollup.share.projectConfig.modulePath) 530 ); 531 const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 532 533 const relativeSourceFilePath = 534 toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, '')); 535 const code: string = fs.readFileSync(dynamicImportpath, 'utf-8'); 536 const moduleSource = new ModuleSourceFile(dynamicImportpath, code); 537 const sourceCode: MagicString = new MagicString(<string>moduleSource.source); 538 const updatedMap: object = sourceCode.generateMap({ 539 source: relativeSourceFilePath, 540 file: `${path.basename(moduleSource.moduleId)}`, 541 includeContent: false, 542 hires: true 543 }); 544 545 updatedMap[SOURCE] = [relativeSourceFilePath]; 546 updatedMap[FILE] = path.basename(relativeSourceFilePath); 547 updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0'; 548 549 sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(undefined, updatedMap)); 550 expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true; 551 // pop dynamicImportpath moduleInfo 552 this.rollup.moduleInfos.pop(); 553 SourceMapGenerator.cleanSourceMapObject(); 554 }); 555 556 mocha.it('5-1: test isAotMode under build debug', function () { 557 this.rollup.build(); 558 const returnInfo = isAotMode(this.rollup.share.projectConfig); 559 expect(returnInfo).to.be.false; 560 }); 561 562 mocha.it('5-2: test isAotMode under build release', function () { 563 this.rollup.build(RELEASE); 564 this.rollup.share.projectConfig.compileMode = ESMODULE; 565 this.rollup.share.projectConfig.anBuildMode = AOT_FULL; 566 const returnInfo = isAotMode(this.rollup.share.projectConfig); 567 expect(returnInfo).to.be.true; 568 }); 569 570 mocha.it('5-3: test isAotMode under preview debug', function () { 571 this.rollup.preview(); 572 this.rollup.share.projectConfig.compileMode = JSBUNDLE; 573 this.rollup.share.projectConfig.anBuildMode = AOT_PARTIAL; 574 const buildModeIsAotType = isAotMode(this.rollup.share.projectConfig); 575 expect(buildModeIsAotType).to.be.false; 576 }); 577 578 mocha.it('5-4: test isAotMode under hot reload debug', function () { 579 this.rollup.hotReload(); 580 this.rollup.share.projectConfig.compileMode = ESMODULE; 581 this.rollup.share.projectConfig.anBuildMode = AOT_TYPE; 582 const buildModeIsAotType = isAotMode(this.rollup.share.projectConfig); 583 expect(buildModeIsAotType).to.be.true; 584 }); 585 586 mocha.it('5-5: test isAotMode under hot fix debug', function () { 587 projectConfig.buildMode = DEBUG; 588 projectConfig.compileMode = JSBUNDLE; 589 projectConfig.anBuildMode = AOT_TYPE; 590 const buildModeIsAotType = isAotMode(projectConfig); 591 expect(buildModeIsAotType).to.be.false; 592 }); 593 594 mocha.it('5-6: test isAotMode under hot fix release', function () { 595 projectConfig.buildMode = RELEASE; 596 projectConfig.compileMode = ESMODULE; 597 projectConfig.anBuildMode = AOT_PARTIAL; 598 const buildModeIsAotType = isAotMode(projectConfig); 599 expect(buildModeIsAotType).to.be.true; 600 }); 601 602 mocha.it('6-1: test isDebug under build debug', function () { 603 this.rollup.build(); 604 const returnInfo = isDebug(this.rollup.share.projectConfig); 605 expect(returnInfo).to.be.true; 606 }); 607 608 mocha.it('6-2: test isDebug under build release', function () { 609 this.rollup.build(RELEASE); 610 const returnInfo = isDebug(this.rollup.share.projectConfig); 611 expect(returnInfo).to.be.false; 612 }); 613 614 mocha.it('6-3: test isDebug under preview debug', function () { 615 this.rollup.preview(); 616 const returnInfo = isDebug(this.rollup.share.projectConfig); 617 expect(returnInfo).to.be.true; 618 }); 619 620 mocha.it('6-4: test isDebug under hot reload debug', function () { 621 this.rollup.hotReload(); 622 const returnInfo = isDebug(this.rollup.share.projectConfig); 623 expect(returnInfo).to.be.true; 624 }); 625 626 mocha.it('6-5: test isDebug under hot fix debug', function () { 627 projectConfig.buildMode = DEBUG; 628 const returnInfo = isDebug(projectConfig); 629 expect(returnInfo).to.be.true; 630 }); 631 632 mocha.it('6-6: test isDebug under hot fix release', function () { 633 projectConfig.buildMode = RELEASE; 634 const returnInfo = isDebug(projectConfig); 635 expect(returnInfo).to.be.false; 636 }); 637 638 mocha.it('7-1: test changeFileExtension under build debug', function () { 639 this.rollup.build(); 640 const targetExt = EXTNAME_TS; 641 const originExt = ''; 642 const allFiles = new Set<string>(); 643 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 644 this.mockfileList = allFiles.values(); 645 for (const file of this.mockfileList) { 646 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) { 647 const currentExt = originExt.length === 0 ? path.extname(file) : originExt; 648 const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt)); 649 const returnInfo = changeFileExtension(file, targetExt, originExt); 650 expect(returnInfo === fileWithoutExt + targetExt).to.be.true; 651 } 652 } 653 }); 654 655 mocha.it('7-2: test changeFileExtension under build release', function () { 656 this.rollup.build(RELEASE); 657 const targetExt = EXTNAME_TS; 658 const originExt = ''; 659 const allFiles = new Set<string>(); 660 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 661 this.mockfileList = allFiles.values(); 662 for (const file of this.mockfileList) { 663 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) { 664 const currentExt = originExt.length === 0 ? path.extname(file) : originExt; 665 const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt)); 666 const returnInfo = changeFileExtension(file, targetExt, originExt); 667 expect(returnInfo === fileWithoutExt + targetExt).to.be.true; 668 } 669 } 670 }); 671 672 mocha.it('7-3: test changeFileExtension under preview debug', function () { 673 this.rollup.preview(); 674 const targetExt = EXTNAME_TS; 675 const originExt = ''; 676 const allFiles = new Set<string>(); 677 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 678 this.mockfileList = allFiles.values(); 679 for (const file of this.mockfileList) { 680 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) { 681 const currentExt = originExt.length === 0 ? path.extname(file) : originExt; 682 const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt)); 683 const returnInfo = changeFileExtension(file, targetExt, originExt); 684 expect(returnInfo === fileWithoutExt + targetExt).to.be.true; 685 } 686 } 687 }); 688 689 mocha.it('7-4: test changeFileExtension under hot reload debug', function () { 690 this.rollup.hotReload(); 691 const targetExt = EXTNAME_TS; 692 const originExt = ''; 693 const allFiles = new Set<string>(); 694 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 695 this.mockfileList = allFiles.values(); 696 for (const file of this.mockfileList) { 697 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) { 698 const currentExt = originExt.length === 0 ? path.extname(file) : originExt; 699 const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt)); 700 const returnInfo = changeFileExtension(file, targetExt, originExt); 701 expect(returnInfo === fileWithoutExt + targetExt).to.be.true; 702 } 703 } 704 }); 705 706 mocha.it('7-5: test changeFileExtension under hot fix debug', function () { 707 projectConfig.buildMode = DEBUG; 708 const file = TEST_TS; 709 const targetExt = EXTNAME_TS; 710 const originExt = ''; 711 const currentExt = originExt.length === 0 ? path.extname(file) : originExt; 712 const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt)); 713 const returnInfo = changeFileExtension(file, targetExt, originExt); 714 expect(returnInfo === fileWithoutExt + targetExt).to.be.true; 715 }); 716 717 mocha.it('7-6: test changeFileExtension under hot fix release', function () { 718 projectConfig.buildMode = RELEASE; 719 const file = TEST_TS; 720 const targetExt = EXTNAME_TS; 721 const originExt = ''; 722 const currentExt = originExt.length === 0 ? path.extname(file) : originExt; 723 const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt)); 724 const returnInfo = changeFileExtension(file, targetExt, originExt); 725 expect(returnInfo === fileWithoutExt + targetExt).to.be.true; 726 }); 727 728 mocha.it('8-1: test isCommonJsPluginVirtualFile under build debug', function () { 729 this.rollup.build(); 730 const allFiles = new Set<string>(); 731 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 732 this.mockfileList = allFiles.values(); 733 for (const filePath of this.mockfileList) { 734 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 735 const returnInfo = isCommonJsPluginVirtualFile(filePath); 736 expect(returnInfo).to.be.false; 737 } 738 } 739 }); 740 741 mocha.it('8-2: test isCommonJsPluginVirtualFile under build release', function () { 742 this.rollup.build(RELEASE); 743 const allFiles = new Set<string>(); 744 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 745 this.mockfileList = allFiles.values(); 746 for (const filePath of this.mockfileList) { 747 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 748 const returnInfo = isCommonJsPluginVirtualFile(filePath); 749 expect(returnInfo).to.be.false; 750 } 751 } 752 }); 753 754 mocha.it('8-3: test isCommonJsPluginVirtualFile under preview debug', function () { 755 this.rollup.preview(); 756 const allFiles = new Set<string>(); 757 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 758 this.mockfileList = allFiles.values(); 759 for (const filePath of this.mockfileList) { 760 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 761 const returnInfo = isCommonJsPluginVirtualFile(filePath); 762 expect(returnInfo).to.be.false; 763 } 764 } 765 }); 766 767 mocha.it('8-4: test isCommonJsPluginVirtualFile under hot reload debug', function () { 768 this.rollup.hotReload(); 769 const allFiles = new Set<string>(); 770 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 771 this.mockfileList = allFiles.values(); 772 for (const filePath of this.mockfileList) { 773 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 774 const returnInfo = isCommonJsPluginVirtualFile(filePath); 775 expect(returnInfo).to.be.false; 776 } 777 } 778 }); 779 780 mocha.it('8-5: test isCommonJsPluginVirtualFile under hot fix debug', function () { 781 projectConfig.buildMode = DEBUG; 782 const filePath = TEST_TS; 783 const returnInfo = isCommonJsPluginVirtualFile(filePath); 784 expect(returnInfo).to.be.false; 785 }); 786 787 mocha.it('8-6: test isCommonJsPluginVirtualFile under hot fix release', function () { 788 projectConfig.buildMode = RELEASE; 789 const filePath = TEST_TS; 790 const returnInfo = isCommonJsPluginVirtualFile(filePath); 791 expect(returnInfo).to.be.false; 792 }); 793 794 mocha.it('9-1: test isCurrentProjectFiles under build debug', function () { 795 this.rollup.build(); 796 const allFiles = new Set<string>(); 797 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 798 this.mockfileList = allFiles.values(); 799 for (const filePath of this.mockfileList) { 800 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 801 const returnInfo = isCurrentProjectFiles(filePath, this.rollup.share.projectConfig); 802 expect(returnInfo).to.be.true; 803 } 804 } 805 }); 806 807 mocha.it('9-2: test isCurrentProjectFiles under build release', function () { 808 this.rollup.build(RELEASE); 809 const allFiles = new Set<string>(); 810 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 811 this.mockfileList = allFiles.values(); 812 for (const filePath of this.mockfileList) { 813 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 814 const returnInfo = isCurrentProjectFiles(filePath, this.rollup.share.projectConfig); 815 expect(returnInfo).to.be.true; 816 } 817 } 818 }); 819 820 mocha.it('9-3: test isCurrentProjectFiles under preview debug', function () { 821 this.rollup.preview(); 822 const allFiles = new Set<string>(); 823 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 824 this.mockfileList = allFiles.values(); 825 for (const filePath of this.mockfileList) { 826 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 827 const returnInfo = isCurrentProjectFiles(filePath, this.rollup.share.projectConfig); 828 expect(returnInfo).to.be.true; 829 } 830 } 831 }); 832 833 mocha.it('9-4: test isCurrentProjectFiles under hot reload debug', function () { 834 this.rollup.hotReload(); 835 const allFiles = new Set<string>(); 836 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 837 this.mockfileList = allFiles.values(); 838 for (const filePath of this.mockfileList) { 839 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 840 const returnInfo = isCurrentProjectFiles(filePath, this.rollup.share.projectConfig); 841 expect(returnInfo).to.be.true; 842 } 843 } 844 }); 845 846 mocha.it('9-5: test isCurrentProjectFiles under hot fix debug', function () { 847 projectConfig.buildMode = DEBUG; 848 const filePath = TEST_TS; 849 const returnInfo = isCurrentProjectFiles(filePath, projectConfig); 850 expect(returnInfo).to.be.false; 851 }); 852 853 mocha.it('9-6: test isCurrentProjectFiles under hot fix release', function () { 854 projectConfig.buildMode = RELEASE; 855 const filePath = TEST_TS; 856 const returnInfo = isCurrentProjectFiles(filePath, projectConfig); 857 expect(returnInfo).to.be.false; 858 }); 859 860 mocha.it('10-1: test isSpecifiedExt under build debug', function () { 861 this.rollup.build(); 862 const fileExtendName = EXTNAME_ETS; 863 const allFiles = new Set<string>(); 864 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 865 this.mockfileList = allFiles.values(); 866 for (const filePath of this.mockfileList) { 867 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 868 if (filePath.endsWith(EXTNAME_ETS)) { 869 expect(isSpecifiedExt(filePath, fileExtendName)).to.be.true; 870 } else { 871 expect(isSpecifiedExt(filePath, fileExtendName)).to.be.false; 872 } 873 } 874 } 875 }); 876 877 mocha.it('10-2: test isSpecifiedExt under build release', function () { 878 this.rollup.build(RELEASE); 879 const fileExtendName = EXTNAME_JS; 880 const allFiles = new Set<string>(); 881 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 882 this.mockfileList = allFiles.values(); 883 for (const filePath of this.mockfileList) { 884 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 885 if (filePath.endsWith(EXTNAME_JS)) { 886 expect(isSpecifiedExt(filePath, fileExtendName)).to.be.true; 887 } else { 888 expect(isSpecifiedExt(filePath, fileExtendName)).to.be.false; 889 } 890 } 891 } 892 }); 893 894 mocha.it('10-3: test isSpecifiedExt under preview debug', function () { 895 this.rollup.preview(); 896 const fileExtendName = EXTNAME_TS; 897 const allFiles = new Set<string>(); 898 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 899 this.mockfileList = allFiles.values(); 900 for (const filePath of this.mockfileList) { 901 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 902 if (filePath.endsWith(EXTNAME_TS)) { 903 expect(isSpecifiedExt(filePath, fileExtendName)).to.be.true; 904 } else { 905 expect(isSpecifiedExt(filePath, fileExtendName)).to.be.false; 906 } 907 } 908 } 909 }); 910 911 mocha.it('10-4: test isSpecifiedExt under hot reload debug', function () { 912 this.rollup.hotReload(); 913 const fileExtendName = EXTNAME_JS; 914 const allFiles = new Set<string>(); 915 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 916 this.mockfileList = allFiles.values(); 917 for (const filePath of this.mockfileList) { 918 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 919 if (filePath.endsWith(EXTNAME_JS)) { 920 expect(isSpecifiedExt(filePath, fileExtendName)).to.be.true; 921 } else { 922 expect(isSpecifiedExt(filePath, fileExtendName)).to.be.false; 923 } 924 } 925 } 926 }); 927 928 mocha.it('10-5: test isSpecifiedExt under hot fix debug', function () { 929 projectConfig.buildMode = DEBUG; 930 const fileExtendName = EXTNAME_JS; 931 const filePath = TEST_JS; 932 const returnInfo = isSpecifiedExt(filePath, fileExtendName); 933 expect(returnInfo).to.be.true; 934 }); 935 936 mocha.it('10-6: test isSpecifiedExt under hot fix release', function () { 937 projectConfig.buildMode = RELEASE; 938 const fileExtendName = EXTNAME_JS; 939 const filePath = TEST_TS; 940 const returnInfo = isSpecifiedExt(filePath, fileExtendName); 941 expect(returnInfo).to.be.false; 942 }); 943 944 mocha.it('11-1: test isTsOrEtsSourceFile under build debug', function () { 945 this.rollup.build(); 946 const allFiles = new Set<string>(); 947 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 948 this.mockfileList = allFiles.values(); 949 for (const file of this.mockfileList) { 950 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) { 951 const returnInfo = isTsOrEtsSourceFile(file); 952 expect(returnInfo).to.be.true; 953 } 954 if (file.endsWith(EXTNAME_JS)) { 955 const returnInfoJs = isTsOrEtsSourceFile(file); 956 expect(returnInfoJs).to.be.false; 957 } 958 } 959 }); 960 961 mocha.it('11-2: test isTsOrEtsSourceFile under build release', function () { 962 this.rollup.build(RELEASE); 963 const allFiles = new Set<string>(); 964 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 965 this.mockfileList = allFiles.values(); 966 for (const file of this.mockfileList) { 967 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) { 968 const returnInfo = isTsOrEtsSourceFile(file); 969 expect(returnInfo).to.be.true; 970 } 971 if (file.endsWith(EXTNAME_JS)) { 972 const returnInfoJs = isTsOrEtsSourceFile(file); 973 expect(returnInfoJs).to.be.false; 974 } 975 } 976 }); 977 978 mocha.it('11-3: test isTsOrEtsSourceFile under preview debug', function () { 979 this.rollup.preview(); 980 const allFiles = new Set<string>(); 981 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 982 this.mockfileList = allFiles.values(); 983 for (const file of this.mockfileList) { 984 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) { 985 const returnInfo = isTsOrEtsSourceFile(file); 986 expect(returnInfo).to.be.true; 987 } 988 if (file.endsWith(EXTNAME_JS)) { 989 const returnInfoJs = isTsOrEtsSourceFile(file); 990 expect(returnInfoJs).to.be.false; 991 } 992 } 993 }); 994 995 mocha.it('11-4: test isTsOrEtsSourceFile under hot reload debug', function () { 996 this.rollup.hotReload(); 997 const allFiles = new Set<string>(); 998 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 999 this.mockfileList = allFiles.values(); 1000 for (const file of this.mockfileList) { 1001 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) { 1002 const returnInfo = isTsOrEtsSourceFile(file); 1003 expect(returnInfo).to.be.true; 1004 } 1005 if (file.endsWith(EXTNAME_JS)) { 1006 const returnInfoJs = isTsOrEtsSourceFile(file); 1007 expect(returnInfoJs).to.be.false; 1008 } 1009 } 1010 }); 1011 1012 mocha.it('11-5: test isTsOrEtsSourceFile under hot fix debug', function () { 1013 projectConfig.buildMode = DEBUG; 1014 const file = TEST_TS; 1015 const returnInfo = isTsOrEtsSourceFile(file); 1016 expect(returnInfo).to.be.true; 1017 const fileEts = TEST_ETS; 1018 const returnInfoEts = isTsOrEtsSourceFile(fileEts); 1019 expect(returnInfoEts).to.be.true; 1020 1021 }); 1022 1023 mocha.it('11-6: test isTsOrEtsSourceFile under hot fix release', function () { 1024 projectConfig.buildMode = RELEASE; 1025 const file = TEST_TS; 1026 const returnInfo = isTsOrEtsSourceFile(file); 1027 expect(returnInfo).to.be.true; 1028 const fileJs = TEST_JS; 1029 const returnInfoJs = isTsOrEtsSourceFile(fileJs); 1030 expect(returnInfoJs).to.be.false; 1031 }); 1032 1033 mocha.it('12-1: test isJsSourceFile under build debug', function () { 1034 this.rollup.build(); 1035 const allFiles = new Set<string>(); 1036 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 1037 this.mockfileList = allFiles.values(); 1038 for (const file of this.mockfileList) { 1039 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) { 1040 const returnInfo = isJsSourceFile(file); 1041 expect(returnInfo).to.be.false; 1042 } 1043 if (file.endsWith(EXTNAME_JS)) { 1044 const returnInfoJs = isJsSourceFile(file); 1045 expect(returnInfoJs).to.be.true; 1046 } 1047 } 1048 }); 1049 1050 mocha.it('12-2: test isJsSourceFile under build release', function () { 1051 this.rollup.build(RELEASE); 1052 const allFiles = new Set<string>(); 1053 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 1054 this.mockfileList = allFiles.values(); 1055 for (const file of this.mockfileList) { 1056 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) { 1057 const returnInfo = isJsSourceFile(file); 1058 expect(returnInfo).to.be.false; 1059 } 1060 if (file.endsWith(EXTNAME_JS)) { 1061 const returnInfoJs = isJsSourceFile(file); 1062 expect(returnInfoJs).to.be.true; 1063 } 1064 } 1065 }); 1066 1067 mocha.it('12-3: test isJsSourceFile under preview debug', function () { 1068 this.rollup.preview(); 1069 const allFiles = new Set<string>(); 1070 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 1071 this.mockfileList = allFiles.values(); 1072 for (const file of this.mockfileList) { 1073 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) { 1074 const returnInfo = isJsSourceFile(file); 1075 expect(returnInfo).to.be.false; 1076 } 1077 if (file.endsWith(EXTNAME_JS)) { 1078 const returnInfoJs = isJsSourceFile(file); 1079 expect(returnInfoJs).to.be.true; 1080 } 1081 } 1082 }); 1083 1084 mocha.it('12-4: test isJsSourceFile under hot reload debug', function () { 1085 this.rollup.hotReload(); 1086 const allFiles = new Set<string>(); 1087 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 1088 this.mockfileList = allFiles.values(); 1089 for (const file of this.mockfileList) { 1090 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) { 1091 const returnInfo = isJsSourceFile(file); 1092 expect(returnInfo).to.be.false; 1093 } 1094 if (file.endsWith(EXTNAME_JS)) { 1095 const returnInfoJs = isJsSourceFile(file); 1096 expect(returnInfoJs).to.be.true; 1097 } 1098 } 1099 }); 1100 1101 mocha.it('12-5: test isJsSourceFile under hot fix debug', function () { 1102 projectConfig.buildMode = DEBUG; 1103 const file = TEST_JS; 1104 const returnInfo = isJsSourceFile(file); 1105 expect(returnInfo).to.be.true; 1106 const fileTs = TEST_TS; 1107 const returnInfoTs = isJsSourceFile(fileTs); 1108 expect(returnInfoTs).to.be.false; 1109 }); 1110 1111 mocha.it('12-6: test isJsSourceFile under hot fix release', function () { 1112 projectConfig.buildMode = RELEASE; 1113 const file = TEST_JS; 1114 const returnInfo = isJsSourceFile(file); 1115 expect(returnInfo).to.be.true; 1116 const fileTs = TEST_TS; 1117 const returnInfoTs = isJsSourceFile(fileTs); 1118 expect(returnInfoTs).to.be.false; 1119 }); 1120 1121 mocha.it('13-1: test isJsonSourceFile under build debug', function () { 1122 this.rollup.build(); 1123 const allFiles = new Set<string>(); 1124 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 1125 this.mockfileList = allFiles.values(); for (const file of this.mockfileList) { 1126 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) { 1127 const returnInfo = isJsonSourceFile(file); 1128 expect(returnInfo).to.be.false; 1129 } 1130 if (file.endsWith(EXTNAME_JSON)) { 1131 const returnInfoJson = isJsonSourceFile(file); 1132 expect(returnInfoJson).to.be.true; 1133 } 1134 } 1135 }); 1136 1137 mocha.it('13-2: test isJsonSourceFile under build release', function () { 1138 this.rollup.build(RELEASE); 1139 const allFiles = new Set<string>(); 1140 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 1141 this.mockfileList = allFiles.values(); 1142 for (const file of this.mockfileList) { 1143 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) { 1144 const returnInfo = isJsonSourceFile(file); 1145 expect(returnInfo).to.be.false; 1146 } 1147 if (file.endsWith(EXTNAME_JSON)) { 1148 const returnInfoJson = isJsonSourceFile(file); 1149 expect(returnInfoJson).to.be.true; 1150 } 1151 } 1152 }); 1153 1154 mocha.it('13-3: test isJsonSourceFile under preview debug', function () { 1155 this.rollup.preview(); 1156 const allFiles = new Set<string>(); 1157 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 1158 this.mockfileList = allFiles.values(); 1159 for (const file of this.mockfileList) { 1160 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) { 1161 const returnInfo = isJsonSourceFile(file); 1162 expect(returnInfo).to.be.false; 1163 } 1164 if (file.endsWith(EXTNAME_JSON)) { 1165 const returnInfoJson = isJsonSourceFile(file); 1166 expect(returnInfoJson).to.be.true; 1167 } 1168 } 1169 }); 1170 1171 mocha.it('13-4: test isJsonSourceFile under hot reload debug', function () { 1172 this.rollup.hotReload(); 1173 const allFiles = new Set<string>(); 1174 scanFiles(this.rollup.share.projectConfig.modulePath, allFiles); 1175 this.mockfileList = allFiles.values(); 1176 for (const file of this.mockfileList) { 1177 if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) { 1178 const returnInfo = isJsonSourceFile(file); 1179 expect(returnInfo).to.be.false; 1180 } 1181 if (file.endsWith(EXTNAME_JSON)) { 1182 const returnInfoJson = isJsonSourceFile(file); 1183 expect(returnInfoJson).to.be.true; 1184 } 1185 } 1186 }); 1187 1188 mocha.it('13-5: test isJsonSourceFile under hot fix debug', function () { 1189 projectConfig.buildMode = DEBUG; 1190 const file = TEST_TS; 1191 const returnInfo = isJsonSourceFile(file); 1192 expect(returnInfo).to.be.false; 1193 const fileJson = TEST_JSON; 1194 const returnInfoJson = isJsonSourceFile(fileJson); 1195 expect(returnInfoJson).to.be.true; 1196 }); 1197 1198 mocha.it('13-6: test isJsonSourceFile under hot fix release', function () { 1199 projectConfig.buildMode = RELEASE; 1200 const file = TEST_TS; 1201 const returnInfo = isJsonSourceFile(file); 1202 expect(returnInfo).to.be.false; 1203 const fileJson = TEST_JS; 1204 const returnInfoJson = isJsonSourceFile(fileJson); 1205 expect(returnInfoJson).to.be.false; 1206 }); 1207 1208 mocha.it('14-1: test moduleResolutionHost', function () { 1209 const dirExists = moduleResolutionHostTest.directoryExists(path.resolve(__dirname)); 1210 const dirNotExists = moduleResolutionHostTest.directoryExists(path.resolve('./dirNotExists')); 1211 expect(dirExists).to.be.true; 1212 expect(dirNotExists).to.be.false; 1213 const fileExists = moduleResolutionHostTest.fileExists(path.resolve(__filename)); 1214 const fileNotExists = moduleResolutionHostTest.fileExists(path.resolve('./fileNotExists')); 1215 expect(fileExists).to.be.true; 1216 expect(fileNotExists).to.be.false; 1217 const dirExistsCache = moduleResolutionHostTest.directoryExists(path.resolve(__dirname)); 1218 const dirNotExistsCache = moduleResolutionHostTest.directoryExists(path.resolve('./dirNotExists')); 1219 expect(dirExistsCache).to.be.true; 1220 expect(dirNotExistsCache).to.be.false; 1221 const fileExistsCache = moduleResolutionHostTest.fileExists(path.resolve(__filename)); 1222 const fileNotExistsCache = moduleResolutionHostTest.fileExists(path.resolve('./fileNotExists')); 1223 expect(fileExistsCache).to.be.true; 1224 expect(fileNotExistsCache).to.be.false; 1225 }); 1226 1227 mocha.it('15-1: test get/setRollupCache with hvigor provided cache interface', function () { 1228 this.rollup.build(); 1229 let cacheKey: string = "cache1"; 1230 let cacheKeyNotExist: string = "cacheNotExist"; 1231 let cacheValue: object = { 1232 value: "value1" 1233 }; 1234 this.rollup.share.initWithCache(); 1235 setRollupCache(this.rollup.share, projectConfig, cacheKey, cacheValue); 1236 expect(getRollupCache(this.rollup.share, projectConfig, cacheKey)).to.be.equal(cacheValue); 1237 expect(getRollupCache(this.rollup.share, projectConfig, cacheKeyNotExist)).to.be.equal(undefined); 1238 }); 1239 1240 mocha.it('15-2: test get/setRollupCache with hvigor provided cacheStoreManager interface', function () { 1241 this.rollup.build(); 1242 let cacheKey: string = "cache1"; 1243 let cacheKeyNotExist: string = "cacheNotExist"; 1244 let cacheValue: object = { 1245 value: "value1" 1246 }; 1247 this.rollup.share.initWithCacheStoreManager(); 1248 setRollupCache(this.rollup.share, projectConfig, cacheKey, cacheValue); 1249 expect(getRollupCache(this.rollup.share, projectConfig, cacheKey)).to.be.equal(cacheValue); 1250 expect(getRollupCache(this.rollup.share, projectConfig, cacheKeyNotExist)).to.be.equal(undefined); 1251 }); 1252 1253 mocha.it('15-3: test get/setRollupCache without hvigor cache interface provided', function () { 1254 this.rollup.build(); 1255 let cacheKey: string = "cache1"; 1256 let cacheKeyNotExist: string = "cacheNotExist"; 1257 let cacheValue: object = { 1258 value: "value1" 1259 }; 1260 this.rollup.share.initWithoutCache(); 1261 setRollupCache(this.rollup.share, projectConfig, cacheKey, cacheValue); 1262 expect(getRollupCache(this.rollup.share, projectConfig, cacheKey)).to.be.equal(undefined); 1263 expect(getRollupCache(this.rollup.share, projectConfig, cacheKeyNotExist)).to.be.equal(undefined); 1264 }); 1265 1266 mocha.it('16-1: test genTemporaryPath adapt external modules', function () { 1267 this.rollup.build(); 1268 const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; 1269 const moduleInfo = { 1270 id: filePath, 1271 meta: { 1272 isLocalDependency: true, 1273 moduleName: 'libhar', 1274 belongModulePath: '/testHar/har', 1275 } 1276 }; 1277 this.rollup.moduleInfos.push(moduleInfo); 1278 const projectConfig = this.rollup.share.projectConfig; 1279 const metaInfo = this.rollup.getModuleInfo(filePath).meta; 1280 const cacheFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.cachePath, 1281 projectConfig, metaInfo); 1282 const expectCacheFilePath = `${projectConfig.cachePath}/libhar/src/main/ets/utils/Calc.ets`; 1283 expect(cacheFilePath === expectCacheFilePath).to.be.true; 1284 }); 1285 1286 mocha.it('16-2: test genTemporaryPath to concatenate the paths under the PackageHar directory', function () { 1287 this.rollup.build(); 1288 const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; 1289 const moduleInfo = { 1290 id: filePath, 1291 meta: { 1292 isLocalDependency: true, 1293 moduleName: 'libhar', 1294 belongModulePath: '/testHar/har', 1295 } 1296 }; 1297 this.rollup.moduleInfos.push(moduleInfo); 1298 const projectConfig = this.rollup.share.projectConfig; 1299 projectConfig.compileHar = true; 1300 const metaInfo = this.rollup.getModuleInfo(filePath).meta; 1301 const buildFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath, 1302 projectConfig, metaInfo, true); 1303 const expectBuildFilePath = `${projectConfig.buildPath}/src/main/ets/utils/Calc.ets`; 1304 expect(buildFilePath === expectBuildFilePath).to.be.true; 1305 }); 1306 1307 mocha.it('17-1: test getProjectRootPath adapt external modules', function () { 1308 this.rollup.build(); 1309 const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; 1310 this.rollup.share.projectConfig.rootPathSet = ['/testHar', `${PROJECT_ROOT}/${DEFAULT_PROJECT}`]; 1311 const projectConfig = this.rollup.share.projectConfig; 1312 const existsSyncStub = sinon.stub(fs, 'existsSync').returns(true); 1313 const statSyncStub = sinon.stub(fs, 'statSync').returns({ 1314 isFile: sinon.stub().returns(true) 1315 }); 1316 const projectRootPath: string = getProjectRootPath(filePath, projectConfig, projectConfig.rootPathSet); 1317 const expectProjectConfig: string = '/testHar'; 1318 expect(projectRootPath === expectProjectConfig).to.be.true; 1319 existsSyncStub.restore(); 1320 statSyncStub.restore(); 1321 }); 1322 1323 mocha.it('17-2: test getProjectRootPath adapt external modules(multiple project names contain a relationship)', 1324 function () { 1325 this.rollup.build(); 1326 const filePath: string = '/project/testA/har/src/main/ets/utils/Calc.ets'; 1327 this.rollup.share.projectConfig.rootPathSet = ['/project/test', '/project/testA', '/project/testAB']; 1328 const projectConfig = this.rollup.share.projectConfig; 1329 const existsSyncStub = sinon.stub(fs, 'existsSync').returns(true); 1330 const statSyncStub = sinon.stub(fs, 'statSync').returns({ 1331 isFile: sinon.stub().returns(true) 1332 }); 1333 const projectRootPath: string = getProjectRootPath(filePath, projectConfig, projectConfig.rootPathSet); 1334 const expectProjectConfig: string = '/project/testA'; 1335 expect(projectRootPath === expectProjectConfig).to.be.true; 1336 existsSyncStub.restore(); 1337 statSyncStub.restore(); 1338 }); 1339 1340 mocha.it('17-3: test getProjectRootPath under build', function () { 1341 this.rollup.build(); 1342 const filePath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har/src/main/ets/utils/Calc.ets`; 1343 const projectConfig = this.rollup.share.projectConfig; 1344 const projectRootPath: string = getProjectRootPath(filePath, projectConfig, projectConfig.rootPathSet); 1345 expect(projectRootPath === projectConfig.projectRootPath).to.be.true; 1346 }); 1347 1348 mocha.it('18-1: test getBelongModuleInfo under build file is local dependency', function () { 1349 this.rollup.build(); 1350 const filePath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har/src/main/ets/utils/Calc.ets`; 1351 const projectRootPath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}`; 1352 const modulePathMap: Object = { 1353 'enrty': `${PROJECT_ROOT}/${DEFAULT_PROJECT}/entry`, 1354 'libhar': `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har` 1355 }; 1356 const expectBelongModuleInfo: Object = { 1357 isLocalDependency: true, 1358 moduleName: 'libhar', 1359 belongModulePath: `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har` 1360 }; 1361 const belongModuleInfo: Object = getBelongModuleInfo(filePath, modulePathMap, projectRootPath); 1362 Object.keys(belongModuleInfo).forEach(item => { 1363 expect(belongModuleInfo[item] === expectBelongModuleInfo[item]).to.be.true; 1364 }); 1365 }); 1366 1367 mocha.it('18-2: test getBelongModuleInfo under build file is not local dependency', function () { 1368 this.rollup.build(); 1369 const filePath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}/oh_modules/.ohpm/json5/index.js`; 1370 const projectRootPath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}`; 1371 const modulePathMap: Object = { 1372 'enrty': `${PROJECT_ROOT}/${DEFAULT_PROJECT}/entry`, 1373 'libhar': `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har` 1374 }; 1375 const expectBelongModuleInfo: Object = { 1376 isLocalDependency: false, 1377 moduleName: '', 1378 belongModulePath: `${PROJECT_ROOT}/${DEFAULT_PROJECT}` 1379 }; 1380 const belongModuleInfo: Object = getBelongModuleInfo(filePath, modulePathMap, projectRootPath); 1381 Object.keys(belongModuleInfo).forEach(item => { 1382 expect(belongModuleInfo[item] === expectBelongModuleInfo[item]).to.be.true; 1383 }); 1384 }); 1385});