@@ -41,9 +41,6 @@ export interface NormalizedEntryPoint {
4141 /** Absolute path to entry file. */
4242 entryFilePath : string ;
4343
44- /** Absolute path to tsConfig file for this entry point. */
45- tsConfigPath : string ;
46-
4744 /** Is this the primary entry point ('.')? */
4845 isPrimary : boolean ;
4946}
@@ -57,7 +54,7 @@ export interface PackageJsonData {
5754 typings ?: string ;
5855 types ?: string ;
5956 sideEffects ?: boolean | string [ ] ;
60- exports ?: Record < string , unknown > ;
57+ exports ?: string | Record < string , unknown > ;
6158 scripts ?: Record < string , string > ;
6259 workspaces ?: unknown ;
6360 dependencies ?: Record < string , string > ;
@@ -119,7 +116,6 @@ export async function normalizeLibraryOptions(
119116
120117 const {
121118 tsConfig,
122- entryPoints : rawEntryPoints ,
123119 assets : rawAssets ,
124120 stylePreprocessorOptions,
125121 inlineStyleLanguage = 'css' ,
@@ -155,10 +151,9 @@ export async function normalizeLibraryOptions(
155151 }
156152
157153 const entryPoints = normalizeEntryPoints (
158- rawEntryPoints ,
159- workspaceRoot ,
160- resolvedTsConfigPath ,
161- projectName ,
154+ packageJson . exports ,
155+ projectRoot ,
156+ packageJsonPath ,
162157 packageName ,
163158 ) ;
164159
@@ -241,18 +236,16 @@ export async function normalizeLibraryOptions(
241236/**
242237 * Normalizes a single entry point specification.
243238 *
244- * @param key The entry point key from configuration (e.g. '.' or './testing').
245- * @param value The entry point file path string or object with entryPoint and tsConfig.
246- * @param workspaceRoot The workspace root directory.
247- * @param defaultTsConfigPath The default tsConfig path for the project.
239+ * @param key The entry point key from package.json exports (e.g. '.' or './testing').
240+ * @param targetPath The relative file path string from exports.
241+ * @param projectRoot The library project root directory.
248242 * @param packageName The root package name (e.g. `@my/lib`).
249243 * @returns The normalized entry point descriptor.
250244 */
251245function normalizeEntryPoint (
252246 key : string ,
253- value : LibraryBuilderOptions [ 'entryPoints' ] [ string ] ,
254- workspaceRoot : string ,
255- defaultTsConfigPath : string ,
247+ targetPath : string ,
248+ projectRoot : string ,
256249 packageName : string ,
257250) : NormalizedEntryPoint {
258251 const posixKey = toPosixPath ( key ) . replace ( / \/ + $ / , '' ) ;
@@ -265,69 +258,95 @@ function normalizeEntryPoint(
265258
266259 if ( name !== '.' && ( path . posix . isAbsolute ( name ) || name . includes ( '..' ) ) ) {
267260 throw new Error (
268- `Invalid entry point key '${ key } '. Entry point keys must be relative subpaths without '..' (e.g. './testing' or 'testing' ).` ,
261+ `Invalid entry point key '${ key } '. Entry point keys must be relative subpaths without '..' (e.g. './testing').` ,
269262 ) ;
270263 }
271264
272265 const subpath = isPrimary ? '.' : `./${ name } ` ;
273266 const displayName = isPrimary ? packageName : `${ packageName } /${ name } ` ;
274267 const bundleName = getEntryPointBundleName ( packageName , name , isPrimary ) ;
275268
276- const entryFilePath = path . resolve (
277- workspaceRoot ,
278- typeof value === 'string' ? value : value . entryPoint ,
279- ) ;
269+ const entryFilePath = path . resolve ( projectRoot , targetPath ) ;
280270
281271 if ( ! / \. (?: t s | m t s ) $ / . test ( entryFilePath ) || / \. d \. (?: t s | m t s ) $ / . test ( entryFilePath ) ) {
282272 throw new Error (
283273 `Entry point '${ key } ' file path must be a TypeScript file ('.ts' or '.mts'): '${ entryFilePath } '.` ,
284274 ) ;
285275 }
286276
287- const tsConfigPath =
288- typeof value !== 'string' && value . tsConfig
289- ? path . resolve ( workspaceRoot , value . tsConfig )
290- : defaultTsConfigPath ;
291-
292277 return {
293278 subpath,
294279 name,
295280 displayName,
296281 bundleName,
297282 entryFilePath,
298- tsConfigPath,
299283 isPrimary,
300284 } ;
301285}
302286
303287/**
304- * Normalizes all entry points for the library project .
288+ * Normalizes all entry points from the library's `package.json` `exports` field .
305289 *
306- * @param rawEntryPoints The raw entryPoints dictionary from schema options.
307- * @param workspaceRoot The workspace root directory.
308- * @param defaultTsConfigPath The default tsConfig path for the project.
309- * @param projectName The project name used in error reporting.
290+ * @param rawExports The `exports` field from `package.json`.
291+ * @param projectRoot The library project root directory.
292+ * @param packageJsonPath Path to `package.json` for error reporting.
310293 * @param packageName The root package name (e.g. `@my/lib`).
311294 * @returns A Map of normalized entry points keyed by name.
312295 */
313296function normalizeEntryPoints (
314- rawEntryPoints : LibraryBuilderOptions [ 'entryPoints' ] ,
315- workspaceRoot : string ,
316- defaultTsConfigPath : string ,
317- projectName : string ,
297+ rawExports : PackageJsonData [ 'exports' ] ,
298+ projectRoot : string ,
299+ packageJsonPath : string ,
318300 packageName : string ,
319301) : Map < string , NormalizedEntryPoint > {
302+ if ( ! rawExports || ( typeof rawExports !== 'string' && typeof rawExports !== 'object' ) ) {
303+ throw new Error (
304+ `The 'package.json' at '${ packageJsonPath } ' must contain an 'exports' field defining the primary entry point ('.').` ,
305+ ) ;
306+ }
307+
308+ const exportsRecord = typeof rawExports === 'string' ? { '.' : rawExports } : rawExports ;
309+
320310 const entryPoints = new Map < string , NormalizedEntryPoint > ( ) ;
321311 let hasPrimary = false ;
322312
323- for ( const [ key , value ] of Object . entries ( rawEntryPoints ) ) {
324- const entryPoint = normalizeEntryPoint (
325- key ,
326- value ,
327- workspaceRoot ,
328- defaultTsConfigPath ,
329- packageName ,
330- ) ;
313+ for ( const [ key , value ] of Object . entries ( exportsRecord ) ) {
314+ let target : string | undefined ;
315+ if ( typeof value === 'string' ) {
316+ target = value ;
317+ } else if (
318+ typeof value === 'object' &&
319+ value !== null &&
320+ ! Array . isArray ( value ) &&
321+ typeof ( value as Record < string , unknown > ) [ 'default' ] === 'string'
322+ ) {
323+ target = ( value as Record < string , unknown > ) [ 'default' ] as string ;
324+ }
325+
326+ const posixKey = toPosixPath ( key ) . replace ( / \/ + $ / , '' ) ;
327+ const isPrimary = posixKey === '.' || posixKey === '' ;
328+
329+ if ( ! target ) {
330+ if ( isPrimary ) {
331+ throw new Error (
332+ `The primary entry point '.' in '${ packageJsonPath } ' must specify a string path ` +
333+ `or a 'default' condition pointing to a TypeScript file.` ,
334+ ) ;
335+ }
336+ // Non-JS/TS conditional export (e.g., sass/style-only subpath); preserve in package.json without compiling.
337+ continue ;
338+ }
339+
340+ if ( ! isPrimary ) {
341+ const isTsSource = / \. (?: t s | m t s ) $ / . test ( target ) && ! / \. d \. (?: t s | m t s ) $ / . test ( target ) ;
342+ const isInvalidCodeEntry = / \. (?: d \. [ c m ] ? t s | c t s | t s x | j s x ) $ / . test ( target ) ;
343+ if ( ! isTsSource && ! isInvalidCodeEntry ) {
344+ // Static asset, stylesheet, or package.json export; preserve in package.json without compiling.
345+ continue ;
346+ }
347+ }
348+
349+ const entryPoint = normalizeEntryPoint ( key , target , projectRoot , packageName ) ;
331350 if ( entryPoints . has ( entryPoint . name ) ) {
332351 throw new Error (
333352 `Duplicate entry point detected: '${ key } ' resolves to the same name ('${ entryPoint . name } ') as an existing entry point.` ,
@@ -341,7 +360,7 @@ function normalizeEntryPoints(
341360
342361 if ( ! hasPrimary ) {
343362 throw new Error (
344- `The 'entryPoints' option in project '${ projectName } ' must contain a primary entry point with key '.'.` ,
363+ `The 'exports' field in '${ packageJsonPath } ' must contain a primary entry point with key '.'.` ,
345364 ) ;
346365 }
347366
0 commit comments