diff --git a/packages/angular/build/src/builders/library/pipeline/package-manifests.ts b/packages/angular/build/src/builders/library/pipeline/package-manifests.ts index 5238ccca76c2..e109c9723270 100644 --- a/packages/angular/build/src/builders/library/pipeline/package-manifests.ts +++ b/packages/angular/build/src/builders/library/pipeline/package-manifests.ts @@ -8,6 +8,7 @@ import { createRequire } from 'node:module'; import path from 'node:path'; +import { toPosixPath } from '../../../utils/path'; import type { NormalizedLibraryOptions, PackageJsonData } from '../types'; import { FESM_OUTPUT_DIR, @@ -17,6 +18,25 @@ import { loadPackageJson, } from './utils'; +const TS_SOURCE_FILE_REGEX = /(? = { - ...(typeof userExports === 'object' && userExports !== null && !Array.isArray(userExports) - ? userExports - : {}), - './package.json': { default: './package.json' }, - }; - const primaryEntryPoint = options.entryPoints.get('.'); if (!primaryEntryPoint) { throw new Error(`Primary entry point '.' was not found in entryPoints.`); @@ -60,7 +73,29 @@ export async function generatePackageManifests( const primaryFesm = `./${FESM_OUTPUT_DIR}/${primaryName}.mjs`; const primaryDts = `./${TYPES_OUTPUT_DIR}/${primaryName}.d.ts`; - exportsMap['.'] = createExportConditions(exportsMap['.'], primaryDts, primaryFesm); + const normalizedUserExports: Record = {}; + if (typeof userExports === 'object' && userExports !== null && !Array.isArray(userExports)) { + const keys = Object.keys(userExports); + if (keys.length > 0 && !keys.some((k) => k.startsWith('.'))) { + normalizedUserExports['.'] = userExports; + } else { + for (const [key, value] of Object.entries(userExports)) { + normalizedUserExports[normalizeExportSubpathKey(key)] = value; + } + } + } + + const { + '.': primaryUserExport, + './package.json': _pkgJsonExport, + ...otherUserExports + } = normalizedUserExports; + + const exportsMap: Record = { + '.': createExportConditions(primaryUserExport, primaryDts, primaryFesm), + './package.json': { default: './package.json' }, + ...otherUserExports, + }; const distPackageJson: PackageJsonData = { ...restPackageJson, @@ -174,11 +209,22 @@ function createExportConditions( ? (existingConditions as Record) : {}; - const { types: _types, default: _default, ...otherConditions } = existing; + const customConditions: Record = {}; + for (const [key, value] of Object.entries(existing)) { + if (key === 'types' || key === 'default') { + continue; + } + + if (typeof value === 'string' && TS_SOURCE_FILE_REGEX.test(value)) { + continue; + } + + customConditions[key] = value; + } return { types: dtsPath, - ...otherConditions, + ...customConditions, default: fesmPath, }; } diff --git a/packages/angular/build/src/builders/library/pipeline/package-manifests_spec.ts b/packages/angular/build/src/builders/library/pipeline/package-manifests_spec.ts index be58cbb3cb20..362b6f5a65e3 100644 --- a/packages/angular/build/src/builders/library/pipeline/package-manifests_spec.ts +++ b/packages/angular/build/src/builders/library/pipeline/package-manifests_spec.ts @@ -262,7 +262,7 @@ describe('generatePackageManifests', () => { './styles.css': './styles.css', './scss/*': './scss/*', '.': { - development: './src/index.ts', + development: './fesm2022/my-lib-dev.mjs', }, }, }, @@ -277,12 +277,152 @@ describe('generatePackageManifests', () => { './package.json': { default: './package.json' }, '.': { types: './types/my-lib.d.ts', - development: './src/index.ts', + development: './fesm2022/my-lib-dev.mjs', default: './fesm2022/my-lib.mjs', }, }); }); + it('should handle root conditional exports without spreading condition keys at top level', async () => { + const options = createOptions({ + packageJson: { + name: 'my-lib', + version: '1.0.0', + exports: { + default: './src/public-api.ts', + custom: './custom.mjs', + }, + }, + }); + + const files = await generatePackageManifests(options, false); + const result = getRootPackageJson(files); + + expect(result.exports).toEqual({ + '.': { + types: './types/my-lib.d.ts', + custom: './custom.mjs', + default: './fesm2022/my-lib.mjs', + }, + './package.json': { default: './package.json' }, + }); + }); + + it('should normalize subpath keys in user exports and merge conditions for secondary entry points', async () => { + const options1 = createOptions( + { + packageJson: { + name: 'my-lib', + version: '1.0.0', + exports: { + '.': './src/public-api.ts', + testing: './testing/src/public-api.ts', + }, + }, + }, + true, + ); + + const result1 = getRootPackageJson(await generatePackageManifests(options1, false)); + expect(result1.exports).toEqual({ + '.': { + types: './types/my-lib.d.ts', + default: './fesm2022/my-lib.mjs', + }, + './package.json': { default: './package.json' }, + './testing': { + types: './types/my-lib-testing.d.ts', + default: './fesm2022/my-lib-testing.mjs', + }, + }); + + const options2 = createOptions( + { + packageJson: { + name: 'my-lib', + version: '1.0.0', + exports: { + './': './src/public-api.ts', + './testing/': { + custom: './testing/custom.mjs', + default: './testing/src/public-api.ts', + }, + }, + }, + }, + true, + ); + + const result2 = getRootPackageJson(await generatePackageManifests(options2, false)); + expect(result2.exports).toEqual({ + '.': { + types: './types/my-lib.d.ts', + default: './fesm2022/my-lib.mjs', + }, + './package.json': { default: './package.json' }, + './testing': { + types: './types/my-lib-testing.d.ts', + custom: './testing/custom.mjs', + default: './fesm2022/my-lib-testing.mjs', + }, + }); + }); + + it('should omit user export conditions pointing to .ts or .mts source files', async () => { + const options = createOptions({ + packageJson: { + name: 'my-lib', + version: '1.0.0', + exports: { + '.': { + import: './src/public-api.ts', + module: './src/public-api.mts', + development: './src/index.ts', + customTypes: './custom.d.ts', + custom: './custom.mjs', + default: './src/public-api.ts', + }, + }, + }, + }); + + const files = await generatePackageManifests(options, false); + const result = getRootPackageJson(files); + + expect(result.exports).toEqual({ + '.': { + types: './types/my-lib.d.ts', + customTypes: './custom.d.ts', + custom: './custom.mjs', + default: './fesm2022/my-lib.mjs', + }, + './package.json': { default: './package.json' }, + }); + }); + + it('should ensure "." and "./package.json" appear first in exports even if "./package.json" is listed before "."', async () => { + const options = createOptions({ + packageJson: { + name: 'my-lib', + version: '1.0.0', + exports: { + './styles.css': './styles.css', + './package.json': './package.json', + '.': './src/public-api.ts', + }, + }, + }); + + const files = await generatePackageManifests(options, false); + const result = getRootPackageJson(files); + + expect(Object.keys(result.exports as Record)).toEqual([ + '.', + './package.json', + './styles.css', + ]); + }); + it('should default sideEffects to false if not specified, and preserve when set', async () => { const files1 = await generatePackageManifests( createOptions({ @@ -306,6 +446,18 @@ describe('generatePackageManifests', () => { false, ); expect(getRootPackageJson(files2).sideEffects).toEqual(['*.css']); + + const files3 = await generatePackageManifests( + createOptions({ + packageJson: { + name: 'my-lib', + version: '1.0.0', + sideEffects: true, + }, + }), + false, + ); + expect(getRootPackageJson(files3).sideEffects).toBeTrue(); }); it('should conditionally inject tslib into dependencies only when hasTslibImport is true', async () => {