@flun/node-mobile-app 3.1.3 → 3.1.5

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.
package/CHANGELOG.md CHANGED
@@ -1,12 +1,18 @@
1
- # 变更日志
2
- ## [3.1.3] - 2026-10-15 23:10
3
- ### 修复
4
- - **删除 `scripts` 字段的范围扩大**:原仅对被合并的包删除 `package.json` 的 `scripts`;跳过的包(如原生模块、JS 较少的包)仍保留,其安装期脚本会被 Gradle `npm rebuild` 触发,若脚本文件已被清理则报 `Cannot find module`、构建失败;现改为**对所有非原生包删除**(原生模块包保留,其 scripts 是编译必需);
5
-
6
- ## [3.1.2] - 2026-10-15 21:05
7
- ### 修复
8
- - 修复了 "src\optimize-node-modules.js" 中 prunePlatform 函数一些边界问题;
9
-
10
- ## [3.1.1] - 2026-10-15 20:29
11
- ### 更新
12
- - 同步更新了一些依赖包;
1
+ # 变更日志
2
+ ## [3.1.5] - 2026-10-18 10:04
3
+ ### 修复
4
+ - **修复原生模块编译时 `node-addon-api` 的 gyp / 头文件被误删**:`pruneDevFiles` 原保护条件为 `COMPILE_EXTS && inNativePkg`,而 `node-addon-api` 自身没有 `binding.gyp`、不在原生模块包列表内,其 `node_addon_api.gyp` 与头文件被当作开发文件删除,导致依赖它的原生模块(如 `bcrypt`)在 Gradle `npm rebuild` 时报 `../node-addon-api/node_addon_api.gyp not found`、构建中断;现新增 `isSkippedPkg`,保护条件改为 `COMPILE_EXTS && (inNativePkg || isSkippedPkg)`,仅保留 `SKIP_PKGS` 包内的编译文件(`.gyp`/`.gypi`/`.h`/`.c` 等),`.md`/`.map`/`.ts` 等仍照常清理,不扩大保留范围;
5
+
6
+ ### 变更
7
+ - **阶段 4 不再清理 `.d.ts`**:`.d.ts` 已由阶段 6 的 `TS_RE`(`/\.(ts|cts|mts)$/`)覆盖,阶段 4 重复清理属冗余;移除后 `.d.ts` 统一由阶段 6 处理,清理职责不重叠;
8
+
9
+ ## [3.1.4] - 2026-10-17 11:32
10
+ ### 修复
11
+ - **修复 `react-native bundle` 报 `Cannot resolve @react-native/metro-config`**:个别场景下用户根 `node_modules` 中缺失该包(如历史安装残留、依赖布局异常),RN CLI 从其自身位置向上解析不到,导致 `test` / `build` 的 bundle 阶段中断;现由 `copyTemplate` 调用新增的 `linkNestedDeps`:遍历主包 `node_modules` 顶层全部条目(含 `@scope/` 子项,不列举包名),对从用户根解析不到的条目,在用户根 `node_modules` 下建 Junction(Windows)/ symlink(macOS/Linux)指向主包物理路径;用户根已能解析的条目跳过、不改 `package.json` / `package-lock.json`、建链接失败即中断;
12
+
13
+ ### 变更
14
+ - **Android 模板仓库增加阿里云镜像兜底**:`settings.gradle` 的 `pluginManagement.repositories` 与 `build.gradle` 的 `buildscript.repositories` 在 `google()` / `mavenCentral()` 之前插入阿里云镜像(gradle-plugin / google / public),改善国内网络拉取 `react-android`、`hermes-android` 等 AAR 时卡在官方源的问题;镜像在前、官方源在后,海外网络镜像不命中会自动回退官方源,仅多一次往返,不影响功能;不写死镜像为唯一源,镜像故障时仍可由官方源兜住;
15
+
16
+ ## [3.1.3] - 2026-10-15 23:10
17
+ ### 修复
18
+ - **删除 `scripts` 字段的范围扩大**:原仅对被合并的包删除 `package.json` 的 `scripts`;跳过的包(如原生模块、JS 较少的包)仍保留,其安装期脚本会被 Gradle `npm rebuild` 触发,若脚本文件已被清理则报 `Cannot find module`、构建失败;现改为**对所有非原生包删除**(原生模块包保留,其 scripts 是编译必需);
package/README.md CHANGED
@@ -124,8 +124,9 @@
124
124
  ```json
125
125
  {
126
126
  "allowScripts": {
127
- "@flun/nodejs-mobile-react-native": true,
128
- "@flun/node-mobile-app": true
127
+ "esbuild": true,
128
+ "@flun/node-mobile-app": true,
129
+ "@flun/nodejs-mobile-react-native":true
129
130
  }
130
131
  }
131
132
  ```
