@nativue/core 0.1.0 → 0.3.0

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.
Files changed (42) hide show
  1. package/Package.swift +26 -2
  2. package/android/build.gradle.kts +2 -2
  3. package/android/consumer-rules.pro +3 -0
  4. package/android/nativue.settings.gradle.kts +56 -0
  5. package/android/src/main/java/dev/nativue/Bridge.kt +51 -7
  6. package/android/src/main/java/dev/nativue/ModalHost.kt +2 -2
  7. package/android/src/main/java/dev/nativue/NativueActivity.kt +8 -0
  8. package/android/src/main/java/dev/nativue/Navigation.kt +32 -24
  9. package/android/src/main/java/dev/nativue/Plugins.kt +75 -0
  10. package/android/src/main/java/dev/nativue/Style.kt +5 -1
  11. package/android/src/main/java/dev/nativue/Theme.kt +61 -0
  12. package/android/src/main/java/dev/nativue/Views.kt +14 -2
  13. package/cli/config.d.mts +19 -0
  14. package/cli/config.mjs +4 -0
  15. package/cli/link.mjs +129 -0
  16. package/cli/nativue.mjs +67 -20
  17. package/cli/symbolicate.mjs +3 -5
  18. package/ios/Bridge.swift +207 -102
  19. package/ios/JavaScriptRuntime/JavaScriptRuntime.mm +231 -0
  20. package/ios/JavaScriptRuntime/include/NativueJavaScriptRuntime.h +29 -0
  21. package/ios/JavaScriptRuntime/vendor/hermes/LICENSE +21 -0
  22. package/ios/JavaScriptRuntime/vendor/hermes/Public/CrashManager.h +107 -0
  23. package/ios/JavaScriptRuntime/vendor/hermes/Public/CtorConfig.h +145 -0
  24. package/ios/JavaScriptRuntime/vendor/hermes/Public/GCConfig.h +211 -0
  25. package/ios/JavaScriptRuntime/vendor/hermes/Public/GCTripwireContext.h +40 -0
  26. package/ios/JavaScriptRuntime/vendor/hermes/Public/HermesExport.h +14 -0
  27. package/ios/JavaScriptRuntime/vendor/hermes/Public/RuntimeConfig.h +141 -0
  28. package/ios/JavaScriptRuntime/vendor/hermes/Public/SamplingProfiler.h +270 -0
  29. package/ios/JavaScriptRuntime/vendor/hermes/hermes.h +242 -0
  30. package/ios/NativeNode.swift +1 -0
  31. package/ios/NativueView.swift +10 -3
  32. package/ios/Plugins.swift +45 -0
  33. package/ios/Style.swift +14 -1
  34. package/package.json +7 -2
  35. package/src/appearance.ts +49 -0
  36. package/src/components.ts +3 -1
  37. package/src/entry.ts +2 -1
  38. package/src/globals.ts +3 -0
  39. package/src/native-functions.ts +41 -0
  40. package/src/renderer.ts +58 -1
  41. package/src/types.ts +2 -0
  42. package/src/virtual-modules.d.ts +2 -0
