11/** @import { Platform } from '../types/index' */
22
3+ import fs from 'node:fs' ;
34import path from 'node:path' ;
45import { findTsconfig , getExtendsChain } from 'get-tsconfig' ;
56import { validate_platform } from './transform/platform.js' ;
@@ -12,6 +13,18 @@ import { validate_platform } from './transform/platform.js';
1213 * @property {string } [integration]
1314 */
1415
16+ /**
17+ * @typedef {object } TsconfigLayer
18+ * @property {string } path
19+ * @property {unknown } config
20+ */
21+
22+ /**
23+ * @typedef {object } DeclaredPlatform
24+ * @property {Platform } platform
25+ * @property {string } path
26+ */
27+
1528/** @param {unknown } value @returns {string } */
1629function render_value ( value ) {
1730 try {
@@ -52,46 +65,126 @@ function read_layer_platform(layer) {
5265}
5366
5467/**
55- * Resolve `tsrx.platform` for a builder from the same nearest/configured
56- * tsconfig and explicit `extends` graph used by the project. An explicit
57- * builder option is retained as an escape hatch, but it must agree with a
58- * tsconfig declaration when both exist.
68+ * TypeScript project references use `.json` files as-is and otherwise look for
69+ * `tsconfig.json` inside the referenced directory.
5970 *
60- * @param {BuildPlatformResolutionOptions } [options]
61- * @returns {Platform | undefined }
71+ * @param {string } from_config_path
72+ * @param {string } reference_path
73+ * @returns {string }
6274 */
63- export function resolveBuildPlatform ( options = { } ) {
64- const integration = options . integration ?? 'build integration' ;
65- const explicit_platform = validate_platform ( options . platform , `${ integration } platform option` ) ;
66- const root = path . resolve ( options . root ?? process . cwd ( ) ) ;
67- const config_path = options . tsconfig
68- ? path . resolve ( root , options . tsconfig )
69- : findTsconfig ( root , { cache : new Map ( ) } ) ;
75+ function resolve_reference_config_path ( from_config_path , reference_path ) {
76+ const resolved = path . resolve ( path . dirname ( from_config_path ) , reference_path ) ;
77+ return path . extname ( resolved ) === '.json' ? resolved : path . join ( resolved , 'tsconfig.json' ) ;
78+ }
7079
71- if ( ! config_path ) return explicit_platform ;
80+ /**
81+ * @param {unknown } config
82+ * @param {string } from_config_path
83+ * @returns {string[] }
84+ */
85+ function get_reference_config_paths ( config , from_config_path ) {
86+ if ( ! config || typeof config !== 'object' || Array . isArray ( config ) ) return [ ] ;
87+ const references = /** @type {Record<string, unknown> } */ ( config ) . references ;
88+ if ( ! Array . isArray ( references ) ) return [ ] ;
89+
90+ /** @type {string[] } */
91+ const paths = [ ] ;
92+ for ( const reference of references ) {
93+ if ( ! reference || typeof reference !== 'object' || Array . isArray ( reference ) ) continue ;
94+ const reference_path = /** @type {Record<string, unknown> } */ ( reference ) . path ;
95+ if ( typeof reference_path !== 'string' || reference_path === '' ) continue ;
96+ paths . push ( resolve_reference_config_path ( from_config_path , reference_path ) ) ;
97+ }
98+ return paths ;
99+ }
100+
101+ /**
102+ * @param {TsconfigLayer[] } layers
103+ * @returns {DeclaredPlatform | undefined }
104+ */
105+ function read_chain_platform ( layers ) {
106+ for ( const layer of layers ) {
107+ const declared = read_layer_platform ( layer ) ;
108+ if ( declared !== undefined ) return { platform : declared , path : layer . path } ;
109+ }
110+ return undefined ;
111+ }
112+
113+ /**
114+ * Resolve a project's own `extends` graph first, then fall through to nested
115+ * `references`. A solution tsconfig that only lists referenced projects, the
116+ * default Vite layout, therefore still finds `tsrx.platform` on
117+ * `tsconfig.app.json`.
118+ *
119+ * @param {string } config_path
120+ * @param {string } integration
121+ * @param {Map<string, unknown> } cache
122+ * @param {Set<string> } seen
123+ * @param {boolean } [allow_missing]
124+ * @returns {DeclaredPlatform | undefined }
125+ */
126+ function read_project_platform ( config_path , integration , cache , seen , allow_missing = false ) {
127+ const normalized = path . resolve ( config_path ) ;
128+ if ( seen . has ( normalized ) ) return undefined ;
129+ seen . add ( normalized ) ;
130+ if ( allow_missing && ! fs . existsSync ( normalized ) ) return undefined ;
72131
73132 let layers ;
74133 try {
75134 // get-tsconfig returns the root first, followed by extended configs in
76135 // precedence order. The first own platform declaration is therefore the
77136 // effective value, while a sibling `tsrx.compiler` key can still inherit.
78- layers = getExtendsChain ( config_path , { cache : new Map ( ) } ) ;
137+ layers = getExtendsChain ( normalized , { cache } ) ;
79138 } catch ( error ) {
80139 throw new Error (
81- `Unable to resolve TSRX platform from ${ config_path } for ${ integration } : ${ error instanceof Error ? error . message : String ( error ) } ` ,
140+ `Unable to resolve TSRX platform from ${ normalized } for ${ integration } : ${ error instanceof Error ? error . message : String ( error ) } ` ,
82141 { cause : error } ,
83142 ) ;
84143 }
85144
86- /** @type {Platform | undefined } */
87- let configured_platform ;
88- for ( const layer of layers ) {
89- const declared = read_layer_platform ( layer ) ;
90- if ( declared !== undefined ) {
91- configured_platform = declared ;
92- break ;
145+ const own = read_chain_platform ( layers ) ;
146+ if ( own ) return own ;
147+
148+ const start = layers [ 0 ] ;
149+ if ( ! start ) return undefined ;
150+
151+ /** @type {DeclaredPlatform | undefined } */
152+ let referenced ;
153+ for ( const reference_path of get_reference_config_paths ( start . config , start . path ) ) {
154+ const nested = read_project_platform ( reference_path , integration , cache , seen , true ) ;
155+ if ( ! nested ) continue ;
156+ if ( referenced && referenced . platform !== nested . platform ) {
157+ throw new Error (
158+ `TSRX platform mismatch for ${ integration } : ${ referenced . path } selects ${ JSON . stringify ( referenced . platform ) } , but ${ nested . path } selects ${ JSON . stringify ( nested . platform ) } .` ,
159+ ) ;
93160 }
161+ referenced = nested ;
94162 }
163+ return referenced ;
164+ }
165+
166+ /**
167+ * Resolve `tsrx.platform` for a builder from the same nearest/configured
168+ * tsconfig, explicit `extends` graph, and TypeScript project `references` used
169+ * by the project. An explicit builder option is retained as an escape hatch,
170+ * but it must agree with a tsconfig declaration when both exist.
171+ *
172+ * @param {BuildPlatformResolutionOptions } [options]
173+ * @returns {Platform | undefined }
174+ */
175+ export function resolveBuildPlatform ( options = { } ) {
176+ const integration = options . integration ?? 'build integration' ;
177+ const explicit_platform = validate_platform ( options . platform , `${ integration } platform option` ) ;
178+ const root = path . resolve ( options . root ?? process . cwd ( ) ) ;
179+ const cache = new Map ( ) ;
180+ const config_path = options . tsconfig
181+ ? path . resolve ( root , options . tsconfig )
182+ : findTsconfig ( root , { cache } ) ;
183+
184+ if ( ! config_path ) return explicit_platform ;
185+
186+ const configured = read_project_platform ( config_path , integration , cache , new Set ( ) ) ;
187+ const configured_platform = configured ?. platform ;
95188
96189 if (
97190 explicit_platform !== undefined &&
0 commit comments