MCPcopy Create free account
hub / github.com/expo/expo / fixObjCSwiftHeaderModuleReferencesAsync

Function fixObjCSwiftHeaderModuleReferencesAsync

tools/src/prebuilds/Frameworks.ts:1353–1409  ·  view source on GitHub ↗
(
  swiftHeaderPath: string,
  spmConfig: SPMConfig
)

Source from the content-addressed store, hash-verified

1351 * @param spmConfig SPM configuration containing product/target mappings
1352 */
1353const fixObjCSwiftHeaderModuleReferencesAsync = async (
1354 swiftHeaderPath: string,
1355 spmConfig: SPMConfig
1356): Promise<void> => {
1357 // Determine which product this header belongs to based on its path
1358 const pathParts = swiftHeaderPath.split('/');
1359 const frameworkName = pathParts.find((p) => p.endsWith('.framework'))?.replace('.framework', '');
1360 const currentProduct = frameworkName || '';
1361
1362 // Read the header file
1363 let content = await fs.readFile(swiftHeaderPath, 'utf8');
1364 let modified = false;
1365
1366 // Remove @import statements for internal SPM targets
1367 // These modules don't exist when consumed via CocoaPods
1368 for (const product of spmConfig.products) {
1369 for (const target of product.targets) {
1370 if (target.name === product.name) {
1371 continue; // Skip targets that have same name as product
1372 }
1373
1374 // Match @import TargetName; statements
1375 const importPattern = new RegExp(`^@import ${target.name};\\s*$`, 'gm');
1376 if (product.name === currentProduct) {
1377 // Same framework - just remove the import, the types are already available
1378 const replaced = content.replace(
1379 importPattern,
1380 `// Removed internal target import: ${target.name}`
1381 );
1382 if (replaced !== content) {
1383 content = replaced;
1384 modified = true;
1385 }
1386 } else {
1387 // Different framework - replace with import of the product module
1388 const replaced = content.replace(importPattern, `@import ${product.name};`);
1389 if (replaced !== content) {
1390 content = replaced;
1391 modified = true;
1392 }
1393 }
1394 }
1395 }
1396
1397 // Remove @import React - React.xcframework has VFS overlays that cause issues
1398 const reactImportPattern = /^@import React;\s*$/gm;
1399 const reactReplaced = content.replace(reactImportPattern, '// Removed: @import React;');
1400 if (reactReplaced !== content) {
1401 content = reactReplaced;
1402 modified = true;
1403 }
1404
1405 // Write back if modified
1406 if (modified) {
1407 await fs.writeFile(swiftHeaderPath, content, 'utf8');
1408 }
1409};
1410

Callers 1

Calls 3

findMethod · 0.80
writeFileMethod · 0.80
replaceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…