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 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 fs from 'fs'; 18import mocha from 'mocha'; 19import path from 'path'; 20import sinon from 'sinon'; 21 22import { 23 RELEASE 24} from '../../../lib/fast_build/ark_compiler/common/ark_define'; 25import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock'; 26import { SourceMapGenerator } from '../../../lib/fast_build/ark_compiler/generate_sourcemap'; 27import { 28 ENTRYABILITY_JS_PATH_DEFAULT, 29 ENTRYABILITY_TS_PATH_DEFAULT, 30 ENTRY_PACKAGE_NAME_DEFAULT, 31 ENTRY_MODULE_VERSION_DEFAULT, 32 INDEX_ETS_PATH_DEFAULT, 33 INDEX_JS_CACHE_PATH 34} from '../mock/rollup_mock/common'; 35import { 36 compilingEtsOrTsFiles, 37 hasTsNoCheckOrTsIgnoreFiles, 38 cleanUpFilesList 39} from '../../../lib/fast_build/ark_compiler/utils'; 40import { 41 ArkTSInternalErrorDescription, 42 ErrorCode, 43} from '../../../lib/fast_build/ark_compiler/error_code'; 44import { 45 CommonLogger, 46 LogData, 47 LogDataFactory 48} from '../../../lib/fast_build/ark_compiler/logger'; 49 50const prefix = `${ENTRY_PACKAGE_NAME_DEFAULT}|${ENTRY_PACKAGE_NAME_DEFAULT}|${ENTRY_MODULE_VERSION_DEFAULT}|`; 51let entryPkgInfo = `${ENTRY_PACKAGE_NAME_DEFAULT}|${ENTRY_MODULE_VERSION_DEFAULT}`; 52 53mocha.describe('test generate_sourcemap api', function () { 54 mocha.before(function () { 55 this.rollup = new RollUpPluginMock(); 56 }); 57 58 mocha.after(() => { 59 delete this.rollup; 60 }); 61 62 mocha.it('1-1: test getPkgInfoByModuleId under build debug', function () { 63 this.rollup.build(); 64 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 65 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 66 let pkgInfo = sourceMapGenerator.getPkgInfoByModuleId(moduleId); 67 68 expect(pkgInfo && pkgInfo.entry && pkgInfo.modulePath && pkgInfo.entry.name && pkgInfo.entry.version !== '' && pkgInfo.entry.version != undefined).to.be.true; 69 SourceMapGenerator.cleanSourceMapObject(); 70 }); 71 72 mocha.it('1-2: test getPkgInfoByModuleId under build release', function () { 73 this.rollup.build(RELEASE); 74 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 75 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 76 let pkgInfo = sourceMapGenerator.getPkgInfoByModuleId(moduleId); 77 78 expect(pkgInfo && pkgInfo.entry && pkgInfo.modulePath && pkgInfo.entry.name && pkgInfo.entry.version !== '' && pkgInfo.entry.version != undefined).to.be.true; 79 SourceMapGenerator.cleanSourceMapObject(); 80 }); 81 82 mocha.it('1-3: test getPkgInfoByModuleId under preview', function () { 83 this.rollup.preview(); 84 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 85 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 86 let pkgInfo = sourceMapGenerator.getPkgInfoByModuleId(moduleId); 87 88 expect(pkgInfo && pkgInfo.entry && pkgInfo.modulePath && pkgInfo.entry.name && pkgInfo.entry.version !== '' && pkgInfo.entry.version != undefined).to.be.true; 89 SourceMapGenerator.cleanSourceMapObject(); 90 }); 91 92 mocha.it('1-4: test getPkgInfoByModuleId under hotReload', function () { 93 this.rollup.hotReload(); 94 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 95 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 96 let pkgInfo = sourceMapGenerator.getPkgInfoByModuleId(moduleId); 97 98 expect(pkgInfo && pkgInfo.entry && pkgInfo.modulePath && pkgInfo.entry.name && pkgInfo.entry.version !== '' && pkgInfo.entry.version != undefined).to.be.true; 99 SourceMapGenerator.cleanSourceMapObject(); 100 }); 101 102 mocha.it('1-5: test getPkgInfoByModuleId with file name obfuscate', function () { 103 this.rollup.build(); 104 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 105 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 106 compilingEtsOrTsFiles.push(moduleId); 107 let pkgInfo = sourceMapGenerator.getPkgInfoByModuleId(moduleId, true); 108 expect(pkgInfo && pkgInfo.entry && pkgInfo.modulePath && pkgInfo.entry.name && pkgInfo.entry.version !== '' && pkgInfo.entry.version != undefined).to.be.true; 109 expect(pkgInfo.modulePath == 'src/main/a/b.ts').to.be.true; 110 cleanUpFilesList(); 111 SourceMapGenerator.cleanSourceMapObject(); 112 }); 113 114 mocha.it('1-6: test GetModuleInfoFaild error of getPkgInfoByModuleId', function () { 115 this.rollup.build(); 116 const errInfo: LogData = LogDataFactory.newInstance( 117 ErrorCode.ETS2BUNDLE_INTERNAL_GET_MODULE_INFO_FAILED, 118 ArkTSInternalErrorDescription, 119 'Failed to get ModuleInfo, moduleId: moduleId' 120 ); 121 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 122 let moduleId = "moduleId"; 123 const getModuleInfoStub = sinon.stub(SourceMapGenerator.rollupObject, 'getModuleInfo').returns(undefined); 124 const stub = sinon.stub(sourceMapGenerator.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 125 126 try { 127 sourceMapGenerator.getPkgInfoByModuleId(moduleId); 128 } catch (e) { 129 } 130 expect(stub.calledWith(errInfo)).to.be.true; 131 getModuleInfoStub.restore(); 132 stub.restore(); 133 SourceMapGenerator.cleanSourceMapObject(); 134 }) 135 136 mocha.it('1-6-1: test GetModuleInfoFaild error of getPkgInfoByModuleId ' + 137 'without getHvigorConsoleLogger', function () { 138 this.rollup.build(); 139 const errInfo: LogData = LogDataFactory.newInstance( 140 ErrorCode.ETS2BUNDLE_INTERNAL_GET_MODULE_INFO_FAILED, 141 ArkTSInternalErrorDescription, 142 'Failed to get ModuleInfo, moduleId: moduleId' 143 ); 144 CommonLogger.destroyInstance(); 145 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 146 this.rollup.share.getHvigorConsoleLogger = undefined; 147 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 148 let moduleId = "moduleId"; 149 const getModuleInfoStub = sinon.stub(SourceMapGenerator.rollupObject, 'getModuleInfo').returns(undefined); 150 const stub = sinon.stub(sourceMapGenerator.logger, 'throwArkTsCompilerError'); 151 152 try { 153 sourceMapGenerator.getPkgInfoByModuleId(moduleId); 154 } catch (e) { 155 } 156 expect(stub.calledWith(errInfo.toString())).to.be.true; 157 CommonLogger.destroyInstance(); 158 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 159 getModuleInfoStub.restore(); 160 stub.restore(); 161 SourceMapGenerator.cleanSourceMapObject(); 162 }) 163 164 mocha.it('1-7: test UnableToGetModuleInfoMeta error of getPkgInfoByModuleId', function () { 165 this.rollup.build(); 166 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 167 let moduleId = "moduleId"; 168 const getModuleInfoStub = sinon.stub(SourceMapGenerator.rollupObject, 'getModuleInfo').returns({'meta': undefined}); 169 const stub = sinon.stub(sourceMapGenerator.logger, 'printErrorAndExit'); 170 171 try { 172 sourceMapGenerator.getPkgInfoByModuleId(moduleId); 173 } catch (e) { 174 } 175 const errInfo: LogData = LogDataFactory.newInstance( 176 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_GET_MODULE_INFO_META, 177 ArkTSInternalErrorDescription, 178 "Failed to get ModuleInfo properties 'meta', moduleId: moduleId" 179 ); 180 expect(stub.calledWith(errInfo)).to.be.true; 181 getModuleInfoStub.restore(); 182 stub.restore(); 183 SourceMapGenerator.cleanSourceMapObject(); 184 }) 185 186 mocha.it('1-8: test UnableToGetModuleInfoMetaPkgPath error of getPkgInfoByModuleId', function () { 187 this.rollup.build(); 188 const errInfo: LogData = LogDataFactory.newInstance( 189 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_GET_MODULE_INFO_META_PKG_PATH, 190 ArkTSInternalErrorDescription, 191 "Failed to get ModuleInfo properties 'meta.pkgPath', moduleId: moduleId" 192 ); 193 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 194 let moduleId = "moduleId"; 195 const getModuleInfoStub = sinon.stub(SourceMapGenerator.rollupObject, 'getModuleInfo').returns({'meta': {'pkgPath': undefined}}); 196 const stub = sinon.stub(sourceMapGenerator.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 197 198 try { 199 sourceMapGenerator.getPkgInfoByModuleId(moduleId); 200 } catch (e) { 201 } 202 expect(stub.calledWith(errInfo)).to.be.true; 203 getModuleInfoStub.restore(); 204 stub.restore(); 205 SourceMapGenerator.cleanSourceMapObject(); 206 }) 207 208 mocha.it('1-9: test UnableToGetModuleInfoMetaPkgPath error of getPkgInfoByModuleId ' + 209 'without getHvigorConsoleLogger', function () { 210 this.rollup.build(); 211 const errInfo: LogData = LogDataFactory.newInstance( 212 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_GET_MODULE_INFO_META_PKG_PATH, 213 ArkTSInternalErrorDescription, 214 "Failed to get ModuleInfo properties 'meta.pkgPath', moduleId: moduleId" 215 ); 216 CommonLogger.destroyInstance(); 217 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 218 this.rollup.share.getHvigorConsoleLogger = undefined; 219 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 220 let moduleId = "moduleId"; 221 const getModuleInfoStub = sinon.stub(SourceMapGenerator.rollupObject, 'getModuleInfo').returns({'meta': {'pkgPath': undefined}}); 222 const stub = sinon.stub(sourceMapGenerator.logger, 'throwArkTsCompilerError'); 223 224 try { 225 sourceMapGenerator.getPkgInfoByModuleId(moduleId); 226 } catch (e) { 227 } 228 expect(stub.calledWith(errInfo.toString())).to.be.true; 229 CommonLogger.destroyInstance(); 230 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 231 getModuleInfoStub.restore(); 232 stub.restore(); 233 SourceMapGenerator.cleanSourceMapObject(); 234 }) 235 236 mocha.it('2-1: test genKey under build debug', function () { 237 this.rollup.build(); 238 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 239 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 240 let genKey = sourceMapGenerator.genKey(moduleId); 241 let expectKey = prefix + ENTRYABILITY_TS_PATH_DEFAULT.substring(1); 242 243 expect(genKey === expectKey).to.be.true; 244 SourceMapGenerator.cleanSourceMapObject(); 245 }); 246 247 mocha.it('2-2: test genKey under build release', function () { 248 this.rollup.build(RELEASE); 249 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 250 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 251 let genKey = sourceMapGenerator.genKey(moduleId); 252 let expectKey = prefix + ENTRYABILITY_TS_PATH_DEFAULT.substring(1); 253 254 expect(genKey === expectKey).to.be.true; 255 SourceMapGenerator.cleanSourceMapObject(); 256 }); 257 258 mocha.it('2-3: test genKey under preview', function () { 259 this.rollup.preview(); 260 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 261 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 262 let genKey = sourceMapGenerator.genKey(moduleId); 263 let expectKey = prefix + ENTRYABILITY_TS_PATH_DEFAULT.substring(1); 264 265 expect(genKey === expectKey).to.be.true; 266 SourceMapGenerator.cleanSourceMapObject(); 267 }); 268 269 mocha.it('2-4: test genKey under hotReload', function () { 270 this.rollup.hotReload(); 271 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 272 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 273 let genKey = sourceMapGenerator.genKey(moduleId); 274 let expectKey = prefix + ENTRYABILITY_TS_PATH_DEFAULT.substring(1); 275 276 expect(genKey === expectKey).to.be.true; 277 SourceMapGenerator.cleanSourceMapObject(); 278 }); 279 280 mocha.it('2-5: test genKey with file name obfuscate', function () { 281 this.rollup.build(); 282 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 283 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 284 let genKey = sourceMapGenerator.genKey(moduleId, true); 285 let expectKey = 'entry|entry|1.0.0|src/main/a/b.ts'; 286 expect(genKey === expectKey).to.be.true; 287 SourceMapGenerator.cleanSourceMapObject(); 288 }); 289 290 mocha.it('3-1: test updateSourceMap under build debug', function () { 291 this.rollup.build(); 292 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 293 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 294 let sourceMapData = { version: '1.0.0' }; 295 sourceMapGenerator.updateSourceMap(moduleId, sourceMapData); 296 let sourceMapDataGet = sourceMapGenerator.getSourceMap(moduleId); 297 expect(sourceMapData === sourceMapDataGet).to.be.true; 298 SourceMapGenerator.cleanSourceMapObject(); 299 }); 300 301 mocha.it('3-2: test updateSourceMap under build release', function () { 302 this.rollup.build(RELEASE); 303 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 304 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 305 let sourceMapData = { version: '1.0.0' }; 306 sourceMapGenerator.updateSourceMap(moduleId, sourceMapData); 307 let sourceMapDataGet = sourceMapGenerator.getSourceMap(moduleId); 308 expect(sourceMapData === sourceMapDataGet).to.be.true; 309 SourceMapGenerator.cleanSourceMapObject(); 310 }); 311 312 mocha.it('3-3: test updateSourceMap under preview', function () { 313 this.rollup.preview(); 314 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 315 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 316 let sourceMapData = { version: '1.0.0' }; 317 sourceMapGenerator.updateSourceMap(moduleId, sourceMapData); 318 let sourceMapDataGet = sourceMapGenerator.getSourceMap(moduleId); 319 expect(sourceMapData === sourceMapDataGet).to.be.true; 320 SourceMapGenerator.cleanSourceMapObject(); 321 }); 322 323 mocha.it('3-4: test updateSourceMap under hotReload', function () { 324 this.rollup.hotReload(); 325 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 326 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 327 let sourceMapData = { version: '1.0.0' }; 328 sourceMapGenerator.updateSourceMap(moduleId, sourceMapData); 329 let sourceMapDataGet = sourceMapGenerator.getSourceMap(moduleId); 330 expect(sourceMapData === sourceMapDataGet).to.be.true; 331 SourceMapGenerator.cleanSourceMapObject(); 332 }); 333 334 mocha.it('4-1: test fillSourceMapPackageInfo under build debug', function () { 335 this.rollup.build(); 336 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 337 338 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 339 let sourceMapData = {}; 340 sourceMapGenerator.fillSourceMapPackageInfo(moduleId, sourceMapData); 341 expect(sourceMapData['entry-package-info'] && sourceMapData['entry-package-info'] === entryPkgInfo).to.be.true; 342 SourceMapGenerator.cleanSourceMapObject(); 343 }); 344 345 mocha.it('4-2: test fillSourceMapPackageInfo under build release', function () { 346 this.rollup.build(RELEASE); 347 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 348 349 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 350 let sourceMapData = {}; 351 sourceMapGenerator.fillSourceMapPackageInfo(moduleId, sourceMapData); 352 expect(sourceMapData['entry-package-info'] && sourceMapData['entry-package-info'] === entryPkgInfo).to.be.true; 353 SourceMapGenerator.cleanSourceMapObject(); 354 }); 355 356 mocha.it('4-3: test fillSourceMapPackageInfo under preview', function () { 357 this.rollup.preview(); 358 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 359 360 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 361 let sourceMapData = {}; 362 sourceMapGenerator.fillSourceMapPackageInfo(moduleId, sourceMapData); 363 expect(sourceMapData['entry-package-info'] && sourceMapData['entry-package-info'] === entryPkgInfo).to.be.true; 364 SourceMapGenerator.cleanSourceMapObject(); 365 }); 366 367 mocha.it('4-4: test fillSourceMapPackageInfo under hotReload', function () { 368 this.rollup.hotReload(); 369 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 370 371 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 372 let sourceMapData = {}; 373 sourceMapGenerator.fillSourceMapPackageInfo(moduleId, sourceMapData); 374 expect(sourceMapData['entry-package-info'] && sourceMapData['entry-package-info'] === entryPkgInfo).to.be.true; 375 SourceMapGenerator.cleanSourceMapObject(); 376 }); 377 378 mocha.it('5-1: test genKey under build debug: arkProjectConfig.processTs is true', function () { 379 this.rollup.build(); 380 this.rollup.share.arkProjectConfig.processTs = true; 381 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 382 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 383 compilingEtsOrTsFiles.push(moduleId); 384 hasTsNoCheckOrTsIgnoreFiles.push(moduleId); 385 let genKey = sourceMapGenerator.genKey(moduleId); 386 cleanUpFilesList(); 387 let expectKey = prefix + ENTRYABILITY_JS_PATH_DEFAULT.substring(1); 388 expect(genKey === expectKey).to.be.true; 389 SourceMapGenerator.cleanSourceMapObject(); 390 }); 391 392 mocha.it('5-2: test genKey under build release: arkProjectConfig.processTs is true', function () { 393 this.rollup.build(RELEASE); 394 this.rollup.share.arkProjectConfig.processTs = true; 395 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 396 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 397 compilingEtsOrTsFiles.push(moduleId); 398 hasTsNoCheckOrTsIgnoreFiles.push(moduleId); 399 let genKey = sourceMapGenerator.genKey(moduleId); 400 cleanUpFilesList(); 401 let expectKey = prefix + ENTRYABILITY_JS_PATH_DEFAULT.substring(1); 402 expect(genKey === expectKey).to.be.true; 403 SourceMapGenerator.cleanSourceMapObject(); 404 }); 405 406 mocha.it('5-3: test genKey under preview: arkProjectConfig.processTs is true', function () { 407 this.rollup.preview(); 408 this.rollup.share.arkProjectConfig.processTs = true; 409 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 410 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 411 compilingEtsOrTsFiles.push(moduleId); 412 hasTsNoCheckOrTsIgnoreFiles.push(moduleId); 413 let genKey = sourceMapGenerator.genKey(moduleId); 414 cleanUpFilesList(); 415 let expectKey = prefix + ENTRYABILITY_JS_PATH_DEFAULT.substring(1); 416 expect(genKey === expectKey).to.be.true; 417 SourceMapGenerator.cleanSourceMapObject(); 418 }); 419 420 mocha.it('5-4: test genKey under hotReload: arkProjectConfig.processTs is true', function () { 421 this.rollup.hotReload(); 422 this.rollup.share.arkProjectConfig.processTs = true; 423 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 424 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT); 425 compilingEtsOrTsFiles.push(moduleId); 426 hasTsNoCheckOrTsIgnoreFiles.push(moduleId); 427 let genKey = sourceMapGenerator.genKey(moduleId); 428 cleanUpFilesList(); 429 let expectKey = prefix + ENTRYABILITY_JS_PATH_DEFAULT.substring(1); 430 expect(genKey === expectKey).to.be.true; 431 SourceMapGenerator.cleanSourceMapObject(); 432 }); 433 434 mocha.it('6-1: test updateCachedSourceMaps under build debug: arkProjectConfig.processTs is true', function () { 435 this.rollup.build(); 436 this.rollup.share.arkProjectConfig.processTs = true; 437 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, INDEX_ETS_PATH_DEFAULT); 438 compilingEtsOrTsFiles.push(moduleId); 439 hasTsNoCheckOrTsIgnoreFiles.push(moduleId); 440 const indexCachePath = this.rollup.share.projectConfig.projectRootPath + '/' + INDEX_JS_CACHE_PATH; 441 if (fs.existsSync(indexCachePath)) { 442 fs.rmSync(indexCachePath); 443 } 444 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 445 sourceMapGenerator.isNewSourceMap = false; 446 const rootPath = this.rollup.share.projectConfig.projectRootPath + '/cache/'; 447 const sourceMapEdit = JSON.parse(fs.readFileSync(rootPath + 'source_map_edit.json').toString()); 448 const sourceMapExpect = JSON.parse(fs.readFileSync(rootPath + 'source_map_expect.json').toString()); 449 sourceMapGenerator.cacheSourceMapPath = rootPath + 'source_map_cache.json'; 450 sourceMapGenerator.sourceMaps = sourceMapEdit; 451 const sourceMapResult = sourceMapGenerator.updateCachedSourceMaps(); 452 expect(Object.keys(sourceMapResult).length == Object.keys(sourceMapExpect).length).to.be.true; 453 Object.keys(sourceMapResult).forEach(key => { 454 expect(sourceMapExpect).to.include.keys(key); 455 }); 456 SourceMapGenerator.cleanSourceMapObject(); 457 }); 458 459 mocha.it('6-2: test updateCachedSourceMaps under build release: arkProjectConfig.processTs is true', function () { 460 this.rollup.build(RELEASE); 461 this.rollup.share.arkProjectConfig.processTs = true; 462 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, INDEX_ETS_PATH_DEFAULT); 463 compilingEtsOrTsFiles.push(moduleId); 464 hasTsNoCheckOrTsIgnoreFiles.push(moduleId); 465 const indexCachePath = this.rollup.share.projectConfig.projectRootPath + '/' + INDEX_JS_CACHE_PATH; 466 if (fs.existsSync(indexCachePath)) { 467 fs.rmSync(indexCachePath); 468 } 469 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 470 sourceMapGenerator.isNewSourceMap = false; 471 const rootPath = this.rollup.share.projectConfig.projectRootPath + '/cache/'; 472 const sourceMapEdit = JSON.parse(fs.readFileSync(rootPath + 'source_map_edit.json').toString()); 473 const sourceMapExpect = JSON.parse(fs.readFileSync(rootPath + 'source_map_expect.json').toString()); 474 sourceMapGenerator.cacheSourceMapPath = rootPath + 'source_map_cache.json'; 475 sourceMapGenerator.sourceMaps = sourceMapEdit; 476 const sourceMapResult = sourceMapGenerator.updateCachedSourceMaps(); 477 expect(Object.keys(sourceMapResult).length == Object.keys(sourceMapExpect).length).to.be.true; 478 Object.keys(sourceMapResult).forEach(key => { 479 expect(sourceMapExpect).to.include.keys(key); 480 }); 481 SourceMapGenerator.cleanSourceMapObject(); 482 }); 483 484 mocha.it('6-3: test updateCachedSourceMaps under preview: arkProjectConfig.processTs is true', function () { 485 this.rollup.preview(); 486 this.rollup.share.arkProjectConfig.processTs = true; 487 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, INDEX_ETS_PATH_DEFAULT); 488 compilingEtsOrTsFiles.push(moduleId); 489 hasTsNoCheckOrTsIgnoreFiles.push(moduleId); 490 const indexCachePath = this.rollup.share.projectConfig.projectRootPath + '/' + INDEX_JS_CACHE_PATH; 491 if (fs.existsSync(indexCachePath)) { 492 fs.rmSync(indexCachePath); 493 } 494 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 495 sourceMapGenerator.isNewSourceMap = false; 496 const rootPath = this.rollup.share.projectConfig.projectRootPath + '/cache/'; 497 const sourceMapEdit = JSON.parse(fs.readFileSync(rootPath + 'source_map_edit.json').toString()); 498 const sourceMapExpect = JSON.parse(fs.readFileSync(rootPath + 'source_map_expect_preview.json').toString()); 499 sourceMapGenerator.cacheSourceMapPath = rootPath + 'source_map_cache_preview.json'; 500 sourceMapGenerator.sourceMaps = sourceMapEdit; 501 const sourceMapResult = sourceMapGenerator.updateCachedSourceMaps(); 502 expect(Object.keys(sourceMapResult).length == Object.keys(sourceMapExpect).length).to.be.true; 503 Object.keys(sourceMapResult).forEach(key => { 504 expect(sourceMapExpect).to.include.keys(key); 505 }); 506 SourceMapGenerator.cleanSourceMapObject(); 507 }); 508 509 mocha.it('6-4: test updateCachedSourceMaps under hotReload: arkProjectConfig.processTs is true', function () { 510 this.rollup.hotReload(); 511 this.rollup.share.arkProjectConfig.processTs = true; 512 let moduleId = path.join(this.rollup.share.projectConfig.modulePath, INDEX_ETS_PATH_DEFAULT); 513 compilingEtsOrTsFiles.push(moduleId); 514 hasTsNoCheckOrTsIgnoreFiles.push(moduleId); 515 const indexCachePath = this.rollup.share.projectConfig.projectRootPath + '/' + INDEX_JS_CACHE_PATH; 516 if (fs.existsSync(indexCachePath)) { 517 fs.rmSync(indexCachePath); 518 } 519 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 520 sourceMapGenerator.isNewSourceMap = false; 521 const rootPath = this.rollup.share.projectConfig.projectRootPath + '/cache/'; 522 const sourceMapEdit = JSON.parse(fs.readFileSync(rootPath + 'source_map_edit.json').toString()); 523 const sourceMapExpect = JSON.parse(fs.readFileSync(rootPath + 'source_map_expect.json').toString()); 524 sourceMapGenerator.cacheSourceMapPath = rootPath + 'source_map_cache.json'; 525 sourceMapGenerator.sourceMaps = sourceMapEdit; 526 const sourceMapResult = sourceMapGenerator.updateCachedSourceMaps(); 527 expect(Object.keys(sourceMapResult).length == Object.keys(sourceMapExpect).length).to.be.true; 528 Object.keys(sourceMapResult).forEach(key => { 529 expect(sourceMapExpect).to.include.keys(key); 530 }); 531 SourceMapGenerator.cleanSourceMapObject(); 532 }); 533 534});