Skip to content

Commit 4a42fed

Browse files
committed
fix: read builder platform from referenced tsconfigs
Builders now follow TypeScript project references after the nearest tsconfig extends chain, so Vite-style tsconfig.app.json platform settings apply at compile time without a per-integration override.
1 parent 99d5890 commit 4a42fed

2 files changed

Lines changed: 175 additions & 23 deletions

File tree

‎packages/tsrx/src/config.js‎

Lines changed: 116 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** @import { Platform } from '../types/index' */
22

3+
import fs from 'node:fs';
34
import path from 'node:path';
45
import { findTsconfig, getExtendsChain } from 'get-tsconfig';
56
import { 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} */
1629
function 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 &&

‎packages/tsrx/tests/utils/config.test.js‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,63 @@ describe('build platform resolution', () => {
8585
write_config('tsconfig.json', { tsrx: { platform } });
8686
expect(() => resolveBuildPlatform({ root: directory })).toThrow(/Invalid TSRX platform/);
8787
});
88+
89+
it('reads platform from a referenced project such as tsconfig.app.json', () => {
90+
write_config('tsconfig.app.json', { tsrx: { compiler: '@tsrx/react', platform: 'ios' } });
91+
write_config('tsconfig.node.json', { include: ['vite.config.ts'] });
92+
write_config('tsconfig.json', {
93+
files: [],
94+
references: [{ path: './tsconfig.app.json' }, { path: './tsconfig.node.json' }],
95+
});
96+
97+
expect(resolveBuildPlatform({ root: directory })).toBe('ios');
98+
});
99+
100+
it('follows extends inside a referenced project', () => {
101+
write_config('tsconfig.base.json', { tsrx: { platform: 'android' } });
102+
write_config('tsconfig.app.json', {
103+
extends: './tsconfig.base.json',
104+
tsrx: { compiler: '@tsrx/react' },
105+
});
106+
write_config('tsconfig.json', {
107+
files: [],
108+
references: [{ path: './tsconfig.app.json' }],
109+
});
110+
111+
expect(resolveBuildPlatform({ root: directory })).toBe('android');
112+
});
113+
114+
it('prefers the solution tsconfig over a referenced project', () => {
115+
write_config('tsconfig.app.json', { tsrx: { platform: 'ios' } });
116+
write_config('tsconfig.json', {
117+
files: [],
118+
references: [{ path: './tsconfig.app.json' }],
119+
tsrx: { platform: 'web' },
120+
});
121+
122+
expect(resolveBuildPlatform({ root: directory })).toBe('web');
123+
});
124+
125+
it('resolves a directory project reference to its tsconfig.json', () => {
126+
write_config('app/tsconfig.json', { tsrx: { platform: 'web' } });
127+
write_config('tsconfig.json', {
128+
files: [],
129+
references: [{ path: './app' }],
130+
});
131+
132+
expect(resolveBuildPlatform({ root: directory })).toBe('web');
133+
});
134+
135+
it('rejects conflicting platforms across referenced projects', () => {
136+
write_config('tsconfig.app.json', { tsrx: { platform: 'ios' } });
137+
write_config('tsconfig.node.json', { tsrx: { platform: 'web' } });
138+
write_config('tsconfig.json', {
139+
files: [],
140+
references: [{ path: './tsconfig.app.json' }, { path: './tsconfig.node.json' }],
141+
});
142+
143+
expect(() => resolveBuildPlatform({ root: directory, integration: 'test builder' })).toThrow(
144+
/platform mismatch.*tsconfig\.app\.json selects "ios".*tsconfig\.node\.json selects "web"/i,
145+
);
146+
});
88147
});

0 commit comments

Comments
 (0)