66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import path from 'node:path' ;
10- import { toPosixPath } from '../../../utils/path' ;
11- import type { NormalizedEntryPoint } from '../options' ;
12-
139const IS_DTS_FILE_REGEXP = / \. d \. [ c m ] ? t s $ / i;
1410const IS_DTS_MAP_FILE_REGEXP = / \. d \. [ c m ] ? t s \. m a p $ / i;
1511
@@ -23,11 +19,6 @@ export const FESM_OUTPUT_DIR = 'fesm2022';
2319 */
2420export const TYPES_OUTPUT_DIR = 'types' ;
2521
26- /**
27- * Function that resolves a file path to its containing normalized library entry point, if any.
28- */
29- export type EntryPointLookup = ( filePath : string ) => NormalizedEntryPoint | undefined ;
30-
3122/**
3223 * Represents an in-memory file to be emitted to disk.
3324 */
@@ -114,76 +105,3 @@ export function isDeclarationFile(path: string): boolean {
114105export function isDeclarationSourceMapFile ( path : string ) : boolean {
115106 return IS_DTS_MAP_FILE_REGEXP . test ( path ) ;
116107}
117-
118- /**
119- * Creates an optimized, memoized lookup function that maps arbitrary file paths
120- * to their closest containing library entry point.
121- *
122- * Entry point root directories are sorted in descending order of path length so that
123- * more specific, nested sub-entry points take precedence over shallower or primary
124- * entry points. Lookups are cached to avoid repeated path scanning.
125- *
126- * @example
127- * Given two entry points:
128- * - Primary entry point `.` at `/project/src/public-api.ts` (dir: `/project/src`)
129- * - Secondary entry point `./testing` at `/project/src/testing/public-api.ts` (dir: `/project/src/testing`)
130- *
131- * ```ts
132- * const findEntryPoint = createEntryDirectoryLookup(entryPoints);
133- *
134- * // Resolves to the 'testing' sub-entry point because /project/src/testing is the longest matching prefix:
135- * findEntryPoint('/project/src/testing/test-bed.ts'); // -> NormalizedEntryPoint ('./testing')
136- *
137- * // Resolves to the primary entry point:
138- * findEntryPoint('/project/src/button.ts'); // -> NormalizedEntryPoint ('.')
139- *
140- * // Returns undefined for files located outside any entry point directory:
141- * findEntryPoint('/project/shared/utils.ts'); // -> undefined
142- * ```
143- *
144- * @param entryPoints Iterable of normalized entry points.
145- * @returns An {@link EntryPointLookup} function that returns the owning {@link NormalizedEntryPoint},
146- * or `undefined` if the file does not belong to any entry point directory.
147- */
148- export function createEntryDirectoryLookup (
149- entryPoints : Iterable < NormalizedEntryPoint > ,
150- ) : EntryPointLookup {
151- const dirs = Array . from ( entryPoints , ( ep ) => {
152- const dir = toPosixPath ( path . dirname ( ep . entryFilePath ) ) ;
153-
154- return {
155- ep,
156- dir,
157- dirSlash : dir . endsWith ( '/' ) ? dir : `${ dir } /` ,
158- } ;
159- } ) . sort ( ( a , b ) => b . dir . length - a . dir . length ) ;
160-
161- const cache = new Map < string , NormalizedEntryPoint | undefined > ( ) ;
162-
163- return ( filePath : string ) : NormalizedEntryPoint | undefined => {
164- const posix = toPosixPath ( filePath ) ;
165- const cached = cache . get ( posix ) ;
166- if ( cached !== undefined || cache . has ( posix ) ) {
167- return cached ;
168- }
169- const found = dirs . find ( ( { dir, dirSlash } ) => posix === dir || posix . startsWith ( dirSlash ) ) ?. ep ;
170- cache . set ( posix , found ) ;
171-
172- return found ;
173- } ;
174- }
175-
176- /**
177- * Computes the base bundle file name for an entry point.
178- *
179- * @param packageName The package name from package.json.
180- * @param entryPointName The entry point subpath name (defaults to '.').
181- * @returns The sanitized bundle base name.
182- */
183- export function getEntryPointBundleName ( packageName : string , entryPointName = '.' ) : string {
184- const isPrimary = ! entryPointName || entryPointName === '.' ;
185- const pkgName = packageName [ 0 ] === '@' ? packageName . slice ( 1 ) : packageName ;
186- const epName = isPrimary ? pkgName : `${ pkgName } -${ entryPointName } ` ;
187-
188- return epName . replaceAll ( '/' , '-' ) ;
189- }
0 commit comments