package/cli/config.mjs ADDED
@@ -0,0 +1,4 @@
1
+ // `import { defineConfig } from '@nativue/core/config'` in nativue.config.ts: only types the config.
2
+ export function defineConfig(config) {
3
+ return config
4
+ }
package/cli/link.mjs ADDED
@@ -0,0 +1,129 @@
1
+ // Links the iOS side of the app's packages: those whose package.json has
2
+ // "nativue": { "ios": { "sources": "ios", "plugin": "HapticsPlugin" } }
3
+ // ios/NativuePlugins becomes a Swift package with a target per package (a link to its sources) and
4
+ // `nativuePlugins`, the list App.swift passes to NativueView. The app's Xcode project depends on it
5
+ // alone; it re-exports Nativue. (Android finds them itself: android/nativue.settings.gradle.kts.)
6
+ import {
7
+ existsSync,
8
+ mkdirSync,
9
+ readdirSync,
10
+ readFileSync,
11
+ readlinkSync,
12
+ rmSync,
13
+ symlinkSync,
14
+ writeFileSync,
15
+ } from 'node:fs'
16
+ import { dirname, join, relative, resolve } from 'node:path'
17
+
18
+ const PLUGINS_DIRECTORY = 'ios/NativuePlugins'
19
+ const SOURCES_DIRECTORY = `${PLUGINS_DIRECTORY}/Sources`
20
+ const LIST_TARGET = 'NativuePlugins'
21
+
22
+ function readJson(path) {
23
+ return JSON.parse(readFileSync(path, 'utf8'))
24
+ }
25
+
26
+ /** node_modules/<name>, looked up the way Node does: in the app's folder, then in its parents. */
27
+ function packageDirectory(name) {
28
+ for (let directory = process.cwd(); ; directory = dirname(directory)) {
29
+ const candidate = join(directory, 'node_modules', name)
30
+ if (existsSync(join(candidate, 'package.json'))) return candidate
31
+ if (directory === dirname(directory)) return null
32
+ }
33
+ }
34
+
35
+ /** '@nativue/haptics' -> 'NativueHaptics', the Swift module of its sources. */
36
+ function moduleName(packageName) {
37
+ return packageName
38
+ .split(/[^a-zA-Z0-9]+/)
39
+ .filter(Boolean)
40
+ .map((word) => word[0].toUpperCase() + word.slice(1))
41
+ .join('')
42
+ }
43
+
44
+ function iosPlugins() {
45
+ const { dependencies = {}, devDependencies = {} } = readJson('package.json')
46
+ const names = [...Object.keys(dependencies), ...Object.keys(devDependencies)]
47
+
48
+ return names.flatMap((name) => {
49
+ const directory = packageDirectory(name)
50
+ const ios = directory && readJson(join(directory, 'package.json')).nativue?.ios
51
+ if (!ios) return []
52
+
53
+ return [{ module: moduleName(name), sources: join(directory, ios.sources), plugin: ios.plugin }]
54
+ })
55
+ }
56
+
57
+ function writeIfChanged(path, content) {
58
+ if (existsSync(path) && readFileSync(path, 'utf8') === content) return
59
+
60
+ mkdirSync(dirname(path), { recursive: true })
61
+ writeFileSync(path, content)
62
+ }
63
+
64
+ /** Sources/<module> -> the package's sources; links of packages the app no longer has are removed. */
65
+ function linkSources(plugins) {
66
+ const linkTargets = new Map(
67
+ plugins.map((plugin) => [plugin.module, relative(SOURCES_DIRECTORY, plugin.sources)]),
68
+ )
69
+ mkdirSync(SOURCES_DIRECTORY, { recursive: true })
70
+
71
+ for (const entry of readdirSync(SOURCES_DIRECTORY)) {
72
+ if (entry !== LIST_TARGET && !linkTargets.has(entry)) rmSync(join(SOURCES_DIRECTORY, entry))
73
+ }
74
+
75
+ for (const [module, target] of linkTargets) {
76
+ const link = join(SOURCES_DIRECTORY, module)
77
+ if (existsSync(link) && readlinkSync(link) === target) continue
78
+
79
+ rmSync(link, { force: true })
80
+ symlinkSync(target, link)
81
+ }
82
+ }
83
+
84
+ function packageManifest(plugins, corePath) {
85
+ const nativue = '.product(name: "Nativue", package: "core")'
86
+ const pluginTargets = plugins.map(
87
+ ({ module }) =>
88
+ ` .target(name: "${module}", dependencies: [${nativue}], path: "Sources/${module}"),\n`,
89
+ )
90
+ const listDependencies = [nativue, ...plugins.map(({ module }) => `"${module}"`)].join(', ')
91
+
92
+ return `// swift-tools-version: 5.9
93
+ // Generated by nativue from the app's packages (packages/core/cli/link.mjs): rewritten on every build.
94
+ import PackageDescription
95
+
96
+ let package = Package(
97
+ name: "NativuePlugins",
98
+ platforms: [.iOS(.v17)],
99
+ products: [.library(name: "NativuePlugins", targets: ["${LIST_TARGET}"])],
100
+ dependencies: [.package(path: "${corePath}")],
101
+ targets: [
102
+ ${pluginTargets.join('')} .target(name: "${LIST_TARGET}", dependencies: [${listDependencies}], path: "Sources/${LIST_TARGET}"),
103
+ ]
104
+ )
105
+ `
106
+ }
107
+
108
+ function pluginList(plugins) {
109
+ const imports = plugins.map(({ module }) => `import ${module}\n`).join('')
110
+ const instances = plugins.map(({ plugin }) => `${plugin}()`).join(', ')
111
+
112
+ return `// Generated by nativue from the app's packages (packages/core/cli/link.mjs): rewritten on every build.
113
+ @_exported import Nativue
114
+ ${imports}
115
+ public let nativuePlugins: [NativuePlugin] = [${instances}]
116
+ `
117
+ }
118
+
119
+ /** coreDirectory: @nativue/core's folder, which holds its Package.swift. */
120
+ export function linkIosPlugins(coreDirectory) {
121
+ if (!existsSync('ios') || !existsSync('package.json')) return
122
+
123
+ const plugins = iosPlugins()
124
+ const corePath = relative(resolve(PLUGINS_DIRECTORY), coreDirectory)
125
+
126
+ linkSources(plugins)
127
+ writeIfChanged(`${PLUGINS_DIRECTORY}/Package.swift`, packageManifest(plugins, corePath))
128
+ writeIfChanged(`${SOURCES_DIRECTORY}/${LIST_TARGET}/Plugins.swift`, pluginList(plugins))
129
+ }
package/cli/nativue.mjs CHANGED
@@ -1,20 +1,21 @@
1
1
  #!/usr/bin/env node
