Skip to content

Commit 2ee80a1

Browse files
leosvelperezFrozenPandaz
authored andcommitted
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 (cherry picked from commit 907e58f)
1 parent 80fec8b commit 2ee80a1

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

packages/js/src/utils/package-json/update-package-json.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,38 @@ describe('getUpdatedPackageJsonContent', () => {
405405
},
406406
});
407407
});
408+
409+
it('should handle outputFileName correctly', () => {
410+
expect(
411+
getUpdatedPackageJsonContent(
412+
{
413+
name: 'test',
414+
version: '0.0.1',
415+
},
416+
{
417+
main: 'proj/src/index.ts',
418+
outputPath: 'dist/proj',
419+
projectRoot: 'proj',
420+
format: ['cjs'],
421+
generateExportsField: true,
422+
outputFileName: 'src/index.js',
423+
}
424+
)
425+
).toEqual({
426+
name: 'test',
427+
main: './src/index.js',
428+
types: './src/index.d.ts',
429+
version: '0.0.1',
430+
type: 'commonjs',
431+
exports: {
432+
'.': {
433+
default: './src/index.js',
434+
types: './src/index.d.ts',
435+
},
436+
'./package.json': './package.json',
437+
},
438+
});
439+
});
408440
});
409441

410442
describe('updatePackageJson', () => {

packages/js/src/utils/package-json/update-package-json.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export function getExports(
242242
): Exports {
243243
const outputDir = getOutputDir(options);
244244
const mainFile = options.outputFileName
245-
? options.outputFileName.replace(/\.[tj]s$/, '')
245+
? basename(options.outputFileName).replace(/\.[tj]s$/, '')
246246
: basename(options.main).replace(/\.[tj]s$/, '');
247247
const exports: Exports = {
248248
'.': outputDir + mainFile + options.fileExt,
@@ -377,9 +377,9 @@ export function getOutputDir(
377377
: options.outputPath;
378378
const relativeOutputPath = relative(packageJsonDir, options.outputPath);
379379
const relativeMainDir = options.outputFileName
380-
? ''
380+
? dirname(options.outputFileName)
381381
: relative(options.rootDir ?? options.projectRoot, dirname(options.main));
382-
const outputDir = join(relativeOutputPath, relativeMainDir);
382+
const outputDir = joinPathFragments(relativeOutputPath, relativeMainDir);
383383

384384
return outputDir === '.' ? `./` : `./${outputDir}/`;
385385
}

0 commit comments

Comments
 (0)