Fix incorrectly generating module declaration files outside of indepen…

(cherry picked commit from <gitee.com//openharmony/developtools_ace_ets2bundle/commit/81603fafb872b65195708bb139463edf70bb6b04>
Signed-off-by: wuhailong <wuhailong22@huawei.com>
This commit is contained in:
wuhailong 2024-08-01 15:56:19 +08:00 committed by 吴海龙
parent 1bdab675fd
commit d4de873191
3 changed files with 36 additions and 6 deletions

View File

@ -38,7 +38,8 @@ import {
validateFilePathLength,
toUnixPath,
isPackageModulesFile,
getProjectRootPath
getProjectRootPath,
isFileInProject
} from '../../utils';
import {
tryMangleFileName,
@ -205,13 +206,13 @@ export function isCommonJsPluginVirtualFile(filePath: string): boolean {
export function isCurrentProjectFiles(filePath: string, projectConfig: Object): boolean {
if (projectConfig.rootPathSet) {
for (const projectRootPath of projectConfig.rootPathSet) {
if (filePath.indexOf(projectRootPath) !== -1) {
if (isFileInProject(filePath, projectRootPath)) {
return true;
}
}
return false;
}
return filePath.indexOf(projectConfig.projectRootPath) >= 0;
return isFileInProject(filePath, projectConfig.projectRootPath);
}
export function genTemporaryModuleCacheDirectoryForBundle(projectConfig: Object): string {

View File

@ -1247,13 +1247,19 @@ export function removeDecorator(decorators: readonly ts.Decorator[], decoratorNa
});
}
export function isFileInProject(filePath: string, projectRootPath: string): boolean {
const relativeFilePath: string = toUnixPath(path.relative(toUnixPath(projectRootPath), toUnixPath(filePath)));
// When processing ohmurl, hsp's filePath is consistent with moduleRequest
return fs.existsSync(filePath) && fs.statSync(filePath).isFile() && !relativeFilePath.startsWith('../');
}
export function getProjectRootPath(filePath: string, projectConfig: Object, rootPathSet: Object): string {
if (rootPathSet) {
for (const rootPath of rootPathSet) {
if (toUnixPath(filePath).indexOf(toUnixPath(rootPath)) !== -1) {
if (isFileInProject(filePath, rootPath)) {
return rootPath;
}
}
}
return projectConfig.projectRootPath;
}
}

View File

@ -1313,12 +1313,35 @@ mocha.describe('test utils file api', function () {
const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets';
this.rollup.share.projectConfig.rootPathSet = ['/testHar', `${PROJECT_ROOT}/${DEFAULT_PROJECT}`];
const projectConfig = this.rollup.share.projectConfig;
const existsSyncStub = sinon.stub(fs, 'existsSync').returns(true);
const statSyncStub = sinon.stub(fs, 'statSync').returns({
isFile: sinon.stub().returns(true)
});
const projectRootPath: string = getProjectRootPath(filePath, projectConfig, projectConfig.rootPathSet);
const expectProjectConfig: string = '/testHar';
expect(projectRootPath === expectProjectConfig).to.be.true;
existsSyncStub.restore();
statSyncStub.restore();
});
mocha.it('17-2: test getProjectRootPath under build', function () {
mocha.it('17-2: test getProjectRootPath adapt external modules(multiple project names contain a relationship)',
function () {
this.rollup.build();
const filePath: string = '/project/testA/har/src/main/ets/utils/Calc.ets';
this.rollup.share.projectConfig.rootPathSet = ['/project/test', '/project/testA', '/project/testAB'];
const projectConfig = this.rollup.share.projectConfig;
const existsSyncStub = sinon.stub(fs, 'existsSync').returns(true);
const statSyncStub = sinon.stub(fs, 'statSync').returns({
isFile: sinon.stub().returns(true)
});
const projectRootPath: string = getProjectRootPath(filePath, projectConfig, projectConfig.rootPathSet);
const expectProjectConfig: string = '/project/testA';
expect(projectRootPath === expectProjectConfig).to.be.true;
existsSyncStub.restore();
statSyncStub.restore();
});
mocha.it('17-3: test getProjectRootPath under build', function () {
this.rollup.build();
const filePath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har/src/main/ets/utils/Calc.ets`;
const projectConfig = this.rollup.share.projectConfig;