Skip to content

Commit a480bbd

Browse files
committed
fix(@angular/build): normalize package.json exports conditions and preserve sideEffects: true
Normalize user-defined `exports` in `generatePackageManifests` so that: - Root conditional export objects (whose keys do not start with `.`) are treated as conditions for the primary entry point (`'.'`) rather than spread as top-level keys alongside `'.'`. - Subpath export keys are normalized to canonical relative subpaths (e.g., `'testing'` and `'./testing/'` -> `'./testing'`) so unnormalized keys do not remain pointing to source files. - Conditional export entries pointing to unbuilt TypeScript source files (`.ts` or `.mts`) are omitted so they do not shadow `default` in the emitted `package.json`. - `'.'` and `'./package.json'` are consistently placed first in the emitted `exports` map.
1 parent 0fd4404 commit a480bbd

2 files changed

Lines changed: 210 additions & 12 deletions

File tree

‎packages/angular/build/src/builders/library/pipeline/package-manifests.ts‎

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import { createRequire } from 'node:module';
1010
import path from 'node:path';
11+
import { toPosixPath } from '../../../utils/path';
1112
import type { NormalizedLibraryOptions, PackageJsonData } from '../types';
1213
import {
1314
FESM_OUTPUT_DIR,
@@ -17,6 +18,25 @@ import {
1718
loadPackageJson,
1819
} from './utils';
1920

21+
const TS_SOURCE_FILE_REGEX = /(?<!\.d)\.m?ts$/;
22+
23+
/**
24+
* Normalizes an export subpath key to a canonical relative subpath (e.g., `'.'` or `'./testing'`).
25+
*
26+
* @param key Raw export subpath key from `package.json`.
27+
* @returns The normalized POSIX subpath key.
28+
*/
29+
function normalizeExportSubpathKey(key: string): string {
30+
const posixKey = toPosixPath(key);
31+
if (posixKey === '.') {
32+
return '.';
33+
}
34+
35+
const normalized = (posixKey.startsWith('./') ? posixKey : `./${posixKey}`).replace(/\/+$/, '');
36+
37+
return normalized || '.';
38+
}
39+
2040
/**
2141
* Generates the APF package.json and secondary entry point package.json manifests.
2242
*
@@ -42,13 +62,6 @@ export async function generatePackageManifests(
4262
...restPackageJson
4363
} = rawPackageJson;
4464

45-
const exportsMap: Record<string, unknown> = {
46-
...(typeof userExports === 'object' && userExports !== null && !Array.isArray(userExports)
47-
? userExports
48-
: {}),
49-
'./package.json': { default: './package.json' },
50-
};
51-
5265
const primaryEntryPoint = options.entryPoints.get('.');
5366
if (!primaryEntryPoint) {
5467
throw new Error(`Primary entry point '.' was not found in entryPoints.`);
@@ -60,7 +73,29 @@ export async function generatePackageManifests(
6073
const primaryFesm = `./${FESM_OUTPUT_DIR}/${primaryName}.mjs`;
6174
const primaryDts = `./${TYPES_OUTPUT_DIR}/${primaryName}.d.ts`;
6275

63-
exportsMap['.'] = createExportConditions(exportsMap['.'], primaryDts, primaryFesm);
76+
const normalizedUserExports: Record<string, unknown> = {};
77+
if (typeof userExports === 'object' && userExports !== null && !Array.isArray(userExports)) {
78+
const keys = Object.keys(userExports);
79+
if (keys.length > 0 && !keys.some((k) => k.startsWith('.'))) {
80+
normalizedUserExports['.'] = userExports;
81+
} else {
82+
for (const [key, value] of Object.entries(userExports)) {
83+
normalizedUserExports[normalizeExportSubpathKey(key)] = value;
84+
}
85+
}
86+
}
87+
88+
const {
89+
'.': primaryUserExport,
90+
'./package.json': _pkgJsonExport,
91+
...otherUserExports
92+
} = normalizedUserExports;
93+
94+
const exportsMap: Record<string, unknown> = {
95+
'.': createExportConditions(primaryUserExport, primaryDts, primaryFesm),
96+
'./package.json': { default: './package.json' },
97+
...otherUserExports,
98+
};
6499

65100
const distPackageJson: PackageJsonData = {
66101
...restPackageJson,
@@ -174,11 +209,22 @@ function createExportConditions(
174209
? (existingConditions as Record<string, unknown>)
175210
: {};
176211

177-
const { types: _types, default: _default, ...otherConditions } = existing;
212+
const customConditions: Record<string, unknown> = {};
213+
for (const [key, value] of Object.entries(existing)) {
214+
if (key === 'types' || key === 'default') {
215+
continue;
216+
}
217+
218+
if (typeof value === 'string' && TS_SOURCE_FILE_REGEX.test(value)) {
219+
continue;
220+
}
221+
222+
customConditions[key] = value;
223+
}
178224

179225
return {
180226
types: dtsPath,
181-
...otherConditions,
227+
...customConditions,
182228
default: fesmPath,
183229
};
184230
}

‎packages/angular/build/src/builders/library/pipeline/package-manifests_spec.ts‎

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ describe('generatePackageManifests', () => {
262262
'./styles.css': './styles.css',
263263
'./scss/*': './scss/*',
264264
'.': {
265-
development: './src/index.ts',
265+
development: './fesm2022/my-lib-dev.mjs',
266266
},
267267
},
268268
},
@@ -277,12 +277,152 @@ describe('generatePackageManifests', () => {
277277
'./package.json': { default: './package.json' },
278278
'.': {
279279
types: './types/my-lib.d.ts',
280-
development: './src/index.ts',
280+
development: './fesm2022/my-lib-dev.mjs',
281281
default: './fesm2022/my-lib.mjs',
282282
},
283283
});
284284
});
285285

286+
it('should handle root conditional exports without spreading condition keys at top level', async () => {
287+
const options = createOptions({
288+
packageJson: {
289+
name: 'my-lib',
290+
version: '1.0.0',
291+
exports: {
292+
default: './src/public-api.ts',
293+
custom: './custom.mjs',
294+
},
295+
},
296+
});
297+
298+
const files = await generatePackageManifests(options, false);
299+
const result = getRootPackageJson(files);
300+
301+
expect(result.exports).toEqual({
302+
'.': {
303+
types: './types/my-lib.d.ts',
304+
custom: './custom.mjs',
305+
default: './fesm2022/my-lib.mjs',
306+
},
307+
'./package.json': { default: './package.json' },
308+
});
309+
});
310+
311+
it('should normalize subpath keys in user exports and merge conditions for secondary entry points', async () => {
312+
const options1 = createOptions(
313+
{
314+
packageJson: {
315+
name: 'my-lib',
316+
version: '1.0.0',
317+
exports: {
318+
'.': './src/public-api.ts',
319+
testing: './testing/src/public-api.ts',
320+
},
321+
},
322+
},
323+
true,
324+
);
325+
326+
const result1 = getRootPackageJson(await generatePackageManifests(options1, false));
327+
expect(result1.exports).toEqual({
328+
'.': {
329+
types: './types/my-lib.d.ts',
330+
default: './fesm2022/my-lib.mjs',
331+
},
332+
'./package.json': { default: './package.json' },
333+
'./testing': {
334+
types: './types/my-lib-testing.d.ts',
335+
default: './fesm2022/my-lib-testing.mjs',
336+
},
337+
});
338+
339+
const options2 = createOptions(
340+
{
341+
packageJson: {
342+
name: 'my-lib',
343+
version: '1.0.0',
344+
exports: {
345+
'./': './src/public-api.ts',
346+
'./testing/': {
347+
custom: './testing/custom.mjs',
348+
default: './testing/src/public-api.ts',
349+
},
350+
},
351+
},
352+
},
353+
true,
354+
);
355+
356+
const result2 = getRootPackageJson(await generatePackageManifests(options2, false));
357+
expect(result2.exports).toEqual({
358+
'.': {
359+
types: './types/my-lib.d.ts',
360+
default: './fesm2022/my-lib.mjs',
361+
},
362+
'./package.json': { default: './package.json' },
363+
'./testing': {
364+
types: './types/my-lib-testing.d.ts',
365+
custom: './testing/custom.mjs',
366+
default: './fesm2022/my-lib-testing.mjs',
367+
},
368+
});
369+
});
370+
371+
it('should omit user export conditions pointing to .ts or .mts source files', async () => {
372+
const options = createOptions({
373+
packageJson: {
374+
name: 'my-lib',
375+
version: '1.0.0',
376+
exports: {
377+
'.': {
378+
import: './src/public-api.ts',
379+
module: './src/public-api.mts',
380+
development: './src/index.ts',
381+
customTypes: './custom.d.ts',
382+
custom: './custom.mjs',
383+
default: './src/public-api.ts',
384+
},
385+
},
386+
},
387+
});
388+
389+
const files = await generatePackageManifests(options, false);
390+
const result = getRootPackageJson(files);
391+
392+
expect(result.exports).toEqual({
393+
'.': {
394+
types: './types/my-lib.d.ts',
395+
customTypes: './custom.d.ts',
396+
custom: './custom.mjs',
397+
default: './fesm2022/my-lib.mjs',
398+
},
399+
'./package.json': { default: './package.json' },
400+
});
401+
});
402+
403+
it('should ensure "." and "./package.json" appear first in exports even if "./package.json" is listed before "."', async () => {
404+
const options = createOptions({
405+
packageJson: {
406+
name: 'my-lib',
407+
version: '1.0.0',
408+
exports: {
409+
'./styles.css': './styles.css',
410+
'./package.json': './package.json',
411+
'.': './src/public-api.ts',
412+
},
413+
},
414+
});
415+
416+
const files = await generatePackageManifests(options, false);
417+
const result = getRootPackageJson(files);
418+
419+
expect(Object.keys(result.exports as Record<string, unknown>)).toEqual([
420+
'.',
421+
'./package.json',
422+
'./styles.css',
423+
]);
424+
});
425+
286426
it('should default sideEffects to false if not specified, and preserve when set', async () => {
287427
const files1 = await generatePackageManifests(
288428
createOptions({
@@ -306,6 +446,18 @@ describe('generatePackageManifests', () => {
306446
false,
307447
);
308448
expect(getRootPackageJson(files2).sideEffects).toEqual(['*.css']);
449+
450+
const files3 = await generatePackageManifests(
451+
createOptions({
452+
packageJson: {
453+
name: 'my-lib',
454+
version: '1.0.0',
455+
sideEffects: true,
456+
},
457+
}),
458+
false,
459+
);
460+
expect(getRootPackageJson(files3).sideEffects).toBeTrue();
309461
});
310462

311463
it('should conditionally inject tslib into dependencies only when hasTslibImport is true', async () => {

0 commit comments

Comments
 (0)