@nativue/core 0.15.2 → 0.15.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ // What the dev server puts before the bundle: Vite's module runtime and nativue's hot client.
2
+ import { readFileSync } from 'node:fs'
3
+ import { createRequire } from 'node:module'
4
+
5
+ const HOT_CLIENT_PATH = new URL('./hot-client.js', import.meta.url)
6
+ // The ESM syntax Hermes doesn't compile: the runtime's export list, and import() but not a method
7
+ // named import (`hooks.import(`).
8
+ export const EXPORT_LIST = /^export \{[^}]*\};?$/m
9
+ export const DYNAMIC_IMPORT = /(?<![.\w$])import\s*\(/g
10
+
11
+ export function hotPrelude(clientId, serverURL) {
12
+ const viteRequire = createRequire(createRequire(import.meta.url).resolve('vite'))
13
+ const runtime = readFileSync(viteRequire.resolve('rolldown/experimental/runtime'), 'utf8')
14
+ const client = readFileSync(HOT_CLIENT_PATH, 'utf8')
15
+ const options = JSON.stringify({ clientId, serverURL })
16
+
17
+ return `${hermesCompatible(runtime)}\nvar __NATIVUE_HOT__ = ${options};\n${client}\n`
18
+ }
19
+
20
+ /**
21
+ * Hermes compiles neither `export` nor `import()`. The runtime's import() loads lazy chunks, which
22
+ * nativue builds without, so it's never called.
23
+ */
24
+ function hermesCompatible(runtime) {
25
+ return `function __nativueDynamicImport(specifier) {
26
+ return Promise.reject(new Error('nativue builds without lazy chunks: ' + specifier))
27
+ }
28
+ ${runtime.replace(EXPORT_LIST, '').replace(DYNAMIC_IMPORT, '__nativueDynamicImport(')}`
29
+ }
package/cli/nativue.mjs CHANGED
@@ -17,10 +17,10 @@ import http from 'node:http'
17
17
  import net from 'node:net'
18
18
  import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, watch } from 'node:fs'
19
19
  import { randomUUID } from 'node:crypto'
20
- import { createRequire } from 'node:module'
21
20
  import { relative } from 'node:path'
22
21
  import { fileURLToPath } from 'node:url'
23
22
  import { compileToBytecode } from './hermes.mjs'
23
+ import { hotPrelude } from './hot-prelude.mjs'
24
24
  import { resolvePlugins } from './plugins.mjs'
25
25
  import { linkNativePackages, PLATFORMS, prebuild } from './prebuild.mjs'
26
26
  import { runOnAndroid, runOnIOS } from './run.mjs'
@@ -238,15 +238,6 @@ function vueHotUpdates(sendToApps) {
238
238
  }
239
239
  }
240
240
 
241
- function hotPrelude(clientId, serverURL) {
242
- const viteRequire = createRequire(createRequire(import.meta.url).resolve('vite'))
243
- const runtime = readFileSync(viteRequire.resolve('rolldown/experimental/runtime'), 'utf8')
244
- const client = readFileSync(frameworkPath('cli/hot-client.js'), 'utf8')
245
- const options = JSON.stringify({ clientId, serverURL })
246
-
247
- return `${runtime.replace(/^export \{[^}]*\};?$/m, '')}\nvar __NATIVUE_HOT__ = ${options};\n${client}\n`
248
- }
249
-
250
241
  // Vite's full bundle mode builds the app and, on save, a patch with the changed modules. Each app
251
242
  // runtime is a Vite client: the dev server speaks Vite's protocol for it and relays updates over SSE.