@@ -169,7 +170,7 @@ npx node-mobile-app build --release
169
170
  ### 常用选项
170
171
 
171
172
  | 选项 | 用途 |
172
- | --------------- | --------------------------------------------- |
173
+ | --------------- | ---------------------------------------------- |
173
174
  | `--ios` | 目标平台为 iOS(默认 Android) |
174
175
  | `--release` | 打 release 包(仅 `build`) |
175
176
  | `--device <id>` | 指定设备(`test`) |
@@ -343,10 +344,10 @@ signing: {
343
344
 
344
345
  构建时自动优化 `node_modules`(默认开启,无需配置),减少打包文件数与体积、加速启动。
345
346
 
346
- | 字段 | 类型 | 默认 | 说明 |
347
- | --------- | ---------- | ------ | ------------------------------------------------------------- |
348
- | `enabled` | `boolean` | `true` | 是否启用优化 |
349
- | `exclude` | `string[]` | `[]` | 排除优化时异常或不想优化的包(写 `package.json` 的 `name`) |
347
+ | 字段 | 类型 | 默认 | 说明 |
348
+ | --------- | ---------- | ------ | ----------------------------------------------------------- |
349
+ | `enabled` | `boolean` | `true` | 是否启用优化 |
350
+ | `exclude` | `string[]` | `[]` | 排除优化时异常或不想优化的包(写 `package.json` 的 `name`) |
350
351
 
351
352
  **六阶段流程**:
352
353
 
@@ -476,6 +477,16 @@ adb logcat | grep -E "NODEJS-MOBILE|ReactNativeJS"
476
477
  - 请检查 你的 'package.json' 中 "allowScripts" 字段
477
478
 
478
479
 
480
+ ### Q:Android 构建卡在拉取依赖(`react-android` / `hermes-android` 等)?
481
+
482
+ Android 模板的 `settings.gradle` 与 `build.gradle` 已在 `google()` / `mavenCentral()` 之前插入阿里云镜像,国内网络优先走镜像;海外网络镜像不命中会自动回退官方源,不影响功能。
483
+
484
+ 若仍卡住:
485
+
486
+ - 检查镜像可达:浏览器打开 `https://maven.aliyun.com/repository/public` 是否正常;
487
+ - 检查本机 `~/.gradle/init.gradle` 是否有与仓库顺序冲突的配置(如强制替换仓库),有则临时移除以排除干扰;
488
+ - 停掉残留 Daemon 后重试:在 `node-mobile-app-build/android/` 下执行 `gradlew --stop`,再重跑 `test` / `build`。
489
+
479
490
  ### Q:Express 版本怎么选?
480
491
 
481
492
  - **Android**:内置完整 ICU(full-icu)→ 可以用 Express 5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flun/node-mobile-app",
3
- "version": "3.1.3",
3
+ "version": "3.1.5",
4
4
  "description": "将 Node.js 项目一键打包为 Android / iOS 移动应用,运行时由 @flun/nodejs-mobile-react-native 提供,无需修改你的 Node 代码;",
5
5
  "author": "flun <open@flun.top>",
6
6
  "keywords": [
package/src/copy.js CHANGED
@@ -4,6 +4,7 @@ import {
4
4
  ensureDir, removeDir, readJson, isExcluded, copyFileIfChanged, writeIfChanged, buildEslintConfigSource
5
5
  } from './utils.js';
6
6
  import { resolveSdkPath } from './config.js';
7
+ import { createRequire } from 'module';
7
8
 
8
9
  const BUILD_DIR_NAME = 'node-mobile-app-build',
9
10
  TEMPLATE_EXCLUDE = [
@@ -94,6 +95,54 @@ const BUILD_DIR_NAME = 'node-mobile-app-build',
94
95
  );
95
96
  }
96
97
  },