2
2
  // The nativue CLI, run from the app's folder:
3
3
  // nativue build bundles src/app/ into ios/App/main.bundle.js (and, with an android/
4
- // folder, android/app/src/main/assets/main.bundle.js; Hermes bytecode
5
- // with --production)
4
+ // folder, android/app/src/main/assets/main.bundle.js); Hermes bytecode
5
+ // with --production
6
6
  // nativue dev rebuilds on save and tells the app (via SSE) to reload the bundle
7
7
  // nativue run <ios | android> builds, installs and opens the app on a simulator or emulator, then
8
8
  // keeps going as `nativue dev` (unless one is already running)
9
- import { build } from 'vite'
9
+ import { build, loadConfigFromFile } from 'vite'
10
10
  import vue from '@vitejs/plugin-vue'
11
11
  import http from 'node:http'
12
12
  import net from 'node:net'
13
13
  import { execFileSync } from 'node:child_process'
14
- import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs'
14
+ import { copyFileSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync } from 'node:fs'
15
15
  import { createRequire } from 'node:module'
16
16
  import { dirname } from 'node:path'
17
17
  import { fileURLToPath } from 'node:url'
18
+ import { linkIosPlugins } from './link.mjs'
18
19
  import { runOnAndroid, runOnIOS } from './run.mjs'
19
20
  import { createSymbolicator } from './symbolicate.mjs'
20
21
 
@@ -59,31 +60,74 @@ function hermescPath() {
59
60
  return `${packageDirectory}/hermesc/${HERMESC_BY_PLATFORM[process.platform]}`
60
61
  }
61
62
 
62
- // The Android app packages its assets folder, so it gets the bundle. A production one is compiled
63
- // to Hermes bytecode, which starts faster: no parsing when the app opens. The runtime tells
64
- // bytecode from source on its own, so the file keeps its name.
65
- function bundleForAndroid(mode) {
63
+ // Hermes bytecode starts faster: no parsing when the app opens. The runtime tells bytecode from
64
+ // source on its own, so the file keeps its name.
65
+ function compileToBytecode(bundlePath) {
66
+ const bytecodePath = `${bundlePath}.hbc`
67
+
68
+ execFileSync(hermescPath(), ['-emit-binary', '-O', '-w', '-out', bytecodePath, bundlePath], {
69
+ stdio: 'inherit',
70
+ })
71
+ renameSync(bytecodePath, bundlePath)
72
+ }
73
+
74
+ // A production bundle becomes bytecode; the Android app packages its assets folder, so it gets a copy.
75
+ function finishBundle(mode) {
66
76
  return {
67
- name: 'nativue-android-bundle',
77
+ name: 'nativue-finish-bundle',
68
78
  writeBundle() {
79
+ if (mode === 'production') compileToBytecode(BUNDLE_PATH)
69
80
  if (!existsSync('android')) return
70
81
 
71
- const androidBundlePath = `${ANDROID_ASSETS_DIRECTORY}/main.bundle.js`
72
82
  mkdirSync(ANDROID_ASSETS_DIRECTORY, { recursive: true })
83
+ copyFileSync(BUNDLE_PATH, `${ANDROID_ASSETS_DIRECTORY}/main.bundle.js`)
84
+ },
85
+ }
86
+ }
73
87
 
74
- if (mode !== 'production') return copyFileSync(BUNDLE_PATH, androidBundlePath)
88
+ const CONFIG_FILES = [
89
+ 'nativue.config.ts',
90
+ 'nativue.config.mts',
91
+ 'nativue.config.js',
92
+ 'nativue.config.mjs',
93
+ ]
94
+ const PLUGIN_RUNTIMES_MODULE = 'virtual:nativue/plugin-runtimes'
75
95
 
76
- execFileSync(
77
- hermescPath(),
78
- ['-emit-binary', '-O', '-w', '-out', androidBundlePath, BUNDLE_PATH],
79
- {
80
- stdio: 'inherit',
81
- },
82
- )
83
- },
96
+ // nativue.config.ts, when the app has one (see config.d.mts).
97
+ async function loadAppConfig() {
98
+ const configFile = CONFIG_FILES.find((file) => existsSync(file))
99
+ if (!configFile) return {}
100
+
101
+ const { config } = await loadConfigFromFile(
102
+ { command: 'build', mode: 'development' },
103
+ configFile,
104
+ process.cwd(),
105
+ 'silent',
106
+ )
107
+
108
+ return config
109
+ }
110
+
111
+ // The entry imports this module first: it imports each plugin's runtime module.
112
+ function pluginRuntimes(plugins) {
113
+ const resolvedId = `\0${PLUGIN_RUNTIMES_MODULE}`
114
+ const imports = plugins
115
+ .filter((plugin) => plugin.runtime)
116
+ .map((plugin) => `import ${JSON.stringify(plugin.runtime)}`)
117
+
118
+ return {
119
+ name: 'nativue-plugin-runtimes',
120
+ resolveId: (id) => (id === PLUGIN_RUNTIMES_MODULE ? resolvedId : null),
121
+ load: (id) => (id === resolvedId ? imports.join('\n') : null),
84
122
  }
85
123
  }
