Skip to content

Commit

Permalink
fix(js): handle outputFileName correctly when generating package json
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Nov 13, 2024
1 parent 0392a67 commit eba8021
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
32 changes: 32 additions & 0 deletions packages/js/src/utils/package-json/update-package-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/js/src/utils/package-json/update-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}/`;
}

0 comments on commit eba8021

Please sign in to comment.