diff --git a/compiler/src/ark_utils.ts b/compiler/src/ark_utils.ts index 9c3684fe247cdd8dc8aef69c2a7ca0312d05f4dd..115ff2a6e5246dfa326615316eb84bc6fa331115 100644 --- a/compiler/src/ark_utils.ts +++ b/compiler/src/ark_utils.ts @@ -72,8 +72,42 @@ export const SRC_MAIN: string = 'src/main'; export var newSourceMaps: Object = {}; export var nameCacheObj: Object = {}; export const packageCollection: Map> = new Map(); - -export function getOhmUrlByFilepath(filePath: string, projectConfig: Object, logger: Object, namespace?: string, importerFile?: string): string { +// Splicing ohmurl or record name based on filePath and context information table. +export function getNormalizedOhmUrlByFilepath(filePath: string, projectConfig: Object, logger: Object, + pkgParams: Object, importerFile: string): string { + const {pkgName, pkgPath, isRecordName} = pkgParams; + // rollup uses commonjs plugin to handle commonjs files, + // the commonjs files are prefixed with '\x00' and need to be removed. + if (filePath.startsWith('\x00')) { + filePath = filePath.replace('\x00', ''); + } + let unixFilePath: string = toUnixPath(filePath); + unixFilePath = unixFilePath.substring(0, filePath.lastIndexOf('.')); // remove extension + let projectFilePath: string = unixFilePath.replace(toUnixPath(pkgPath), ''); + // case1: /entry/src/main/ets/xxx/yyy + // case2: /entry/src/ohosTest/ets/xxx/yyy + // case3: /node_modules/xxx/yyy + // case4: /entry/node_modules/xxx/yyy + // case5: /library/node_modules/xxx/yyy + // case6: /library/index.ts + // ---> @normalized:N&&&/entry/ets/xxx/yyy& + let pkgInfo = projectConfig.pkgContextInfo[pkgName]; + if (pkgInfo === undefined) { + logger.error(red, + `ArkTS:ERROR Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}". ` + + `Please check whether the module which ${filePath} belongs to is correctly configured ` + + `and the corresponding file name is correct(including case-sensitivity)`, reset); + } + let recordName = `${pkgInfo.bundleName}&${pkgName}${projectFilePath}&${pkgInfo.version}`; + if (isRecordName) { + // record name style: &/entry/ets/xxx/yyy& + return recordName; + } + return `${pkgInfo.isSO ? 'Y' : 'N'}&${pkgInfo.moduleName}&${recordName}`; +} + +export function getOhmUrlByFilepath(filePath: string, projectConfig: Object, logger: Object, namespace?: string, + importerFile?: string): string { // remove '\x00' from the rollup virtual commonjs file's filePath if (filePath.startsWith('\x00')) { filePath = filePath.replace('\x00', ''); @@ -153,8 +187,8 @@ function processPackageDir(params: Object): string { logger.error(red, `ArkTS:ERROR Failed to get a resolved OhmUrl for "${originalFilePath}" imported by "${importerFile}". ` + - `Please check whether the module which ${originalFilePath} belongs to is correctly configured` + - `and the corresponding file name matches (case sensitive)`, reset); + `Please check whether the module which ${originalFilePath} belongs to is correctly configured ` + + `and the corresponding file name is correct(including case-sensitivity)`, reset); return originalFilePath; } @@ -186,14 +220,14 @@ function processPackageDir(params: Object): string { logger.error(red, `ArkTS:ERROR Failed to get a resolved OhmUrl for "${originalFilePath}" imported by "${importerFile}". ` + - `Please check whether the module which ${originalFilePath} belongs to is correctly configured` + - `and the corresponding file name matches (case sensitive)`, reset); + `Please check whether the module which ${originalFilePath} belongs to is correctly configured ` + + `and the corresponding file name is correct(including case-sensitivity)`, reset); return originalFilePath; } -export function getOhmUrlBySystemApiOrLibRequest(moduleRequest: string) : string -{ +export function getOhmUrlBySystemApiOrLibRequest(moduleRequest: string, config?: Object, + useNormalizedOHMUrl: boolean = false) : string { // 'arkui-x' represents cross platform related APIs, processed as 'ohos' const REG_SYSTEM_MODULE: RegExp = new RegExp(`@(${sdkConfigPrefix})\\.(\\S+)`); const REG_LIB_SO: RegExp = /lib(\S+)\.so/; @@ -219,6 +253,11 @@ export function getOhmUrlBySystemApiOrLibRequest(moduleRequest: string) : string }); } if (REG_LIB_SO.test(moduleRequest.trim())) { + if (useNormalizedOHMUrl) { + const pkgInfo = config.pkgContextInfo[moduleRequest]; + const isSo = pkgInfo.isSO ? 'Y' : 'N'; + return `@normalized:${isSo}&${pkgInfo.moduleName}&${pkgInfo.bundleName}&${moduleRequest}&${pkgInfo.version}`; + } return moduleRequest.replace(REG_LIB_SO, (_, libsoKey) => { return `@app:${projectConfig.bundleName}/${projectConfig.moduleName}/${libsoKey}`; }); @@ -281,21 +320,57 @@ export function transformModuleSpecifier(sourcePath: string, sourceCode: string, }); } -export function getOhmUrlByHarName(moduleRequest: string, projectConfig: Object): string | undefined { +function removeSuffix(filePath: string) { + const SUFFIX_REG = /\.(?:d\.)?e?ts$/; + return filePath.split(path.sep).join('/').replace(SUFFIX_REG ,''); +} + +export function getNormalizedOhmUrlByHspName(aliasName: string, projectConfig: Object, + logger?: Object, filePath?: string) { + let pkgName: string = aliasName; + const aliasPkgNameMap: Map = projectConfig.dependencyAliasMap; + if (aliasPkgNameMap.has(aliasName)) { + pkgName = aliasPkgNameMap.get(aliasName); + } + const pkgInfo: Object = projectConfig.pkgContextInfo[pkgName]; + if (pkgInfo === undefined) { + logger.error(red, `ArkTS:INTERNAL ERROR: package ${pkgName} not found`, reset); + } + let normalizedPath: string = ''; + if (filePath === undefined) { + normalizedPath = `${pkgName}/${toUnixPath(pkgInfo.entryPath)}`; + normalizedPath = removeSuffix(normalizedPath); + } else { + const relativePath = toUnixPath(filePath).replace(aliasName, ''); + normalizedPath = `${pkgName}${relativePath}`; + } + const isSo = pkgInfo.isSO ? 'Y' : 'N'; + return `@normalized:${isSo}&${pkgInfo.moduleName}&${pkgInfo.bundleName}&${normalizedPath}&${pkgInfo.version}`; +} + +export function getOhmUrlByHspName(moduleRequest: string, projectConfig: Object, logger?: Object, + useNormalizedOHMUrl: boolean = false): string | undefined { + // The harNameOhmMap store the ohmurl with the alias of hsp package . if (projectConfig.harNameOhmMap) { // case1: "@ohos/lib" ---> "@bundle:bundleName/lib/ets/index" if (projectConfig.harNameOhmMap.hasOwnProperty(moduleRequest)) { + if(useNormalizedOHMUrl) { + return getNormalizedOhmUrlByHspName(moduleRequest, projectConfig); + } return projectConfig.harNameOhmMap[moduleRequest]; } // case2: "@ohos/lib/src/main/ets/pages/page1" ---> "@bundle:bundleName/lib/ets/pages/page1" - for (const harName in projectConfig.harNameOhmMap) { - if (moduleRequest.startsWith(harName + '/')) { - const idx: number = projectConfig.harNameOhmMap[harName].split('/', 2).join('/').length; - const harOhmName: string = projectConfig.harNameOhmMap[harName].substring(0, idx); - if (moduleRequest.indexOf(harName + '/' + SRC_MAIN) === 0) { - return moduleRequest.replace(harName + '/' + SRC_MAIN, harOhmName); + for (const hspName in projectConfig.harNameOhmMap) { + if (moduleRequest.startsWith(hspName + '/')) { + if(useNormalizedOHMUrl) { + return getNormalizedOhmUrlByHspName(hspName, projectConfig, logger, moduleRequest); + } + const idx: number = projectConfig.harNameOhmMap[hspName].split('/', 2).join('/').length; + const hspOhmName: string = projectConfig.harNameOhmMap[hspName].substring(0, idx); + if (moduleRequest.indexOf(hspName + '/' + SRC_MAIN) === 0) { + return moduleRequest.replace(hspName + '/' + SRC_MAIN, hspOhmName); } else { - return moduleRequest.replace(harName, harOhmName); + return moduleRequest.replace(hspName, hspOhmName); } } } @@ -304,10 +379,10 @@ export function getOhmUrlByHarName(moduleRequest: string, projectConfig: Object) } function replaceHarDependency(item:string, moduleRequest: string, projectConfig: Object): string { - const harOhmUrl: string | undefined = getOhmUrlByHarName(moduleRequest, projectConfig); - if (harOhmUrl !== undefined) { + const hspOhmUrl: string | undefined = getOhmUrlByHspName(moduleRequest, projectConfig); + if (hspOhmUrl !== undefined) { return item.replace(/(['"])(?:\S+)['"]/, (_, quotation) => { - return quotation + harOhmUrl + quotation; + return quotation + hspOhmUrl + quotation; }); } return item; diff --git a/compiler/src/fast_build/ark_compiler/module/module_mode.ts b/compiler/src/fast_build/ark_compiler/module/module_mode.ts index 21bfbdddf6b5c082e3deee174cfe8163bbaf8f4d..cda5f12897638d7be092fe8dbdcd3ca388b48cb1 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_mode.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_mode.ts @@ -75,8 +75,9 @@ import { } from '../../../utils'; import { getPackageInfo, + getNormalizedOhmUrlByFilepath, getOhmUrlByFilepath, - getOhmUrlByHarName, + getOhmUrlByHspName, isTs2Abc, isEs2Abc, createAndStartEvent, @@ -136,6 +137,7 @@ export class ModuleMode extends CommonMode { protoFilePath: string; filterModuleInfos: Map; symlinkMap: Object; + useNormalizedOHMUrl: boolean; constructor(rollupObject: Object) { super(rollupObject); @@ -157,6 +159,7 @@ export class ModuleMode extends CommonMode { this.hashJsonObject = {}; this.filterModuleInfos = new Map(); this.symlinkMap = rollupObject.share.symlinkMap; + this.useNormalizedOHMUrl = this.isUsingNormalizedOHMUrl(); } prepareForCompilation(rollupObject: Object, parentEvent: Object): void { @@ -174,18 +177,24 @@ export class ModuleMode extends CommonMode { continue; } const moduleInfo: Object = module.getModuleInfo(moduleId); - if (moduleInfo['meta']['isNodeEntryFile']) { + if (moduleInfo['meta']['isNodeEntryFile'] && !this.useNormalizedOHMUrl) { this.getPackageEntryInfo(moduleId, moduleInfo['meta'], pkgEntryInfos); } this.processModuleInfos(moduleId, moduleInfos, moduleInfo['meta']); } - this.getDynamicImportEntryInfo(pkgEntryInfos); - this.getNativeModuleEntryInfo(pkgEntryInfos); + if (!this.useNormalizedOHMUrl) { + this.getDynamicImportEntryInfo(pkgEntryInfos); + this.getNativeModuleEntryInfo(pkgEntryInfos); + } this.moduleInfos = moduleInfos; this.pkgEntryInfos = pkgEntryInfos; } + private isUsingNormalizedOHMUrl() { + return !!this.projectConfig.useNormalizedOHMUrl; + } + private updatePkgEntryInfos(pkgEntryInfos: Map, key: String, value: PackageEntryInfo): void { if (!pkgEntryInfos.has(key)) { pkgEntryInfos.set(key, new PackageEntryInfo(key, value)); @@ -203,7 +212,8 @@ export class ModuleMode extends CommonMode { this.updatePkgEntryInfos(pkgEntryInfos, pkgName, ohmurl); continue; } - let hspOhmurl: string | undefined = getOhmUrlByHarName(pkgName, this.projectConfig); + let hspOhmurl: string | undefined = getOhmUrlByHspName(pkgName, this.projectConfig, this.logger, + this.useNormalizedOHMUrl); if (hspOhmurl !== undefined) { hspOhmurl = hspOhmurl.replace(/^@(\w+):(.*)/, '@$1.$2'); this.updatePkgEntryInfos(pkgEntryInfos, pkgName, hspOhmurl); @@ -304,17 +314,28 @@ export class ModuleMode extends CommonMode { const isPackageModules = isPackageModulesFile(filePath, this.projectConfig); // if release mode, enable obfuscation, enable filename obfuscation -> call mangleFilePath() filePath = this.handleObfuscatedFilePath(filePath, isPackageModules); - let namespace: string = metaInfo['moduleName']; - let recordName: string = getOhmUrlByFilepath(filePath, this.projectConfig, this.logger, namespace); + let moduleName: string = metaInfo['moduleName']; + let recordName: string = ''; let sourceFile: string = filePath.replace(this.projectConfig.projectRootPath + path.sep, ''); let cacheFilePath: string = this.genFileCachePath(filePath, this.projectConfig.projectRootPath, this.projectConfig.cachePath); let packageName: string = ''; - if (isPackageModules) { - packageName = this.getPkgModulesFilePkgName(metaInfo['pkgPath']); + if (this.useNormalizedOHMUrl) { + packageName = metaInfo['pkgName']; + const pkgParams = { + pkgName: packageName, + pkgPath: metaInfo['pkgPath'], + isRecordName: true + }; + recordName = getNormalizedOhmUrlByFilepath(filePath, this.projectConfig, this.logger, pkgParams, undefined); } else { - packageName = - metaInfo['isLocalDependency'] ? namespace : getPackageInfo(this.projectConfig.aceModuleJsonPath)[1]; + recordName = getOhmUrlByFilepath(filePath, this.projectConfig, this.logger, moduleName); + if (isPackageModules) { + packageName = this.getPkgModulesFilePkgName(metaInfo['pkgPath']); + } else { + packageName = + metaInfo['isLocalDependency'] ? moduleName : getPackageInfo(this.projectConfig.aceModuleJsonPath)[1]; + } } if (extName.length !== 0) { diff --git a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts index 40fe8887c437b06f2a753ef93c2b4ee15feb0ec8..90a8fb47fe1e5fe1bb833c339708742b571f62cf 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts @@ -22,8 +22,9 @@ import { PACKAGES } from '../common/ark_define'; import { + getNormalizedOhmUrlByFilepath, getOhmUrlByFilepath, - getOhmUrlByHarName, + getOhmUrlByHspName, getOhmUrlBySystemApiOrLibRequest, mangleDeclarationFileName, } from '../../../ark_utils'; @@ -125,14 +126,25 @@ export class ModuleSourceFile { return; } + let useNormalizedOHMUrl = false; + if (rollupObject.share.projectConfig.hasOwnProperty('useNormalizedOHMUrl')) { + useNormalizedOHMUrl = rollupObject.share.projectConfig.useNormalizedOHMUrl; + } let mockFile: string = ModuleSourceFile.mockConfigInfo[originKey].source; let mockFilePath: string = `${toUnixPath(rollupObject.share.projectConfig.modulePath)}/${mockFile}`; - let mockFileOhmUrl: string = getOhmUrlByFilepath(mockFilePath, - ModuleSourceFile.projectConfig, - ModuleSourceFile.logger, - rollupObject.share.projectConfig.entryModuleName, - importerFile); - mockFileOhmUrl = mockFileOhmUrl.startsWith(PACKAGES) ? `@package:${mockFileOhmUrl}` : `@bundle:${mockFileOhmUrl}`; + let mockFileOhmUrl: string = ''; + if (useNormalizedOHMUrl) { + const targetModuleInfo: Object = rollupObject.getModuleInfo(filePath); + mockFileOhmUrl = ModuleSourceFile.spliceNormalizedOhmurl(targetModuleInfo, mockFilePath, importerFile); + } else { + mockFileOhmUrl = getOhmUrlByFilepath(mockFilePath, + ModuleSourceFile.projectConfig, + ModuleSourceFile.logger, + rollupObject.share.projectConfig.entryModuleName, + importerFile); + mockFileOhmUrl = mockFileOhmUrl.startsWith(PACKAGES) ? `@package:${mockFileOhmUrl}` : `@bundle:${mockFileOhmUrl}`; + } + // record mock target mapping for incremental compilation ModuleSourceFile.addNewMockConfig(transKey, mockFileOhmUrl); } @@ -275,26 +287,36 @@ export class ModuleSourceFile { } private getOhmUrl(rollupObject: Object, moduleRequest: string, filePath: string | undefined, importerFile: string): string | undefined { - let systemOrLibOhmUrl: string | undefined = getOhmUrlBySystemApiOrLibRequest(moduleRequest); + let useNormalizedOHMUrl = false; + if (rollupObject.share.projectConfig.hasOwnProperty('useNormalizedOHMUrl')) { + useNormalizedOHMUrl = rollupObject.share.projectConfig.useNormalizedOHMUrl; + } + let systemOrLibOhmUrl = getOhmUrlBySystemApiOrLibRequest(moduleRequest, ModuleSourceFile.projectConfig, useNormalizedOHMUrl); if (systemOrLibOhmUrl != undefined) { if (ModuleSourceFile.needProcessMock) { ModuleSourceFile.generateNewMockInfoByOrignMockConfig(moduleRequest, systemOrLibOhmUrl, rollupObject, importerFile); } return systemOrLibOhmUrl; } - const harOhmUrl: string | undefined = getOhmUrlByHarName(moduleRequest, ModuleSourceFile.projectConfig); - if (harOhmUrl !== undefined) { + const hspOhmurl: string | undefined = getOhmUrlByHspName(moduleRequest, ModuleSourceFile.projectConfig, + ModuleSourceFile.logger, useNormalizedOHMUrl); + if (hspOhmurl !== undefined) { if (ModuleSourceFile.needProcessMock) { - ModuleSourceFile.generateNewMockInfoByOrignMockConfig(moduleRequest, harOhmUrl, rollupObject, importerFile); + ModuleSourceFile.generateNewMockInfoByOrignMockConfig(moduleRequest, hspOhmurl, rollupObject, importerFile); } - return harOhmUrl; + return hspOhmurl; } if (filePath) { const targetModuleInfo: Object = rollupObject.getModuleInfo(filePath); - const namespace: string = targetModuleInfo['meta']['moduleName']; - const ohmUrl: string = - getOhmUrlByFilepath(filePath, ModuleSourceFile.projectConfig, ModuleSourceFile.logger, namespace, importerFile); - let res: string = ohmUrl.startsWith(PACKAGES) ? `@package:${ohmUrl}` : `@bundle:${ohmUrl}`; + let res: string = ""; + if (useNormalizedOHMUrl) { + res = ModuleSourceFile.spliceNormalizedOhmurl(targetModuleInfo, filePath, importerFile); + } else { + const moduleName: string = targetModuleInfo['meta']['moduleName']; + const ohmUrl: string = + getOhmUrlByFilepath(filePath, ModuleSourceFile.projectConfig, ModuleSourceFile.logger, moduleName, importerFile); + res = ohmUrl.startsWith(PACKAGES) ? `@package:${ohmUrl}` : `@bundle:${ohmUrl}`; + } if (ModuleSourceFile.needProcessMock) { // processing cases of har or lib mock targets ModuleSourceFile.generateNewMockInfoByOrignMockConfig(moduleRequest, res, rollupObject, importerFile); @@ -309,6 +331,18 @@ export class ModuleSourceFile { return undefined; } + private static spliceNormalizedOhmurl(moduleInfo: Object, filePath: string, importerFile: string): string { + const pkgParams = { + pkgName: moduleInfo['meta']['pkgName'], + pkgPath: moduleInfo['meta']['pkgPath'], + isRecordName: false + }; + const ohmUrl: string = + getNormalizedOhmUrlByFilepath(filePath, ModuleSourceFile.projectConfig, ModuleSourceFile.logger, pkgParams, + importerFile); + return `@normalized:${ohmUrl}`; + } + private processJsModuleRequest(rollupObject: Object): void { const moduleInfo: Object = rollupObject.getModuleInfo(this.moduleId); const importMap: Object = moduleInfo.importedIdMaps; diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/module_info.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/module_info.ts index 24ef5c9200971bc3b6f304b06c6e05d9bc57e91b..b04c994eb62bf7f8acfe85a15a5735e3b1092a7e 100644 --- a/compiler/test/ark_compiler_ut/mock/rollup_mock/module_info.ts +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/module_info.ts @@ -18,13 +18,15 @@ import { MODULE_ID_ROLLUP_PLACEHOLDER } from '../rollup_mock/path_config'; class Meta { hostModulesInfo: Array; moduleName: string; + pkgName: string; isLocalDependency: boolean; isNodeEntryFile: boolean; pkgPath: string; - constructor(entryModuleName: string, modulePath: string) { + constructor(entryModuleName: string, modulePath: string, packageName: string) { this.hostModulesInfo = []; this.moduleName = entryModuleName; + this.pkgName = packageName; this.isLocalDependency = true; this.isNodeEntryFile = false; this.pkgPath = modulePath; diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts index 18bc552d48ad20b1df39785a2513ebe95770a0cf..db92c7b101ec5892d1a8e6c43a1e68ed3fb1e9fe 100644 --- a/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts @@ -107,6 +107,9 @@ class ProjectConfig { aceSoPath?: string; mockParams?: object; projectRootPath: string; + pkgContextInfo: object; + useNormalizedOHMUrl: boolean = false; + dependencyAliasMap: Map; constructor(buildMode: string) { this.watchMode = 'false'; diff --git a/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts b/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts index 887dad5684bc27805a31f6799eeeb54c7bc0d2a2..c98f599e623a51371b3ce726663961bb0c275540 100644 --- a/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts +++ b/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts @@ -17,12 +17,17 @@ import { expect } from 'chai'; import mocha from 'mocha'; import sinon from 'sinon'; -import { getOhmUrlByFilepath, getOhmUrlBySystemApiOrLibRequest, getOhmUrlByHarName } from '../../../../lib/ark_utils'; +import { + getOhmUrlByFilepath, + getOhmUrlByHspName, + getOhmUrlBySystemApiOrLibRequest +} from '../../../../lib/ark_utils'; import { PACKAGES } from '../../../../lib/pre_define'; import projectConfig from '../../utils/processProjectConfig'; import { projectConfig as mainProjectConfig } from '../../../../main'; import RollUpPluginMock from '../../mock/rollup_mock/rollup_plugin_mock'; import { GEN_ABC_PLUGIN_NAME } from '../../../../lib/fast_build/ark_compiler/common/ark_define'; +import { ModuleSourceFile } from '../../../../lib/fast_build/ark_compiler/module/module_source_file'; mocha.describe('generate ohmUrl', function () { mocha.before(function () { @@ -34,10 +39,10 @@ mocha.describe('generate ohmUrl', function () { }); mocha.it('nested src main ets|js in filePath', function () { - const filePath = `${projectConfig.projectRootPath}/entry/src/main/ets/feature/src/main/js/` + const filePath: string = `${projectConfig.projectRootPath}/entry/src/main/ets/feature/src/main/js/` + `subfeature/src/main/ets/pages/test.ts`; - const moduleName = 'entry'; - const moduleNamespace = 'library'; + const moduleName: string = 'entry'; + const moduleNamespace: string = 'library'; let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; @@ -47,10 +52,10 @@ mocha.describe('generate ohmUrl', function () { }); mocha.it('nested src ohosTest ets|js in filePath', function () { - const filePath = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/feature/src/main/js/` + const filePath: string = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/feature/src/main/js/` + `subfeature/src/main/ets/pages/test.ts`; - const moduleName = 'entry'; - const moduleNamespace = 'library'; + const moduleName: string = 'entry'; + const moduleNamespace: string = 'library'; let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; @@ -80,9 +85,9 @@ mocha.describe('generate ohmUrl', function () { const sharedLibraryPackageName: string = "@ohos/sharedLibrary"; const sharedLibraryPage: string = "@ohos/sharedLibrary/src/main/ets/pages/page1"; const errorSharedLibrary: string = "@ohos/staticLibrary"; - const sharedLibraryPackageNameOhmUrl: string = getOhmUrlByHarName(sharedLibraryPackageName, projectConfig); - const sharedLibraryPageOhmUrl: string = getOhmUrlByHarName(sharedLibraryPage, projectConfig); - const errorSharedLibraryOhmUrl = getOhmUrlByHarName(errorSharedLibrary, projectConfig); + const sharedLibraryPackageNameOhmUrl: string = getOhmUrlByHspName(sharedLibraryPackageName, projectConfig); + const sharedLibraryPageOhmUrl: string = getOhmUrlByHspName(sharedLibraryPage, projectConfig); + const errorSharedLibraryOhmUrl = getOhmUrlByHspName(errorSharedLibrary, projectConfig); const expectedSharedLibraryOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/index"; const expectedSharedLibraryPageOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/pages/page1"; const expectedErrorSharedLibraryOhmUrl = undefined; @@ -92,10 +97,10 @@ mocha.describe('generate ohmUrl', function () { }); mocha.it('project module', function () { - const filePath = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/test.ts`; + const filePath: string = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/test.ts`; const harFilePath = `${projectConfig.projectRootPath}/library/src/main/ets/pages/test.ts`; - const moduleName = 'entry'; - const moduleNamespace = 'library'; + const moduleName: string = 'entry'; + const moduleNamespace: string = 'library'; const ohmUrl = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); const harOhmUrl = getOhmUrlByFilepath(harFilePath, projectConfig, undefined, moduleNamespace); const expected = 'UtTestApplication/entry/ets/pages/test'; @@ -107,7 +112,7 @@ mocha.describe('generate ohmUrl', function () { mocha.it('thirdParty module', function () { const moduleLevelPkgPath = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; const projectLevelPkgPath = `${projectConfig.projectRootPath}/oh_modules/json5/dist/index.js`; - const moduleName = 'entry'; + const moduleName: string = 'entry'; const moduleLevelPkgOhmUrl = getOhmUrlByFilepath(moduleLevelPkgPath, projectConfig, undefined, undefined); const projectLevelPkgOhmUrl = getOhmUrlByFilepath(projectLevelPkgPath, projectConfig, undefined, undefined); const moduleLevelPkgOhmUrlExpected = `${PACKAGES}@${moduleName}/json5/dist/index`; @@ -118,7 +123,7 @@ mocha.describe('generate ohmUrl', function () { mocha.it('static library entry', function () { const staticLibraryEntry = `${projectConfig.projectRootPath}/library/index.ets`; - const moduleNamespace = 'library'; + const moduleNamespace: string = 'library'; const staticLibraryEntryOhmUrl = getOhmUrlByFilepath(staticLibraryEntry, projectConfig, undefined, moduleNamespace); const staticLibraryEntryOhmUrlExpected = 'UtTestApplication/entry@library/index'; @@ -127,7 +132,7 @@ mocha.describe('generate ohmUrl', function () { mocha.it('ohosTest module', function () { const ohosTestfilePath = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/pages/test.ts`; - const moduleName = 'entry'; + const moduleName: string = 'entry'; const ohmUrl = getOhmUrlByFilepath(ohosTestfilePath, projectConfig, undefined, moduleName); const expected = 'UtTestApplication/entry/ets/pages/test'; expect(ohmUrl == expected).to.be.true; @@ -138,16 +143,16 @@ mocha.describe('generate ohmUrl', function () { projectConfig.modulePathMap = {}; const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; - const filePath = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; - const moduleName = 'entry'; - const importerFile = 'importTest.ts'; + const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; + const moduleName: string = 'entry'; + const importerFile: string = 'importTest.ts'; const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME) const loggerStub = sinon.stub(logger, 'error'); getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); expect(loggerStub.calledWith(red, `ArkTS:ERROR Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}". ` + - `Please check whether the module which ${filePath} belongs to is correctly configured` + - `and the corresponding file name matches (case sensitive)`, reset)).to.be.true; + `Please check whether the module which ${filePath} belongs to is correctly configured ` + + `and the corresponding file name is correct(including case-sensitivity)`, reset)).to.be.true; loggerStub.restore(); }); @@ -157,16 +162,689 @@ mocha.describe('generate ohmUrl', function () { projectConfig.modulePathMap = {}; const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; - const filePath = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; - const moduleName = 'entry'; - const importerFile = 'importTest.ts'; + const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; + const moduleName: string = 'entry'; + const importerFile: string = 'importTest.ts'; const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME) const loggerStub = sinon.stub(logger, 'error'); getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); expect(loggerStub.calledWith(red, `ArkTS:ERROR Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}". ` + - `Please check whether the module which ${filePath} belongs to is correctly configured` + - `and the corresponding file name matches (case sensitive)`, reset)).to.be.true; + `Please check whether the module which ${filePath} belongs to is correctly configured ` + + `and the corresponding file name is correct(including case-sensitivity)`, reset)).to.be.true; loggerStub.restore(); }); + + mocha.it('NormalizedOHMUrl inter-app hsp self import', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghsp': { + 'packageName': 'pkghsp', + 'bundleName': 'com.test.testHsp', + 'moduleName': '', + 'version': '', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHsp/hsp/src/main/ets/utils/Calc.ets'; + const moduleInfo = { + id: filePath, + meta: { + pkgName: 'pkghsp', + pkgPath: '/testHsp/hsp' + } + } + this.rollup.moduleInfos.push(moduleInfo); + const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' + const relativePath: string = '../utils/Calc'; + const etsBasedAbsolutePath: string = 'ets/utils/Calc'; + const standardImportPath: string = 'pkghsp/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); + const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, + importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghsp/src/main/ets/utils/Calc&'; + expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl inter-app hsp others import', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghsp': { + 'packageName': 'pkghsp', + 'bundleName': 'com.test.testHsp', + 'moduleName': 'hsp', + 'version': '', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + this.rollup.share.projectConfig.dependencyAliasMap = new Map([ + ['pkghsp_alias', 'pkghsp'] + ]); + this.rollup.share.projectConfig.harNameOhmMap = { + 'pkghsp_alias': '@bundle:com.test.testHsp/src/main/ets/utils/Calc' + } + const filePath: string = 'pkghsp/src/main/ets/utils/Calc'; + const indexFilePath: string = 'pkghsp_alias'; + const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' + const importByPkgName = 'pkghsp_alias'; + const standardImportPath: string = 'pkghsp_alias/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&hsp&com.test.testHsp&pkghsp/Index&'; + const standardImportPathNormalizedOhmUrl: string = + '@normalized:N&hsp&com.test.testHsp&pkghsp/src/main/ets/utils/Calc&'; + expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl in-app hsp self import', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghsp': { + 'packageName': 'pkghsp', + 'bundleName': '', + 'moduleName': '', + 'version': '', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHsp/hsp/src/main/ets/utils/Calc.ets'; + const moduleInfo = { + id: filePath, + meta: { + pkgName: 'pkghsp', + pkgPath: '/testHsp/hsp' + } + } + this.rollup.moduleInfos.push(moduleInfo); + const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' + const relativePath: string = '../utils/Calc'; + const etsBasedAbsolutePath: string = 'ets/utils/Calc'; + const standardImportPath: string = 'pkghsp/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); + const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, + importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:N&&&pkghsp/src/main/ets/utils/Calc&'; + expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl in-app hsp others import', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghsp': { + 'packageName': 'pkghsp', + 'bundleName': '', + 'moduleName': 'hsp', + 'version': '', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + this.rollup.share.projectConfig.dependencyAliasMap = new Map([ + ['pkghsp_alias', 'pkghsp'] + ]); + this.rollup.share.projectConfig.harNameOhmMap = { + 'pkghsp_alias': '@bundle:com.test.testHap/src/main/ets/utils/Calc' + } + const filePath: string = 'pkghsp_alias/src/main/ets/utils/Calc'; + const indexFilePath: string = 'pkghsp_alias'; + + const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' + const importByPkgName = 'pkghsp_alias'; + const standardImportPath: string = 'pkghsp_alias/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&hsp&&pkghsp/Index&'; + const standardImportPathNormalizedOhmUrl: string = '@normalized:N&hsp&&pkghsp/src/main/ets/utils/Calc&'; + expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl hap self import', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'entry': { + 'packageName': 'entry', + 'bundleName': '', + 'moduleName': '', + 'version': '', + 'entryPath': '', + 'isSO': false + } + } + const filePath: string = '/testHap/entry/src/main/ets/utils/Calc.ets'; + const moduleInfo = { + id: filePath, + meta: { + pkgName: 'entry', + pkgPath: '/testHap/entry' + } + } + this.rollup.moduleInfos.push(moduleInfo); + const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' + const relativePath: string = '../utils/Calc'; + const etsBasedAbsolutePath: string = 'ets/utils/Calc'; + const standardImportPath: string = 'entry/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); + const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, + importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:N&&&entry/src/main/ets/utils/Calc&'; + expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl source code har self import (hap/in-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghar': { + 'packageName': 'pkghar', + 'bundleName': '', + 'moduleName': '', + 'version': '1.0.1', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; + const moduleInfo = { + id: filePath, + meta: { + pkgName: 'pkghar', + pkgPath: '/testHar/har' + } + } + this.rollup.moduleInfos.push(moduleInfo); + const importerFile: string = '/testHar/har/src/main/ets/pages/Index.ets' + const relativePath: string = '../utils/Calc'; + const etsBasedAbsolutePath: string = 'ets/utils/Calc'; + const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); + const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, + importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; + expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl source code har others import (hap/in-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghar': { + 'packageName': 'pkghar', + 'bundleName': '', + 'moduleName': '', + 'version': '1.0.1', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; + const indexFilePath: string = '/testHar/har/Index.ets'; + for (let file of [filePath, indexFilePath]) { + const moduleInfo = { + id: file, + meta: { + pkgName: 'pkghar', + pkgPath: '/testHar/har' + } + } + this.rollup.moduleInfos.push(moduleInfo); + } + const importerFile: string = '/testHar/entry/src/main/ets/pages/Index.ets' + const importByPkgName = 'pkghar'; + const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&pkghar/Index&1.0.1'; + const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; + expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl source code har self import (inter-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghar': { + 'packageName': 'pkghar', + 'bundleName': 'com.test.testHsp', + 'moduleName': '', + 'version': '1.0.1', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; + const moduleInfo = { + id: filePath, + meta: { + pkgName: 'pkghar', + pkgPath: '/testHsp/har' + } + } + this.rollup.moduleInfos.push(moduleInfo); + const importerFile: string = '/testHsp/har/src/main/ets/pages/Index.ets' + const relativePath: string = '../utils/Calc'; + const etsBasedAbsolutePath: string = 'ets/utils/Calc'; + const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); + const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, + importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; + expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl source code har others import (inter-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghar': { + 'packageName': 'pkghar', + 'bundleName': 'com.test.testHsp', + 'moduleName': '', + 'version': '1.0.1', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; + const indexFilePath: string = '/testHsp/har/Index.ets'; + for (let file of [filePath, indexFilePath]) { + const moduleInfo = { + id: file, + meta: { + pkgName: 'pkghar', + pkgPath: '/testHsp/har' + } + } + this.rollup.moduleInfos.push(moduleInfo); + } + const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' + const importByPkgName = 'pkghar'; + const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/Index&1.0.1'; + const standardImportPathNormalizedOhmUrl: string = + '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; + expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl product har self import (hap/in-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghar': { + 'packageName': 'pkghar', + 'bundleName': '', + 'moduleName': '', + 'version': '1.0.1', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; + const moduleInfo = { + id: filePath, + meta: { + pkgName: 'pkghar', + pkgPath: '/testHar/har' + } + } + this.rollup.moduleInfos.push(moduleInfo); + const importerFile: string = '/testHar/har/src/main/ets/pages/Index.ets' + const relativePath: string = '../utils/Calc'; + const etsBasedAbsolutePath: string = 'ets/utils/Calc'; + const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); + const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, + importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; + expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl product har others import (hap/in-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghar': { + 'packageName': 'pkghar', + 'bundleName': '', + 'moduleName': '', + 'version': '1.0.1', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHap/oh_modules/.ohpm/pkghar@test=/oh_modules/pkghar/src/main/ets/utils/Calc.ets'; + const indexFilePath: string = '/testHap/oh_modules/.ohpm/pkghar@test=/oh_modules/pkghar/Index.ets'; + for (let file of [filePath, indexFilePath]) { + const moduleInfo = { + id: file, + meta: { + pkgName: 'pkghar', + pkgPath: '/testHap/oh_modules/.ohpm/pkghar@test=/oh_modules/pkghar' + } + } + this.rollup.moduleInfos.push(moduleInfo); + } + const importerFile: string = '/testHar/entry/src/main/ets/pages/index.ets' + const importByPkgName = 'pkghar'; + const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&pkghar/Index&1.0.1'; + const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; + expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl remote source code har self import (inter-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghar': { + 'packageName': 'pkghar', + 'bundleName': 'com.test.testHsp', + 'moduleName': '', + 'version': '1.0.1', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; + const moduleInfo = { + id: filePath, + meta: { + pkgName: 'pkghar', + pkgPath: '/testHsp/har' + } + } + this.rollup.moduleInfos.push(moduleInfo); + const importerFile: string = '/testHsp/har/src/main/ets/pages/Index.ets' + const relativePath: string = '../utils/Calc'; + const etsBasedAbsolutePath: string = 'ets/utils/Calc'; + const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); + const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, + importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; + expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl remote source code har others import (inter-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'pkghar': { + 'packageName': 'pkghar', + 'bundleName': 'com.test.testHsp', + 'moduleName': '', + 'version': '1.0.1', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; + const indexFilePath: string = '/testHsp/har/Index.ets'; + for (let file of [filePath, indexFilePath]) { + const moduleInfo = { + id: file, + meta: { + pkgName: 'pkghar', + pkgPath: '/testHsp/har' + } + } + this.rollup.moduleInfos.push(moduleInfo); + } + const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' + const importByPkgName = 'pkghar'; + const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/Index&1.0.1'; + const standardImportPathNormalizedOhmUrl: string = + '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; + expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl native so others import (hap/in-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'libproduct.so': { + 'packageName': 'libproduct.so', + 'bundleName': '', + 'moduleName': '', + 'version': '', + 'entryPath': '', + 'isSO': true + } + } + const importerFile: string = '/testHap/hsp/src/main/ets/pages/Index.ets' + const moduleRequest = 'libproduct.so'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:Y&&&libproduct.so&'; + expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl native so others import (inter-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'libproduct.so': { + 'packageName': 'libproduct.so', + 'bundleName': 'com.test.testHsp', + 'moduleName': '', + 'version': '', + 'entryPath': '', + 'isSO': true + } + } + const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' + const moduleRequest = 'libproduct.so'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:Y&&com.test.testHsp&libproduct.so&'; + expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl native so others import (source code har)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'libhar.so': { + 'packageName': 'libhar.so', + 'bundleName': '', + 'moduleName': '', + 'version': '', + 'entryPath': '', + 'isSO': true + } + } + const importerFile: string = '/testHap/har/src/main/ets/pages/Index.ets' + const moduleRequest = 'libhar.so'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:Y&&&libhar.so&'; + expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl native so others import (product har)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + 'libhar.so': { + 'packageName': 'libhar.so', + 'bundleName': '', + 'moduleName': '', + 'version': '', + 'entryPath': '', + 'isSO': true + } + } + const importerFile: string = + '/testHap/oh_modules/.ohpm/pkghar@test+har=/oh_modules/pkghar/src/main/ets/pages/Index.ets'; + const moduleRequest = 'libhar.so'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); + const expectedNormalizedOhmUrl: string = '@normalized:Y&&&libhar.so&'; + expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl ohpm package others import (hap/in-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + '@ohos/Test': { + 'packageName': '@ohos/Test', + 'bundleName': '', + 'moduleName': '', + 'version': '2.3.1', + 'entryPath': 'index.ets', + 'isSO': false + } + } + const filePath: string = + '/testHap/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/src/main/ets/utils/Calc.ets' + const indexFilePath: string = '/testHap/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/index.ets'; + for (let file of [filePath, indexFilePath]) { + const moduleInfo = { + id: file, + meta: { + pkgName: '@ohos/Test', + pkgPath: '/testHap/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test' + } + } + this.rollup.moduleInfos.push(moduleInfo); + } + const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' + const importByPkgName = '@ohos/Test'; + const standardImportPath: string = '@ohos/Test/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&@ohos/Test/index&2.3.1'; + const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&@ohos/Test/src/main/ets/utils/Calc&2.3.1'; + expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; + }); + + mocha.it('NormalizedOHMUrl ohpm package others import (inter-app hsp)', function () { + this.rollup.build(); + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + this.rollup.share.projectConfig.pkgContextInfo = { + '@ohos/Test': { + 'packageName': '@ohos/Test', + 'bundleName': 'com.test.testHsp', + 'moduleName': '', + 'version': '2.3.1', + 'entryPath': 'index.ets', + 'isSO': false + } + } + const filePath: string = + '/testHsp/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/src/main/ets/utils/Calc.ets' + const indexFilePath: string = '/testHsp/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/index.ets'; + for (let file of [filePath, indexFilePath]) { + const moduleInfo = { + id: file, + meta: { + pkgName: '@ohos/Test', + pkgPath: '/testHsp/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test' + } + } + this.rollup.moduleInfos.push(moduleInfo); + } + const importerFile: string = '/testHsp/entry/src/main/ets/pages/index.ets' + const importByPkgName = '@ohos/Test'; + const standardImportPath: string = '@ohos/Test/src/main/ets/utils/Calc'; + const moduleSourceFile: string = new ModuleSourceFile(); + ModuleSourceFile.initPluginEnv(this.rollup); + const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); + const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, + importerFile); + const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&@ohos/Test/index&2.3.1'; + const standardImportPathNormalizedOhmUrl: string = + '@normalized:N&&com.test.testHsp&@ohos/Test/src/main/ets/utils/Calc&2.3.1'; + expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; + expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; + }); }); \ No newline at end of file