Skip to content

Commit 526bc0a

Browse files
committed
fix(@angular/build): preserve entry-point displayName casing and flatten bundle names
- Preserve original casing in entry point `displayName` so case-sensitive TypeScript `paths` mappings resolve camelCase secondary entry points while keeping `bundleName` lowercased and flattened. - Skip static `.d.ts` and `.d.mts` subpath exports in `normalizeEntryPoints` so they are preserved as non-compiled static exports in `package.json#exports`. - Validate that resolved entry point file paths stay within `projectRoot` using `isSubDirectory`.
1 parent 0fd4404 commit 526bc0a

2 files changed

Lines changed: 92 additions & 5 deletions

File tree

‎packages/angular/build/src/builders/library/pipeline/entry-points.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import path from 'node:path';
10-
import { toPosixPath } from '../../../utils/path';
10+
import { isSubDirectory, toPosixPath } from '../../../utils/path';
1111
import type { BuilderState } from '../builder-state';
1212
import type { NormalizedEntryPoint, NormalizedLibraryOptions, PackageJsonData } from '../types';
1313
import type { BundleEntryPointInput } from './bundler';
@@ -55,10 +55,16 @@ function normalizeEntryPoint(
5555
}
5656

5757
const subpath = isPrimary ? '.' : `./${name}`;
58-
const displayName = (isPrimary ? packageName : `${packageName}/${name}`).toLowerCase();
58+
const displayName = isPrimary ? packageName : `${packageName}/${name}`;
5959
const bundleName = getEntryPointBundleName(packageName, name);
6060
const entryFilePath = toPosixPath(path.resolve(projectRoot, targetPath));
6161

62+
if (!isSubDirectory(projectRoot, entryFilePath)) {
63+
throw new Error(
64+
`Entry point '${key}' file path '${entryFilePath}' is outside the project root '${projectRoot}'.`,
65+
);
66+
}
67+
6268
if (!/(?<!\.d)\.(?:ts|mts)$/.test(entryFilePath)) {
6369
throw new Error(
6470
`Entry point '${key}' file path must be a TypeScript file ('.ts' or '.mts'): '${entryFilePath}'.`,
@@ -157,8 +163,8 @@ export function normalizeEntryPoints(
157163
continue;
158164
}
159165

160-
if (!isPrimary && !/\.m?ts$/.test(target)) {
161-
// Static asset, stylesheet, or package.json export; preserve in package.json without compiling.
166+
if (!isPrimary && !/(?<!\.d)\.m?ts$/.test(target)) {
167+
// Static asset, stylesheet, declaration file, or package.json export; preserve in package.json without compiling.
162168
continue;
163169
}
164170

‎packages/angular/build/src/builders/library/pipeline/entry-points_spec.ts‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('entry-points pipeline', () => {
6060
});
6161

6262
it('should replace nested slashes with dashes in secondary entry point name', () => {
63+
expect(getEntryPointBundleName('my-lib', 'testing/http')).toBe('my-lib-testing-http');
6364
expect(getEntryPointBundleName('@my-scope/my-lib', 'testing/sub/deep')).toBe(
6465
'my-scope-my-lib-testing-sub-deep',
6566
);
@@ -240,12 +241,67 @@ describe('entry-points pipeline', () => {
240241
);
241242
});
242243

243-
it('should ignore non-TypeScript exports for secondary subpaths', () => {
244+
it('should preserve casing in displayName while lowercasing bundleName', () => {
245+
const rawExports = {
246+
'.': './src/public-api.ts',
247+
'./myFeature': './my-feature/src/public-api.ts',
248+
};
249+
250+
const entryPoints = normalizeEntryPoints(
251+
rawExports,
252+
projectRoot,
253+
packageJsonPath,
254+
'@My-Scope/My-Lib',
255+
);
256+
257+
expect(entryPoints.get('.')).toEqual({
258+
subpath: '.',
259+
name: '.',
260+
displayName: '@My-Scope/My-Lib',
261+
bundleName: 'my-scope-my-lib',
262+
entryFilePath: join(projectRoot, 'src/public-api.ts'),
263+
isPrimary: true,
264+
});
265+
266+
expect(entryPoints.get('myFeature')).toEqual({
267+
subpath: './myFeature',
268+
name: 'myFeature',
269+
displayName: '@My-Scope/My-Lib/myFeature',
270+
bundleName: 'my-scope-my-lib-myfeature',
271+
entryFilePath: join(projectRoot, 'my-feature/src/public-api.ts'),
272+
isPrimary: false,
273+
});
274+
});
275+
276+
it('should flatten nested secondary entry-point bundleName', () => {
277+
const entryPoints = normalizeEntryPoints(
278+
{
279+
'.': './src/public-api.ts',
280+
'./testing/http': './testing/http/src/public-api.ts',
281+
},
282+
projectRoot,
283+
packageJsonPath,
284+
'my-lib',
285+
);
286+
287+
expect(entryPoints.get('testing/http')).toEqual({
288+
subpath: './testing/http',
289+
name: 'testing/http',
290+
displayName: 'my-lib/testing/http',
291+
bundleName: 'my-lib-testing-http',
292+
entryFilePath: join(projectRoot, 'testing/http/src/public-api.ts'),
293+
isPrimary: false,
294+
});
295+
});
296+
297+
it('should ignore non-TypeScript exports and static declaration files for secondary subpaths', () => {
244298
const rawExports = {
245299
'.': './src/public-api.ts',
246300
'./package.json': './package.json',
247301
'./styles.css': './styles.css',
248302
'./theme': { sass: './theme.scss' },
303+
'./custom-types': './src/custom-types.d.ts',
304+
'./custom-esm-types': { default: './src/custom-types.d.mts' },
249305
};
250306

251307
const entryPoints = normalizeEntryPoints(
@@ -260,6 +316,31 @@ describe('entry-points pipeline', () => {
260316
expect(entryPoints.has('package.json')).toBeFalse();
261317
expect(entryPoints.has('styles.css')).toBeFalse();
262318
expect(entryPoints.has('theme')).toBeFalse();
319+
expect(entryPoints.has('custom-types')).toBeFalse();
320+
expect(entryPoints.has('custom-esm-types')).toBeFalse();
321+
});
322+
323+
it('should throw when entry point file path escapes projectRoot', () => {
324+
expect(() =>
325+
normalizeEntryPoints(
326+
{ '.': '../other-lib/src/public-api.ts' },
327+
projectRoot,
328+
packageJsonPath,
329+
packageName,
330+
),
331+
).toThrowError(/is outside the project root/);
332+
333+
expect(() =>
334+
normalizeEntryPoints(
335+
{
336+
'.': './src/public-api.ts',
337+
'./testing': '../../outside/public-api.ts',
338+
},
339+
projectRoot,
340+
packageJsonPath,
341+
packageName,
342+
),
343+
).toThrowError(/is outside the project root/);
263344
});
264345

265346
it('should throw when exports is missing or not an object/string', () => {

0 commit comments

Comments
 (0)