| 5 | |
| 6 | // NativeScript CLI integration for live reloads, non-HMR rebuilds |
| 7 | export function cliPlugin(opts: { distOutputFolder: string; isDevMode: boolean; verbose: boolean; hmrActive: boolean }) { |
| 8 | // Capture emitted files directly from Rollup hooks to avoid relying on FS state |
| 9 | const lastBundleFiles = new Set<string>(); |
| 10 | let lastOutDir: string | undefined; |
| 11 | // This plugin should not trigger NativeScript CLI rebuild/refresh cycles when HMR is active |
| 12 | return { |
| 13 | name: 'nativescript-cli-integration', |
| 14 | apply: 'build', |
| 15 | // Ensure we run after other plugins (like vite-plugin-static-copy) |
| 16 | enforce: 'post' as const, |
| 17 | // Resolve build outDir so we can read final outputs after all plugins finish |
| 18 | configResolved(config) { |
| 19 | // Under HMR, when not initial build; skip - HMR client handles everything |
| 20 | if (opts.hmrActive && !isInitialBuild) return; |
| 21 | }, |
| 22 | // Use Rollup hook to record emitted file names (relative to output dir) |
| 23 | generateBundle(options, bundle) { |
| 24 | // Under HMR, still capture files on initial build |
| 25 | if (opts.hmrActive && !isInitialBuild) return; |
| 26 | try { |
| 27 | const keys = Object.keys(bundle); |
| 28 | for (const k of keys) lastBundleFiles.add(k); |
| 29 | // Remember outDir/file from Rollup options if provided |
| 30 | lastOutDir = (options as any).dir || ((options as any).file && path.dirname((options as any).file)) || undefined; |
| 31 | if (opts.verbose) { |
| 32 | console.log(`[cliPlugin] generateBundle: files=${keys.length}, dir=${lastOutDir ?? '(unknown)'}`); |
| 33 | if (keys.length) { |
| 34 | console.log('[cliPlugin] bundle keys:', keys); |
| 35 | } |
| 36 | } |
| 37 | } catch { |
| 38 | // noop |
| 39 | } |
| 40 | }, |
| 41 | // called only when writing to disk |
| 42 | writeBundle(options, bundle) { |
| 43 | // Under HMR, still capture files on initial build |
| 44 | if (opts.hmrActive && !isInitialBuild) return; |
| 45 | try { |
| 46 | const keys = Object.keys(bundle); |
| 47 | for (const k of keys) lastBundleFiles.add(k); |
| 48 | lastOutDir = (options as any).dir || ((options as any).file && path.dirname((options as any).file)) || lastOutDir; |
| 49 | if (opts.verbose) { |
| 50 | console.log(`[cliPlugin] writeBundle: files=${keys.length}, dir=${lastOutDir ?? '(unknown)'}`); |
| 51 | } |
| 52 | } catch { |
| 53 | // noop |
| 54 | } |
| 55 | }, |
| 56 | // Defer CLI notification until after all plugins (including static copy) are done. |
| 57 | closeBundle() { |
| 58 | if (opts.hmrActive && !isInitialBuild) { |
| 59 | // Reset trackers to avoid leaking state between builds even if no-op |
| 60 | lastBundleFiles.clear(); |
| 61 | lastOutDir = undefined; |
| 62 | return; |
| 63 | } |
| 64 | if (!opts.isDevMode) return; |