Skip to content

Commit 8aef823

Browse files
committed
fix(vite): build/prepare handling for the bundle
1 parent ef1300f commit 8aef823

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

lib/constants.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export const TNS_CORE_THEME_NAME = "nativescript-theme-core";
1717
export const SCOPED_TNS_CORE_THEME_NAME = "@nativescript/theme";
1818
export const WEBPACK_PLUGIN_NAME = "@nativescript/webpack";
1919
export const RSPACK_PLUGIN_NAME = "@nativescript/rspack";
20+
// Project-relative directory the Vite bundler writes its build output to
21+
// before the CLI copies it into the platforms app folder. Mirrors the
22+
// default value computed in `@nativescript/vite`'s base configuration
23+
// (`process.env.NS_VITE_DIST_DIR || '.ns-vite-build'`).
24+
export const VITE_DIST_FOLDER_NAME = ".ns-vite-build";
2025
export const TNS_CORE_MODULES_WIDGETS_NAME = "tns-core-modules-widgets";
2126
export const UI_MOBILE_BASE_NAME = "@nativescript/ui-mobile-base";
2227
export const TNS_ANDROID_RUNTIME_NAME = "tns-android";
@@ -172,9 +177,7 @@ export class ITMSConstants {
172177
static altoolExecutableName = "altool";
173178
}
174179

175-
class ItunesConnectApplicationTypesClass
176-
implements IiTunesConnectApplicationType
177-
{
180+
class ItunesConnectApplicationTypesClass implements IiTunesConnectApplicationType {
178181
public iOS = "iOS App";
179182
public Mac = "Mac OS X App";
180183
}
@@ -409,7 +412,7 @@ export enum IOSNativeTargetTypes {
409412
watchApp = "watch_app",
410413
watchExtension = "watch_extension",
411414
appExtension = "app_extension",
412-
application = 'application',
415+
application = "application",
413416
}
414417

415418
const pathToLoggerAppendersDir = join(

lib/services/bundler/bundler-compiler-service.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
BUNDLER_COMPILATION_COMPLETE,
1010
PackageManagers,
1111
CONFIG_FILE_NAME_DISPLAY,
12+
VITE_DIST_FOLDER_NAME,
1213
} from "../../constants";
1314
import {
1415
IPackageManager,
@@ -128,7 +129,7 @@ export class BundlerCompilerService
128129
// Copy Vite output files directly to platform destination
129130
const distOutput = path.join(
130131
projectData.projectDir,
131-
".ns-vite-build",
132+
VITE_DIST_FOLDER_NAME,
132133
);
133134
const destDir = path.join(
134135
platformData.appDestinationDirectoryPath,
@@ -373,6 +374,8 @@ export class BundlerCompilerService
373374
reject(err);
374375
});
375376

377+
const isVite = this.getBundler() === "vite";
378+
376379
childProcess.on("close", async (arg: any) => {
377380
await this.$cleanupService.removeKillProcess(
378381
childProcess.pid.toString(),
@@ -381,6 +384,34 @@ export class BundlerCompilerService
381384
delete this.bundlerProcesses[platformData.platformNameLowerCase];
382385
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
383386
if (exitCode === 0) {
387+
// Non-watch Vite builds spawn the child with stdio:"inherit"
388+
// (no IPC channel), so the emittedFiles message handler in
389+
// compileWithWatch never fires and the Vite output is never
390+
// copied to the platforms app folder. Mirror that copy step
391+
// here so release/CI prepare and build flows actually deploy
392+
// the freshly built bundle. Without this, the deploy folder
393+
// is left empty (or worse, runs stale dev/HMR artifacts from
394+
// a previous `ns debug` run) and the runtime crashes on
395+
// launch with `Check failed: has_pending_exception()`.
396+
if (isVite) {
397+
try {
398+
const distOutput = path.join(
399+
projectData.projectDir,
400+
VITE_DIST_FOLDER_NAME,
401+
);
402+
const destDir = path.join(
403+
platformData.appDestinationDirectoryPath,
404+
this.$options.hostProjectModuleName,
405+
);
406+
this.copyViteBundleToNative(distOutput, destDir);
407+
} catch (copyErr) {
408+
this.$logger.warn(
409+
`Failed to copy Vite output to platform destination: ${
410+
(copyErr as Error).message
411+
}`,
412+
);
413+
}
414+
}
384415
resolve();
385416
} else {
386417
const error: any = new Error(

0 commit comments

Comments
 (0)