Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ injector.require(
"viteHmrPortService",
"./services/bundler/vite-hmr-port-service",
);
injector.require(
"iOSDeviceTunnelService",
"./services/ios/ios-device-tunnel-service",
);

injector.require(
"applePortalSessionService",
Expand Down
1 change: 1 addition & 0 deletions lib/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export { Errors } from "./errors";
export { FileSystem } from "./file-system";
export { HostInfo } from "./host-info";
export { HttpClient } from "./http-client";
export { IOSDeviceTunnelService } from "./ios-device-tunnel-service";
export { Logger } from "./logger";
export { PackageManager } from "./package-manager";
export { ProjectData } from "./project-data";
Expand Down
25 changes: 25 additions & 0 deletions lib/contracts/ios-device-tunnel-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Contract } from "../common/di/contract";

/**
* Holds the CoreDevice tunnel of a USB-connected iOS 17+ / visionOS device
* so an app can reach the Vite dev server on the Mac over the cable, without
* Wi-Fi or the Local Network prompt.
*/
@Contract({ name: "iOSDeviceTunnelService" })
export abstract class IOSDeviceTunnelService {
/**
* Opens the device's CoreDevice tunnel and keeps it up until `close` or
* the CLI exits. Resolves the Mac's address on the tunnel, or null when
* the device has no usable USB tunnel. A tunnel gets a new address every
* time it is re-established.
*/
abstract open(device: Mobile.IDevice): Promise<string | null>;

/**
* The Mac's tunnel address for the single device of `platform` that has
* an open tunnel, if any.
*/
abstract getHostAddress(platform: string): string | undefined;

abstract close(deviceIdentifier: string): Promise<void>;
}
71 changes: 71 additions & 0 deletions lib/controllers/run-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "../common/declarations";
import { IInjector } from "../common/definitions/yok";
import { ViteHmrPortService } from "../contracts/vite-hmr-port-service";
import { IOSDeviceTunnelService } from "../contracts/ios-device-tunnel-service";
import { injector } from "../common/yok";

