fix(js): handle outputFileName correctly when generating package json (#28915)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #28894
This commit is contained in:
Leosvel Pérez Espinosa 2024-11-13 15:43:01 +01:00 committed by GitHub
parent dbaeb86102
commit 907e58fda7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 3 deletions

View File

@ -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', () => {

View File

@ -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}/`;
}