Skip to content

Commit

Permalink
fix(js): handle outputFileName correctly when generating package js…
Browse files Browse the repository at this point in the history
…on (#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
  • Loading branch information
leosvelperez authored Nov 13, 2024
1 parent dbaeb86 commit 907e58f
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 907e58f

Please sign in to comment.