export class RunController extends EventEmitter implements IRunController {
Expand Down Expand Up @@ -66,6 +67,7 @@ export class RunController extends EventEmitter implements IRunController {
protected $projectDataService: IProjectDataService,
private $staticConfig: Config.IStaticConfig,
private $viteHmrPortService: ViteHmrPortService,
private $iOSDeviceTunnelService: IOSDeviceTunnelService,
) {
super();
}
Expand Down Expand Up @@ -167,6 +169,10 @@ export class RunController extends EventEmitter implements IRunController {
_.includes(deviceIdentifiersToRemove, descriptor.identifier),
).map((descriptor) => descriptor.identifier);

for (const deviceIdentifier of removedDeviceIdentifiers) {
await this.$iOSDeviceTunnelService.close(deviceIdentifier);
}

// Handle the case when no more devices left for any of the persisted platforms

for (let i = 0; i < liveSyncProcessInfo.platforms.length; i++) {
Expand Down Expand Up @@ -647,6 +653,13 @@ export class RunController extends EventEmitter implements IRunController {
platform: string;
isEmulator: boolean;
}> = {};
// Snapshot before any device action: a device whose sync fails leaves
// the session but must still count as a target of its platform.
const sessionDeviceDescriptors = [
...this.$liveSyncProcessDataService.getDeviceDescriptors(
projectData.projectDir,
),
];

const deviceAction = async (device: Mobile.IDevice) => {
const deviceDescriptor = _.find(
Expand Down Expand Up @@ -679,6 +692,12 @@ export class RunController extends EventEmitter implements IRunController {
liveSyncInfo,
"pre-build",
);
await this.openIOSViteHmrTunnel(
device,
projectData,
liveSyncInfo,
sessionDeviceDescriptors,
);

const prepareResultData =
await this.$prepareController.prepare(prepareData);
Expand Down Expand Up @@ -819,6 +838,58 @@ export class RunController extends EventEmitter implements IRunController {
);
}

/**
* Serves Vite HMR to a USB-connected iOS / visionOS device over its
* CoreDevice tunnel. One dev server bakes one origin into `bundle.mjs`
* and a tunnel address is reachable only from its own device, so this
* applies only when the device is the session's only target of its
* platform. Must run before `prepare` spawns the Vite children.
*/
private async openIOSViteHmrTunnel(
device: Mobile.IDevice,
projectData: IProjectData,
liveSyncInfo: ILiveSyncInfo,
sessionDeviceDescriptors: ILiveSyncDeviceDescriptor[],
): Promise<void> {
const { platform, identifier } = device.deviceInfo;
if (
!this.$mobileHelper.isApplePlatform(platform) ||
projectData.bundler !== "vite" ||
liveSyncInfo.skipWatcher ||
!liveSyncInfo.useHotModuleReload ||
liveSyncInfo.release ||
process.env.NS_HMR_HOST
) {
return;
}
if (
!this.$bundlerCompilerService.acceptsIPv6DevHost(platform, projectData)
) {
this.$logger.trace(
`Not using a USB tunnel for ${identifier}: the installed @nativescript/vite does not accept an IPv6 dev host.`,
);
return;
}

const platformTargets = sessionDeviceDescriptors.filter((descriptor) => {
const target = this.$devicesService.getDeviceByIdentifier(
descriptor.identifier,
);
return (
!target ||
target.deviceInfo.platform.toLowerCase() === platform.toLowerCase()
);
});
if (platformTargets.length !== 1) {
this.$logger.trace(
`Not using a USB tunnel for ${identifier}: the session has ${platformTargets.length} ${platform} targets.`,
);
return;
}

await this.$iOSDeviceTunnelService.open(device);
}

/**
* Set up `adb reverse tcp:<port> tcp:<port>` for an Android device
* when the project bundles with Vite in HMR/watch mode, then export
Expand Down
33 changes: 30 additions & 3 deletions lib/services/bundler/bundler-compiler-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from "../../common/declarations";
import { ICleanupService } from "../../definitions/cleanup-service";
import { ViteHmrPortService } from "../../contracts/vite-hmr-port-service";
import { IOSDeviceTunnelService } from "../../contracts/ios-device-tunnel-service";
import { injector } from "../../common/yok";
import {
resolvePackagePath,
Expand Down Expand Up @@ -121,6 +122,7 @@ export class BundlerCompilerService
private $packageInstallationManager: IPackageInstallationManager, // private $sharedEventBus: ISharedEventBus
private $projectConfigService: IProjectConfigService,
private $viteHmrPortService: ViteHmrPortService,
private $iOSDeviceTunnelService: IOSDeviceTunnelService,
) {
super();
}
Expand Down Expand Up @@ -795,19 +797,38 @@ export class BundlerCompilerService
if (process.env[VITE_SINGLE_PROCESS_ENV] === "0") {
return false;
}
return this.getViteSingleProcessPlatforms(projectData).includes(platform);
}

/**
* Older `@nativescript/vite` releases bind the dev server to
* `NS_HMR_HOST` and put an IPv6 literal into URLs unbracketed. The fix
* shipped together with single-process dev, so its platform list is the
* capability marker.
*/
public acceptsIPv6DevHost(
platform: string,
projectData: IProjectData,
): boolean {
return this.getViteSingleProcessPlatforms(projectData).includes(
platform.toLowerCase(),
);
}

private getViteSingleProcessPlatforms(projectData: IProjectData): string[] {
try {
const packageJSONPath = resolvePackageJSONPath(
this.getBundlerPackageName(),
{ paths: [projectData.projectDir] },
);
if (!packageJSONPath) {
return false;
return [];
}
const platforms =
this.$fs.readJson(packageJSONPath)?.nativescriptCli?.singleProcessDev;
return Array.isArray(platforms) && platforms.includes(platform);
return Array.isArray(platforms) ? platforms : [];
} catch {
return false;
return [];
}
}

Expand All @@ -831,6 +852,8 @@ export class BundlerCompilerService
* sessions — the dev-server port. The port is resolved here, once, and
* handed to `@nativescript/vite` as `NS_HMR_PORT`, so the URLs baked into
* `bundle.mjs`, the server's bind and the `adb reverse` tunnel all match.
* When the platform's only device has a USB tunnel open, its Mac address
* becomes `NS_HMR_HOST`, unless the user set one.
*/
private async getViteChildEnv(
platform: string,
Expand All @@ -843,6 +866,10 @@ export class BundlerCompilerService
env.NS_HMR_PORT = String(
await this.$viteHmrPortService.getPort(platform),
);
const tunnelHost = this.$iOSDeviceTunnelService.getHostAddress(platform);
if (tunnelHost && !process.env.NS_HMR_HOST) {
env.NS_HMR_HOST = tunnelHost;
}
}
return env;
}
Expand Down
5 changes: 5 additions & 0 deletions lib/services/bundler/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ declare global {
prepareData: IPrepareData,
): Promise<void>;
stopBundlerCompiler(platform: string): Promise<void>;
/**
* Whether the installed `@nativescript/vite` accepts a bare IPv6
* address as `NS_HMR_HOST` for `platform`.
*/
acceptsIPv6DevHost(platform: string, projectData: IProjectData): boolean;
}

/**
Expand Down
Loading
Loading