From eba8021363d65acf75b29fc7ba94baf407dcd851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Wed, 13 Nov 2024 11:29:16 +0100 Subject: [PATCH] fix(js): handle `outputFileName` correctly when generating package json --- .../package-json/update-package-json.spec.ts | 32 +++++++++++++++++++ .../utils/package-json/update-package-json.ts | 6 ++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packages/js/src/utils/package-json/update-package-json.spec.ts b/packages/js/src/utils/package-json/update-package-json.spec.ts index 784cd866c1530..e2d225a6b7e5b 100644 --- a/packages/js/src/utils/package-json/update-package-json.spec.ts +++ b/packages/js/src/utils/package-json/update-package-json.spec.ts @@ -405,6 +405,38 @@ describe('getUpdatedPackageJsonContent', () => { }, }); }); + + it('should handle outputFileName correctly', () => { + expect( + getUpdatedPackageJsonContent( + { + name: 'test', + version: '0.0.1', + }, + { + main: 'proj/src/index.ts', + outputPath: 'dist/proj', + projectRoot: 'proj', + format: ['cjs'], + generateExportsField: true, + outputFileName: 'src/index.js', + } + ) + ).toEqual({ + name: 'test', + main: './src/index.js', + types: './src/index.d.ts', + version: '0.0.1', + type: 'commonjs', + exports: { + '.': { + default: './src/index.js', + types: './src/index.d.ts', + }, + './package.json': './package.json', + }, + }); + }); }); describe('updatePackageJson', () => { diff --git a/packages/js/src/utils/package-json/update-package-json.ts b/packages/js/src/utils/package-json/update-package-json.ts index 523a12774c948..c8776a6710f16 100644 --- a/packages/js/src/utils/package-json/update-package-json.ts +++ b/packages/js/src/utils/package-json/update-package-json.ts @@ -242,7 +242,7 @@ export function getExports( ): Exports { const outputDir = getOutputDir(options); const mainFile = options.outputFileName - ? options.outputFileName.replace(/\.[tj]s$/, '') + ? basename(options.outputFileName).replace(/\.[tj]s$/, '') : basename(options.main).replace(/\.[tj]s$/, ''); const exports: Exports = { '.': outputDir + mainFile + options.fileExt, @@ -377,9 +377,9 @@ export function getOutputDir( : options.outputPath; const relativeOutputPath = relative(packageJsonDir, options.outputPath); const relativeMainDir = options.outputFileName - ? '' + ? dirname(options.outputFileName) : relative(options.rootDir ?? options.projectRoot, dirname(options.main)); - const outputDir = join(relativeOutputPath, relativeMainDir); + const outputDir = joinPathFragments(relativeOutputPath, relativeMainDir); return outputDir === '.' ? `./` : `./${outputDir}/`; }