@@ -90,6 +90,19 @@ export class BundlerCompilerService
9090 ) ;
9191 }
9292
93+ private getViteBuildPaths (
94+ platformData : IPlatformData ,
95+ projectData : IProjectData ,
96+ ) {
97+ return {
98+ distOutput : this . getViteDistOutputPath ( projectData . projectDir ) ,
99+ destDir : path . join (
100+ platformData . appDestinationDirectoryPath ,
101+ this . $options . hostProjectModuleName ,
102+ ) ,
103+ } ;
104+ }
105+
93106 public async compileWithWatch (
94107 platformData : IPlatformData ,
95108 projectData : IProjectData ,
@@ -158,12 +171,9 @@ export class BundlerCompilerService
158171 }
159172
160173 // Copy Vite output files directly to platform destination
161- const distOutput = this . getViteDistOutputPath (
162- projectData . projectDir ,
163- ) ;
164- const destDir = path . join (
165- platformData . appDestinationDirectoryPath ,
166- this . $options . hostProjectModuleName ,
174+ const { distOutput, destDir } = this . getViteBuildPaths (
175+ platformData ,
176+ projectData ,
167177 ) ;
168178
169179 if ( debugLog ) {
@@ -428,25 +438,21 @@ export class BundlerCompilerService
428438 // is left empty (or worse, runs stale dev/HMR artifacts from
429439 // a previous `ns debug` run) and the runtime crashes on
430440 // launch with `Check failed: has_pending_exception()`.
431- if ( isVite ) {
432- try {
433- const distOutput = this . getViteDistOutputPath (
434- projectData . projectDir ,
435- ) ;
436- const destDir = path . join (
437- platformData . appDestinationDirectoryPath ,
438- this . $options . hostProjectModuleName ,
439- ) ;
440- this . copyViteBundleToNative ( distOutput , destDir ) ;
441- } catch ( copyErr ) {
442- this . $logger . warn (
443- `Failed to copy Vite output to platform destination: ${
444- ( copyErr as Error ) . message
445- } `,
441+ // The copy must succeed for the build to succeed — a build
442+ // whose bundle never reached the native app is not a
443+ // successful build, so copy failures reject here.
444+ try {
445+ if ( isVite ) {
446+ const { distOutput, destDir } = this . getViteBuildPaths (
447+ platformData ,
448+ projectData ,
446449 ) ;
450+ this . copyViteBundleToNative ( distOutput , destDir , null , true ) ;
447451 }
452+ resolve ( ) ;
453+ } catch ( error ) {
454+ reject ( error ) ;
448455 }
449- resolve ( ) ;
450456 } else {
451457 const error : any = new Error (
452458 `Executing ${ projectData . bundler } failed with exit code ${ exitCode } .` ,
@@ -1102,6 +1108,7 @@ export class BundlerCompilerService
11021108 distOutput : string ,
11031109 destDir : string ,
11041110 specificFiles : string [ ] = null ,
1111+ failOnError = false ,
11051112 ) {
11061113 // Clean and copy Vite output to native platform folder
11071114 if ( debugLog ) {
@@ -1143,23 +1150,31 @@ export class BundlerCompilerService
11431150 console . log ( "Full build: Copying all files." ) ;
11441151 }
11451152
1153+ // Validate the source before touching the destination — cleaning
1154+ // destDir first would wipe a previously good bundle and leave an
1155+ // empty app folder behind a missing Vite output.
1156+ if ( ! this . $fs . exists ( distOutput ) ) {
1157+ throw new Error (
1158+ `Vite output directory does not exist: ${ distOutput } ` ,
1159+ ) ;
1160+ }
1161+
11461162 // Clean destination directory
11471163 if ( this . $fs . exists ( destDir ) ) {
11481164 this . $fs . deleteDirectory ( destDir ) ;
11491165 }
11501166 this . $fs . createDirectory ( destDir ) ;
11511167
11521168 // Copy all files from dist to platform destination
1153- if ( this . $fs . exists ( distOutput ) ) {
1154- this . copyRecursiveSync ( distOutput , destDir ) ;
1155- } else {
1156- this . $logger . warn (
1157- `Vite output directory does not exist: ${ distOutput } ` ,
1158- ) ;
1159- }
1169+ this . copyRecursiveSync ( distOutput , destDir ) ;
11601170 }
11611171 } catch ( error ) {
1162- this . $logger . warn ( `Failed to copy Vite bundle: ${ error . message } ` ) ;
1172+ const copyError =
1173+ error instanceof Error ? error : new Error ( String ( error ) ) ;
1174+ if ( failOnError ) {
1175+ throw copyError ;
1176+ }
1177+ this . $logger . warn ( `Failed to copy Vite bundle: ${ copyError . message } ` ) ;
11631178 }
11641179 }
11651180
0 commit comments