252
243
  async function dev() {
@@ -40,6 +40,21 @@ public final class NativueAppDelegate: NSObject, UIApplicationDelegate, UNUserNo
40
40
  /// Set by App.swift before any of the app's events.
41
41
  public static var plugins: [NativuePlugin] = []
42
42
 
43
+ /// iOS warns at every launch about a delegate handling remote notifications without the
44
+ /// remote-notification background mode: with no plugin listening and no mode, it doesn't handle them.
45
+ override public func responds(to selector: Selector!) -> Bool {
46
+ guard selector == Self.remoteNotificationSelector else { return super.responds(to: selector) }
47
+
48
+ let isListened = Self.plugins.contains { $0 is AppLifecycleListener }
49
+
50
+ return (isListened || Self.hasRemoteNotificationMode) && super.responds(to: selector)
51
+ }
52
+
53
+ private static let remoteNotificationSelector =
54
+ NSSelectorFromString("application:didReceiveRemoteNotification:fetchCompletionHandler:")
55
+ private static let hasRemoteNotificationMode =
56
+ (Bundle.main.object(forInfoDictionaryKey: "UIBackgroundModes") as? [String])?.contains("remote-notification") == true
57
+
43
58
  private var lifecycleListeners: [AppLifecycleListener] {
44
59
  Self.plugins.compactMap { $0 as? AppLifecycleListener }
45
60
  }
package/ios/Bridge.swift CHANGED
@@ -158,7 +158,7 @@ final class Bridge {
158
158
  }
159
159
 
160
160
  private func modalTransitionInProgress() -> UIViewControllerTransitionCoordinator? {
161
- guard let presentedViewController = rootView.window?.rootViewController?.presentedViewController else {
161
+ guard let presentedViewController = rootView.hostWindow?.rootViewController?.presentedViewController else {
162
162
  return nil
163
163
  }
164
164
 
@@ -200,7 +200,7 @@ final class Bridge {
200
200
  windowSizeDidChange(to: rootView.bounds.size)
201
201
 
202
202
  for contentRoot in contentRootsByNodeId.values {
203
- (contentRoot as? ModalController)?.dismissIfDetached()
203
+ (contentRoot as? ModalController)?.followPlaceholder()
204
204
  contentRoot.layoutContent()
205
205
  }
206
206
  }
@@ -440,7 +440,7 @@ final class Bridge {
440
440
  }
441
441
 
442
442
  define("dismissKeyboard") { [unowned self] _ in
443
- enqueue { [unowned self] in rootView.window?.endEditing(true) }
443
+ enqueue { [unowned self] in rootView.hostWindow?.endEditing(true) }
444
444
  }
445
445
 
446
446
  return JavaScriptRuntime(functions: functions) { [unowned self] message, stack in
@@ -491,8 +491,7 @@ final class Bridge {
491
491
  // MARK: - Main thread
492
492
 
493
493
  private func resetNodeTree() {
494
- // Sheets of the old tree's screen stacks too, which only their stack knows about.
495
- rootView.window?.rootViewController?.dismiss(animated: false)
494
+ dismissPresentedControllers()
496
495
  rootView.subviews.forEach { $0.removeFromSuperview() }
497
496
  contentRootsByNodeId.values.forEach { ($0 as? ModalController)?.setVisible(false) }
498
497
  contentRootsByNodeId = [:]
@@ -539,7 +538,7 @@ final class Bridge {
539
538
  private func forgetNode(id: Int) {
540
539
  let node = nodesById.removeValue(forKey: id)
541
540
  (node?.view as? ScreenStackView)?.dismissPresentedScreens()
542
- modalController(id)?.dismissIfDetached()
541
+ modalController(id)?.detach()
543
542
  contentRootsByNodeId[id] = nil
544
543
  gestureReportersByNodeId[id] = nil
545
544
  reportedLayoutsByNodeId[id] = nil
@@ -569,6 +568,29 @@ final class Bridge {
569
568
  evaluate { [unowned self] in runtime?.call("__layoutsChanged", arguments: [changedLayouts]) }
570
569
  }
571
570
 
571
+ /// What's presented over the controller holding the root view: an app's own sheet holding the
572
+ /// NativueView stays.
573
+ private func dismissPresentedControllers() {
574
+ guard let root = rootView.hostWindow?.rootViewController else { return }
575
+
576
+ let presentations = Array(sequence(first: root, next: \.presentedViewController))
577
+ guard let holderIndex = presentations.lastIndex(where: { rootView.isDescendant(of: $0.view) }),
578
+ holderIndex < presentations.count - 1 else { return }
579
+
580
+ presentations[holderIndex].dismiss(animated: false)
581
+ }
582
+
583
+ /// In the window, or under a full-screen modal that the controller holding it presented: UIKit takes
584
+ /// the views under one out of the window. A screen pushed under another, or an unselected tab, is not.
585
+ private func isOnScreen(_ view: UIView) -> Bool {
586
+ if view.window != nil { return true }
587
+
588
+ let holder = sequence(first: view, next: { $0.superview }).compactMap { $0.next as? UIViewController }.last
589
+ guard let holder, let presented = holder.presentedViewController else { return false }
590
+
591
+ return presented.presentingViewController === holder
592
+ }
593
+
572
594
  private func modalController(_ id: Int) -> ModalController? {
573
595
  contentRootsByNodeId[id] as? ModalController
574
596
  }
@@ -682,6 +704,10 @@ final class Bridge {
682
704
  case "Modal":
683
705
  let modalController = ModalController()
684
706
  modalController.onEvent = { [unowned self] eventName in dispatchEvent(eventName, toNodeId: id) }
707
+ modalController.hostWindow = { [unowned self] in rootView.hostWindow }
708
+ modalController.isPlaceholderOnScreen = { [unowned self, unowned modalController] in
709
+ isOnScreen(modalController.placeholderView)
710
+ }
685
711
  contentRootsByNodeId[id] = modalController
686
712
 
687
713
  let node = NativeNode(view: modalController.placeholderView, measuresContent: false)
@@ -14,6 +14,11 @@ final class ModalController: UIViewController, ContentRoot, UIAdaptivePresentati
14
14
  let placeholderView = UIView()
15
15
 
16
16
  var onEvent: ((String) -> Void)?
17
+ /// The app's window: the placeholder's is nil while a full-screen modal covers it.
18
+ var hostWindow: () -> UIWindow? = { nil }
19
+ /// The placeholder is in the window, or covered by a full-screen modal: not in a screen pushed under
20
+ /// another, an unselected tab or a deactivated <KeepAlive>.
21
+ var isPlaceholderOnScreen: () -> Bool = { false }
17
22
  var animationType = "none"
18
23
  var presentationStyleName = "fullScreen"
19
24
  var transparent = false
@@ -39,20 +44,26 @@ final class ModalController: UIViewController, ContentRoot, UIAdaptivePresentati
39
44
  contentNode.calculateLayout(width: view.bounds.width, height: view.bounds.height)
40
45
  }
41
46
 
42
- /// Deferred to after the current batch, so the placeholder is already in the window when presenting.
47
+ /// Deferred to after the current batch, so the placeholder is already on screen when presenting.
43
48
  func setVisible(_ visible: Bool) {
44
49
  wantsVisible = visible
45
50
  DispatchQueue.main.async { [self] in applyVisibility() }
46
51
  }
47
52
 
48
53
  /// The <Modal> left the tree (an ancestor's v-if): its content must go too.
49
- func dismissIfDetached() {
50
- guard placeholderView.window == nil else { return }
51
-
54
+ func detach() {
52
55
  wantsVisible = false
53
56
  applyVisibility()
54
57
  }
55
58
 
59
+ /// Closed while its placeholder is off screen (a tab switched, a <KeepAlive> deactivated), and shown
60
+ /// again when it's back, if JS still wants it.
61
+ func followPlaceholder() {
62
+ guard !isTransitioning else { return }
63
+
64
+ if !isPlaceholderOnScreen() { dismissIfPresented() } else if wantsVisible && !isPresented { present() }
65
+ }
66
+
56
67
  private func applyVisibility() {
57
68
  guard !isTransitioning else { return }
58
69
 
@@ -60,7 +71,7 @@ final class ModalController: UIViewController, ContentRoot, UIAdaptivePresentati
60
71
  }
61
72
 
62
73
  private func present() {
63
- guard !isPresented, let presenter = topViewController() else { return }
74
+ guard !isPresented, isPlaceholderOnScreen(), let presenter = topViewController() else { return }
64
75
 
65
76
  view.backgroundColor = transparent ? .clear : .systemBackground
66
77
  modalPresentationStyle = transparent
@@ -89,7 +100,7 @@ final class ModalController: UIViewController, ContentRoot, UIAdaptivePresentati
89
100
  }
90
101
 
91
102
  private func topViewController() -> UIViewController? {
92
- var top = placeholderView.window?.rootViewController
103
+ var top = hostWindow()?.rootViewController
93
104
 
94
105
  while let presented = top?.presentedViewController, presented !== self {
95
106
  top = presented
@@ -42,6 +42,8 @@ struct VueRoot: UIViewRepresentable {
42
42
  final class RootView: UIView {
43
43
  var onLayoutSubviews: (() -> Void)?
44
44
  var onTraitCollectionChange: ((_ previous: UITraitCollection?) -> Void)?
45
+ /// `window` is nil under a full-screen modal: the bridge finds that modal through this one.
46
+ private(set) weak var hostWindow: UIWindow?
45
47
 
46
48
  override func layoutSubviews() {
47
49
  super.layoutSubviews()
@@ -53,6 +55,14 @@ final class RootView: UIView {
53
55
  onTraitCollectionChange?(previous)
54
56
  }
55
57
 
58
+ override func didMoveToWindow() {
59
+ super.didMoveToWindow()
60
+ if let window { hostWindow = window }
61
+ #if DEBUG
62
+ becomeFirstResponder()
63
+ #endif
64
+ }
65
+
56
66
  #if DEBUG
57
67
  var onReloadShortcut: (() -> Void)?
58
68
 
@@ -62,11 +72,6 @@ final class RootView: UIView {
62
72
  [UIKeyCommand(input: "r", modifierFlags: .command, action: #selector(reloadShortcutPressed))]
63
73
  }
64
74
 
65
- override func didMoveToWindow() {
66
- super.didMoveToWindow()
67
- becomeFirstResponder()
68
- }
69
-
70
75
  @objc private func reloadShortcutPressed() {
71
76
  onReloadShortcut?()
72
77
  }
@@ -73,6 +73,7 @@ final class ScreenStackView: UIView, ScreenContainer, UINavigationControllerDele
73
73
  super.didMoveToWindow()
74
74
 
75
75
  embedInNearestViewController(navigationController)
76
+ if window != nil { scheduleSync() }
76
77
  }
77
78
 
78
79
  private func configure(_ navigationController: UINavigationController) {
@@ -185,9 +186,14 @@ final class ScreenStackView: UIView, ScreenContainer, UINavigationControllerDele
185
186
  }
186
187
 
187
188
  private func presentModal(_ group: [ScreenController]) {
188
- let modalNavigationController = UINavigationController()
189
189
  let presenter = modalNavigationControllers.last ?? navigationController
190
190
 
191
+ // Out of the window under a full-screen modal: UIKit would refuse without calling back. didMoveToWindow
192
+ // syncs again once it's back.
193
+ guard presenter.view.window != nil else { return }
194
+
195
+ let modalNavigationController = UINavigationController()
196
+
191
197
  configure(modalNavigationController)
192
198
  modalNavigationController.setViewControllers(group, animated: false)
193
199
  modalNavigationController.modalPresentationStyle =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nativue/core",
3
- "version": "0.15.2",
3
+ "version": "0.15.4",
4
4
  "description": "Vue rendering real native views on iOS and Android",
5
5
  "keywords": [
6
6
  "vue",
@@ -37,14 +37,14 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@vitejs/plugin-vue": "^6.0.11",
40
- "@vue/runtime-core": "^3.5.45",
40
+ "@vue/runtime-core": "^3.5.46",
41
41
  "hermes-compiler": "260318099.0.8",
42
- "vite": "^8.3.8",
43
- "vue": "^3.5.45",
42
+ "vite": "^8.4.0",
43
+ "vue": "^3.5.46",
44
44
  "whatwg-fetch": "^3.6.20"
45
45
  },
46
46
  "devDependencies": {
47
- "typescript": "^5.9.3",
47
+ "typescript": "^6.0.3",
48
48
  "vue-tsc": "^3.3.14"
49
49
  },
50
50
  "files": [