86
124
 
125
+ const { plugins: appPlugins = [] } = await loadAppConfig()
126
+ const APP_VITE_PLUGINS = [
127
+ pluginRuntimes(appPlugins),
128
+ ...appPlugins.flatMap((plugin) => plugin.vite ?? []),
129
+ ]
130
+
87
131
  function viteConfig({ mode, plugins = [], watch = null, sourcemap = false }) {
88
132
  // Vite reads the mode from NODE_ENV: development builds keep each component's file (for errors).
89
133
  process.env.NODE_ENV = mode
@@ -98,7 +142,8 @@ function viteConfig({ mode, plugins = [], watch = null, sourcemap = false }) {
98
142
  compilerOptions: { isCustomElement: (tag) => NATIVE_COMPONENT_TAGS.includes(tag) },
99
143
  },
100
144
  }),
101
- bundleForAndroid(mode),
145
+ ...APP_VITE_PLUGINS,
146
+ finishBundle(mode),
102
147
  ...plugins,
103
148
  ],
104
149
  // Compiled templates and app code import from 'vue': point them to the native renderer.
@@ -213,6 +258,8 @@ async function runApp(platform) {
213
258
  const [command, ...args] = process.argv.slice(2)
214
259
 
215
260
  try {
261
+ if (['dev', 'build', 'run'].includes(command)) linkIosPlugins(frameworkPath(''))
262
+
216
263
  if (command === 'dev') {
217
264
  await dev()
218
265
  } else if (command === 'build') {
@@ -1,13 +1,11 @@
1
- // Maps stack frames of the bundle back to the app's files with the bundle's source map, in the
2
- // engine's format: JavaScriptCore's `name@main.bundle.js:8161:20` becomes `name@src/app/index.vue:22:9`,
3
- // Hermes' `at name (main.bundle.js:8161:20)` becomes `at name (src/app/index.vue:22:9)`. Consecutive
1
+ // Maps stack frames of the bundle back to the app's files with the bundle's source map: Hermes'
2
+ // `at name (main.bundle.js:8161:20)` becomes `at name (src/app/index.vue:22:9)`. Consecutive
4
3
  // frames from outside src/ (Vue, nativue, Hermes' native ones) collapse into one line, so the app's
5
4
  // own frames stand out. Other lines are kept.
6
5
  import { SourceMap } from 'node:module'
7
6
  import { relative, resolve } from 'node:path'
8
7
 
9
8
  // Groups: before the location, the location's line and column, after it.
10
- const JAVASCRIPTCORE_FRAME = /^(.*@)(?:.*main\.bundle\.js):(\d+):(\d+)()$/
11
9
  const HERMES_FRAME = /^(\s*at .* \()(?:.*main\.bundle\.js):(\d+):(\d+)(\))$/
12
10
  const HERMES_NATIVE_FRAME = /^\s*at .* \(native\)$/
13
11
 
@@ -28,7 +26,7 @@ export function createSymbolicator(sourceMapPayload, bundleDirectory) {
28
26
  function symbolicateLine(line) {
29
27
  if (HERMES_NATIVE_FRAME.test(line)) return { text: line, isInternal: true }
30
28
 
31
- const frame = JAVASCRIPTCORE_FRAME.exec(line) ?? HERMES_FRAME.exec(line)
29
+ const frame = HERMES_FRAME.exec(line)
32
30
  if (!frame) return { text: line, isInternal: false }
33
31
 
34
32
  const [, before, lineNumber, columnNumber, after] = frame