From 232e2db6741f7b3daa1b51cab7a341fbcb1a65ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Thu, 25 Jul 2024 12:47:50 +0200 Subject: [PATCH] fix(js): generate exports without the filename for barrel files --- .../package-json/update-package-json.spec.ts | 25 +++++++++++++++++-- .../utils/package-json/update-package-json.ts | 7 +++++- 2 files changed, 29 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 2a9b8020615b0..56acde3b54455 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 @@ -200,6 +200,7 @@ describe('getUpdatedPackageJsonContent', () => { 'proj/src/foo.ts', 'proj/src/bar.ts', 'proj/migrations.json', + 'proj/feature/index.ts', ], outputPath: 'dist/proj', projectRoot: 'proj', @@ -219,6 +220,8 @@ describe('getUpdatedPackageJsonContent', () => { './bar': './src/bar.js', './package.json': './package.json', './migrations.json': './migrations.json', + './feature': './feature/index.js', + './feature/index': './feature/index.js', }, }); @@ -231,7 +234,11 @@ describe('getUpdatedPackageJsonContent', () => { }, { main: 'proj/src/index.ts', - additionalEntryPoints: ['proj/src/foo.ts', 'proj/src/bar.ts'], + additionalEntryPoints: [ + 'proj/src/foo.ts', + 'proj/src/bar.ts', + 'proj/feature/index.ts', + ], outputPath: 'dist/proj', projectRoot: 'proj', format: ['esm'], @@ -250,6 +257,8 @@ describe('getUpdatedPackageJsonContent', () => { './foo': './src/foo.js', './bar': './src/bar.js', './package.json': './package.json', + './feature': './feature/index.js', + './feature/index': './feature/index.js', }, }); @@ -262,7 +271,11 @@ describe('getUpdatedPackageJsonContent', () => { }, { main: 'proj/src/index.ts', - additionalEntryPoints: ['proj/src/foo.ts', 'proj/src/bar.ts'], + additionalEntryPoints: [ + 'proj/src/foo.ts', + 'proj/src/bar.ts', + 'proj/feature/index.ts', + ], outputPath: 'dist/proj', projectRoot: 'proj', format: ['cjs', 'esm'], @@ -289,6 +302,14 @@ describe('getUpdatedPackageJsonContent', () => { import: './src/bar.js', default: './src/bar.cjs', }, + './feature': { + import: './feature/index.js', + default: './feature/index.cjs', + }, + './feature/index': { + import: './feature/index.js', + default: './feature/index.cjs', + }, './package.json': './package.json', }, }); 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 14366be160fb5..558d254963df4 100644 --- a/packages/js/src/utils/package-json/update-package-json.ts +++ b/packages/js/src/utils/package-json/update-package-json.ts @@ -261,8 +261,13 @@ export function getExports( options.projectRoot ); const sourceFilePath = relativeDir + fileName; - const entryFilepath = sourceFilePath.replace(/^\.\/src\//, './'); + const entryRelativeDir = relativeDir.replace(/^\.\/src\//, './'); + const entryFilepath = entryRelativeDir + fileName; const isJsFile = jsRegex.test(fileExt); + if (isJsFile && fileName === 'index') { + const barrelEntry = entryRelativeDir.replace(/\/$/, ''); + exports[barrelEntry] = sourceFilePath + options.fileExt; + } exports[isJsFile ? entryFilepath : entryFilepath + fileExt] = sourceFilePath + (isJsFile ? options.fileExt : fileExt); }