98
+ /**
99
+ * 把主包 node_modules 里被 npm 嵌套(未提升到用户根)的依赖,按需链接到用户根 node_modules 下,
100
+ * 让 RN CLI / Metro 从用户根也能解析到它们。
101
+ * - 判据:从用户根视角解析不到,才建链接;能解析则跳过;
102
+ * - 范围:遍历主包 node_modules 顶层全部条目(含 @scope/ 下每个子项),不列举任何包名;
103
+ * - 落点:只在用户根 node_modules 缺失条目时建 symlink,不覆盖已有内容、不碰 package.json / package-lock.json;
104
+ * - 建链接失败:中断报错,不静默。
105
+ */
106
+ linkNestedDeps = (pkgRoot, buildDir) => {
107
+ const pkgNm = path.join(pkgRoot, 'node_modules');
108
+ if (!fs.existsSync(pkgNm)) return;
109
+
110
+ const userProjectDir = path.dirname(buildDir),
111
+ userNm = path.join(userProjectDir, 'node_modules'),
112
+ req = createRequire(path.join(userProjectDir, 'index.js')),
113
+ candidates = [];
114
+ for (const entry of fs.readdirSync(pkgNm, { withFileTypes: true })) {
115
+ if (!entry.isDirectory() || entry.name.startsWith('.')) continue;
116
+ if (entry.name.startsWith('@')) {
117
+ const scopeDir = path.join(pkgNm, entry.name);
118
+ for (const sub of fs.readdirSync(scopeDir, { withFileTypes: true }))
119
+ if (sub.isDirectory()) candidates.push(entry.name + '/' + sub.name);
120
+ } else candidates.push(entry.name);
121
+ }
122
+
123
+ let linked = 0;
124
+ for (const name of candidates) {
125
+ try { req.resolve(name); continue; } catch { /* 解析不到,进入兜底 */ }
126
+ const src = path.join(pkgNm, name);
127
+ if (!fs.existsSync(src)) continue;
128
+ const realSrc = fs.realpathSync(src), dst = path.join(userNm, name);
129
+ let occupied = false;
130
+ try { fs.lstatSync(dst); occupied = true; } catch { /* 不存在 */ }
131
+ if (occupied) continue;
132
+ ensureDir(path.dirname(dst));
133
+ try {
134
+ fs.symlinkSync(realSrc, dst, 'junction');
135
+ linked++;
136
+ } catch (err) {
137
+ throw new Error(
138
+ '建立兜底链接失败:' + dst + ' -> ' + realSrc + '\n' +
139
+ ' 原因:' + err.message + '\n' +
140
+ ' 请检查 node_modules 是否被其它程序占用后重试'
141
+ );
142
+ }
143
+ }
144
+ if (linked > 0) console.log(' ✓ 已兜底链接 ' + linked + ' 个嵌套依赖到用户根 node_modules');
145
+ },
97
146
  /**
98
147
  * 把 CLI 包里的 template/ 逐文件覆盖到 build 目录
99
148
  * - 只覆盖模板里存在的文件,不删除 buildDir 里已有的其他内容(保留 Gradle 增量缓存)
@@ -110,6 +159,7 @@ const BUILD_DIR_NAME = 'node-mobile-app-build',
110
159
  copyDirOverwrite(templateDir, buildDir, TEMPLATE_EXCLUDE, buildDir, keepSet);
111
160
  fs.writeFileSync(path.join(buildDir, 'eslint.config.js'), buildEslintConfigSource(), 'utf-8');
112
161
  syncTemplateDependencies(pkgRoot, buildDir);
162
+ linkNestedDeps(pkgRoot, buildDir);
113
163
  console.log(' ✓ 模板已更新到', BUILD_DIR_NAME + '/');
114
164
  if (keepSet.size > 0) console.log(' 保留文件:', Array.from(keepSet).join(', '));
115
165
  },
@@ -15,6 +15,10 @@ const require = createRequire(import.meta.url), subpath = (...parts) => './' + p
15
15
  IOS_ARCHES = ['arm64', 'x64'], INDEX_FILES = ['index.js', 'index.cjs', 'index.mjs'],
16
16
  // ==================== 通用工具 ====================
17
17
  toPosix = p => p.replace(POSIX_RE, '/'),
18
+ isSkippedPkg = rel => {
19
+ const seg = rel.split('/'), name = seg[0].startsWith('@') ? seg[0] + '/' + (seg[1] || '') : seg[0];
20
+ return SKIP_PKGS.has(name) || SKIP_PKGS.has(seg[0]);
21
+ },
18
22
  pickCond = (v, prefer) => {
19
23
  if (typeof v === 'string') return v;
20
24
  if (v && typeof v === 'object') {
@@ -500,7 +504,7 @@ const require = createRequire(import.meta.url), subpath = (...parts) => './' + p
500
504
  /(^|\/)\.editorconfig$/, /(^|\/)\.prettierignore$/, /(^|\/)\.eslintignore$/,
501
505
  /(^|\/)\.dockerignore$/, /(^|\/)\.gitignore$/, /(^|\/)\.gitattributes$/, /(^|\/)\.npmignore$/,
502
506
  /\.(md|markdown)$/i, /\.(gyp|gypi)$/i, /\.(yml|yaml)$/i, /\.(h|c)$/,
503
- /\.d\.ts$/, /\.map$/, /metafile-.*\.json$/,
507
+ /\.map$/, /metafile-.*\.json$/,
504
508
  /(^|\/)\.eslintrc(\.(json|yml|js|cjs))?$/, /(^|\/)\.babelrc(\.(json|js))?$/,
505
509
  /(^|\/)package-lock\.json$/, /(^|\/)\.package-lock\.json$/, /(^|\/)yarn\.lock$/,
506
510
  /(^|\/)package-support\.json$/,
@@ -519,7 +523,7 @@ const require = createRequire(import.meta.url), subpath = (...parts) => './' + p
519
523
  walk(full);
520
524
  } else {
521
525
  const rel = toPosix(path.relative(nmDir, full));
522
- if (PATTERNS.some(re => re.test(rel)) && !(COMPILE_EXTS.test(f.name) && inNativePkg(full)))
526
+ if (PATTERNS.some(re => re.test(rel)) && !(COMPILE_EXTS.test(f.name) && (inNativePkg(full) || isSkippedPkg(rel))))
523
527
  try { fs.unlinkSync(full), deleted++; } catch { }
524
528
  }
525
529
  }
@@ -699,4 +703,4 @@ const SELF_HASH = (() => {
699
703
  } catch { return 'unknown'; }
700
704
  })();
701
705
 
702
- export { clearVerified, markVerified, optimizeNodeModules, SELF_HASH };
706
+ export { clearVerified, markVerified, optimizeNodeModules, SELF_HASH };
@@ -8,6 +8,8 @@ buildscript {
8
8
  kotlinVersion = "2.2.0"
9
9
  }
10
10
  repositories {
11
+ maven { url 'https://maven.aliyun.com/repository/google' }
12
+ maven { url 'https://maven.aliyun.com/repository/public' }
11
13
  google()
12
14
  mavenCentral()
13
15
  }
@@ -1,4 +1,13 @@
1
- pluginManagement { includeBuild("../../node_modules/@react-native/gradle-plugin") }
1
+ pluginManagement {
2
+ repositories {
3
+ maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
4
+ maven { url 'https://maven.aliyun.com/repository/google' }
5
+ maven { url 'https://maven.aliyun.com/repository/public' }
6
+ google()
7
+ mavenCentral()
8
+ }
9
+ includeBuild("../../node_modules/@react-native/gradle-plugin")
10
+ }
2
11
  plugins { id("com.facebook.react.settings") }
3
12
  extensions.configure(com.facebook.react.ReactSettingsExtension){ ex -> ex.autolinkLibrariesFromCommand() }
4
13
  rootProject.name = 'MyApp'