@flun/node-mobile-app 3.1.5 → 4.0.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.
- package/CHANGELOG.md +33 -5
- package/README.md +559 -546
- package/bin/cli.js +3 -2
- package/package.json +4 -3
- package/src/commands/build.js +115 -114
- package/src/commands/clean.js +4 -3
- package/src/commands/test.js +160 -109
- package/src/config.js +6 -5
- package/src/copy.js +320 -319
- package/src/fingerprint.js +123 -122
- package/src/generate.js +105 -104
- package/src/install.js +317 -215
- package/src/optimize-node-modules.js +55 -56
- package/src/patcher.js +389 -388
- package/src/platform-ios.js +6 -5
- package/src/utils.js +92 -5
package/src/install.js
CHANGED
|
@@ -1,215 +1,317 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import { run } from './utils.js';
|
|
4
|
-
import { clearVerified, optimizeNodeModules, SELF_HASH } from './optimize-node-modules.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
fs.writeFileSync(
|
|
141
|
-
console.log('
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { run } from './utils.js';
|
|
4
|
+
import { clearVerified, optimizeNodeModules, SELF_HASH } from './optimize-node-modules.js';
|
|
5
|
+
import { createRequire } from 'module';
|
|
6
|
+
import chalk from 'chalk';
|
|
7
|
+
import { resolveSdkPath } from './config.js';
|
|
8
|
+
import { createHash } from 'crypto';
|
|
9
|
+
const
|
|
10
|
+
/**
|
|
11
|
+
* 内部:从 libnode 的 node_version.h 读取运行时 Node 主版本号
|
|
12
|
+
* 读不到直接报错退出(说明 libnode 目录不完整,继续执行必失败)
|
|
13
|
+
*/
|
|
14
|
+
resolveRuntimeMajor = (userProjectDir, targetPlatform) => {
|
|
15
|
+
const sub = targetPlatform === 'ios' ? 'ios' : 'android',
|
|
16
|
+
header = path.join(userProjectDir, 'node_modules', '@flun', 'nodejs-mobile-react-native',
|
|
17
|
+
sub, 'libnode', 'include', 'node', 'node_version.h');
|
|
18
|
+
if (!fs.existsSync(header)) {
|
|
19
|
+
console.error(chalk.red(' [错误] 未找到 node_version.h: ' + header));
|
|
20
|
+
console.error(chalk.red(' 说明 libnode 目录不完整,请重新安装依赖。')), process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
const m = fs.readFileSync(header, 'utf-8').match(/#define\s+NODE_MAJOR_VERSION\s+(\d+)/);
|
|
23
|
+
if (!m) console.error(chalk.red(' [错误] 无法解析 node_version.h 中的 NODE_MAJOR_VERSION')), process.exit(1);
|
|
24
|
+
return m[1];
|
|
25
|
+
},
|
|
26
|
+
/**
|
|
27
|
+
* 内部:判断用户项目的生产依赖里是否含原生模块(binding.gyp)
|
|
28
|
+
* 按用户根 package.json 的 dependencies 键名,去 userProjectDir/node_modules 下逐个查
|
|
29
|
+
* 不用扫 nodejs-project/node_modules:全新构建时它还不存在
|
|
30
|
+
*/
|
|
31
|
+
hasNativeModules = userProjectDir => {
|
|
32
|
+
const pkgPath = path.join(userProjectDir, 'package.json');
|
|
33
|
+
if (!fs.existsSync(pkgPath)) return false;
|
|
34
|
+
let deps = {};
|
|
35
|
+
try {
|
|
36
|
+
deps = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')).dependencies || {};
|
|
37
|
+
} catch { return false; }
|
|
38
|
+
|
|
39
|
+
const nodeModulesDir = path.join(userProjectDir, 'node_modules');
|
|
40
|
+
for (const name of Object.keys(deps))
|
|
41
|
+
if (fs.existsSync(path.join(nodeModulesDir, name, 'binding.gyp'))) return true;
|
|
42
|
+
return false;
|
|
43
|
+
},
|
|
44
|
+
/**
|
|
45
|
+
* 内部:定位 @flun/nodejs-mobile-react-native 插件目录
|
|
46
|
+
*/
|
|
47
|
+
findPluginDir = userProjectDir => {
|
|
48
|
+
const candidates = [path.join(userProjectDir, 'node_modules', '@flun', 'nodejs-mobile-react-native')];
|
|
49
|
+
for (const c of candidates) if (fs.existsSync(path.join(c, 'android', 'build.gradle'))) return c;
|
|
50
|
+
return null;
|
|
51
|
+
},
|
|
52
|
+
ABI_MAP = {
|
|
53
|
+
'armeabi-v7a': { arch: 'arm', prefix: 'armv7a-linux-androideabi' },
|
|
54
|
+
'arm64-v8a': { arch: 'arm64', prefix: 'aarch64-linux-android' },
|
|
55
|
+
'x86_64': { arch: 'x64', prefix: 'x86_64-linux-android' }
|
|
56
|
+
},
|
|
57
|
+
resolveNdkVersion = buildDir => {
|
|
58
|
+
const gradle = path.join(buildDir, 'android', 'build.gradle');
|
|
59
|
+
if (!fs.existsSync(gradle)) console.error(chalk.red(' [错误] 未找到 android/build.gradle: ' + gradle)), process.exit(1);
|
|
60
|
+
const m = fs.readFileSync(gradle, 'utf-8').match(/ndkVersion\s*=\s*"([^"]+)"/);
|
|
61
|
+
if (!m) console.error(chalk.red(' [错误] 无法从 android/build.gradle 解析 ndkVersion')), process.exit(1);
|
|
62
|
+
return m[1];
|
|
63
|
+
},
|
|
64
|
+
resolveCompileSdk = buildDir => {
|
|
65
|
+
const gradle = path.join(buildDir, 'android', 'build.gradle'),
|
|
66
|
+
m = fs.readFileSync(gradle, 'utf-8').match(/minSdkVersion\s*=\s*(\d+)/);
|
|
67
|
+
return Math.max(m ? +m[1] : 24, 24);
|
|
68
|
+
},
|
|
69
|
+
findNativePackages = targetDir => {
|
|
70
|
+
const nm = path.join(targetDir, 'node_modules'), result = [];
|
|
71
|
+
if (!fs.existsSync(nm)) return result;
|
|
72
|
+
for (const entry of fs.readdirSync(nm)) {
|
|
73
|
+
if (entry.startsWith('.')) continue;
|
|
74
|
+
const full = path.join(nm, entry);
|
|
75
|
+
if (!fs.statSync(full).isDirectory()) continue;
|
|
76
|
+
if (entry.startsWith('@')) {
|
|
77
|
+
for (const sub of fs.readdirSync(full)) {
|
|
78
|
+
const pkgDir = path.join(full, sub);
|
|
79
|
+
if (fs.statSync(pkgDir).isDirectory() && fs.existsSync(path.join(pkgDir, 'binding.gyp'))) result.push(entry + '/' + sub);
|
|
80
|
+
}
|
|
81
|
+
} else if (fs.existsSync(path.join(full, 'binding.gyp'))) result.push(entry);
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
},
|
|
85
|
+
compileNativeModules = (targetDir, userProjectDir, config, buildDir, abiFilters) => {
|
|
86
|
+
const sdkPath = resolveSdkPath(config);
|
|
87
|
+
if (!sdkPath) console.error(chalk.red(' [错误] 未找到 Android SDK,无法编译原生模块')), process.exit(1);
|
|
88
|
+
const ndkVersion = resolveNdkVersion(buildDir), compileSdk = resolveCompileSdk(buildDir),
|
|
89
|
+
ndkPath = path.join(sdkPath, 'ndk', ndkVersion);
|
|
90
|
+
if (!fs.existsSync(ndkPath)) console.error(chalk.red(' [错误] 未找到 NDK ' + ndkVersion + ': ' + ndkPath)), process.exit(1);
|
|
91
|
+
const pluginDir = findPluginDir(userProjectDir);
|
|
92
|
+
if (!pluginDir) console.error(chalk.red(' [错误] 未找到 @flun/nodejs-mobile-react-native 插件')), process.exit(1);
|
|
93
|
+
const libnodeDir = path.join(pluginDir, 'android', 'libnode'),
|
|
94
|
+
gypPath = createRequire(path.join(pluginDir, 'package.json')).resolve('@flun/nodejs-mobile-gyp/bin/node-gyp.js'),
|
|
95
|
+
hostTag = { win32: 'windows-x86_64', darwin: 'darwin-x86_64', linux: 'linux-x86_64' }[process.platform],
|
|
96
|
+
hostOs = process.platform === 'darwin' ? 'mac' : 'linux',
|
|
97
|
+
toolSuffix = process.platform === 'win32' ? '.cmd' : '',
|
|
98
|
+
binSuffix = process.platform === 'win32' ? '.exe' : '';
|
|
99
|
+
if (!hostTag) console.error(chalk.red(' [错误] 不支持的操作系统: ' + process.platform)), process.exit(1);
|
|
100
|
+
const toolchain = path.join(ndkPath, 'toolchains', 'llvm', 'prebuilt', hostTag),
|
|
101
|
+
assetsDir = path.dirname(targetDir), gradleProjectDir = path.join(buildDir, 'android');
|
|
102
|
+
const patchScript = path.join(pluginDir, 'scripts', 'patch-package.js');
|
|
103
|
+
if (fs.existsSync(patchScript)) run(process.execPath, [patchScript, path.join(targetDir, 'node_modules')], { cwd: targetDir, inherit: true });
|
|
104
|
+
for (const abi of abiFilters) {
|
|
105
|
+
const map = ABI_MAP[abi];
|
|
106
|
+
if (!map) console.error(chalk.red(' [错误] 不支持的 ABI: ' + abi)), process.exit(1);
|
|
107
|
+
const compilerPrefix = map.prefix + compileSdk,
|
|
108
|
+
env = {
|
|
109
|
+
...process.env,
|
|
110
|
+
npm_config_node_engine: 'v8', npm_config_nodedir: libnodeDir + path.sep,
|
|
111
|
+
npm_config_node_gyp: gypPath, npm_config_arch: map.arch,
|
|
112
|
+
npm_config_platform: 'android', npm_config_format: 'make-android',
|
|
113
|
+
PROJECT_DIR: gradleProjectDir, NODEJS_MOBILE_GYP: gypPath,
|
|
114
|
+
TOOLCHAIN: toolchain,
|
|
115
|
+
AR: path.join(toolchain, 'bin', 'llvm-ar' + binSuffix),
|
|
116
|
+
CC: path.join(toolchain, 'bin', compilerPrefix + '-clang' + toolSuffix),
|
|
117
|
+
CXX: path.join(toolchain, 'bin', compilerPrefix + '-clang++' + toolSuffix),
|
|
118
|
+
LINK: path.join(toolchain, 'bin', compilerPrefix + '-clang++' + toolSuffix),
|
|
119
|
+
RANLIB: path.join(toolchain, 'bin', 'llvm-ranlib' + binSuffix),
|
|
120
|
+
GYP_DEFINES: 'target_arch=' + map.arch + ' v8_target_arch=' + map.arch + ' android_target_arch=' + map.arch +
|
|
121
|
+
' host_os=' + hostOs + ' OS=android android_ndk_path=' + ndkPath.replace(/\\/g, '/'),
|
|
122
|
+
PATH: [path.join(ndkPath, 'prebuilt', hostTag, 'bin'), path.join(toolchain, 'bin'), path.join(targetDir, 'node_modules', '.bin'), process.env.PATH].filter(Boolean).join(path.delimiter)
|
|
123
|
+
};
|
|
124
|
+
console.log(chalk.cyan(' ℹ️ 编译原生模块 (' + abi + ')...'));
|
|
125
|
+
try { run('npm', ['rebuild', '--build-from-source', '--no-audit', '--no-fund'], { cwd: targetDir, env, quiet: true }); }
|
|
126
|
+
catch (e) { console.error(chalk.red(' [错误] 原生模块编译失败 (' + abi + '): ' + e.message)), process.exit(1); }
|
|
127
|
+
const missing = findNativePackages(targetDir).filter(name => {
|
|
128
|
+
const pkgDir = path.join(targetDir, 'node_modules', name),
|
|
129
|
+
prebuildDir = path.join(pkgDir, 'prebuilds', 'android-' + map.arch),
|
|
130
|
+
hasPrebuild = fs.existsSync(prebuildDir) && fs.readdirSync(prebuildDir).some(f => f.endsWith('.node')),
|
|
131
|
+
releaseDir = path.join(pkgDir, 'build', 'Release'),
|
|
132
|
+
hasBuild = fs.existsSync(releaseDir) && fs.readdirSync(releaseDir).some(f => f.endsWith('.node'));
|
|
133
|
+
return !hasPrebuild && !hasBuild;
|
|
134
|
+
});
|
|
135
|
+
if (missing.length > 0) {
|
|
136
|
+
console.error(chalk.red(' [错误] 以下原生模块编译后缺少 .node (' + abi + '):'));
|
|
137
|
+
for (const n of missing) console.error(chalk.red(' - ' + n));
|
|
138
|
+
process.exit(1);
|
|
139
|
+
}
|
|
140
|
+
fs.writeFileSync(path.join(assetsDir, '.native-built-' + abi), new Date().toISOString());
|
|
141
|
+
console.log(chalk.green(' ✓ 原生模块编译完成 (' + abi + ')'));
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
/**
|
|
145
|
+
* 内部:iOS 的 WebView SSL 补丁
|
|
146
|
+
*/
|
|
147
|
+
patchWebViewSslIos = userProjectDir => {
|
|
148
|
+
const file = path.join(userProjectDir, 'node_modules', 'react-native-webview', 'apple', 'RNCWebViewImpl.m');
|
|
149
|
+
if (!fs.existsSync(file)) return;
|
|
150
|
+
|
|
151
|
+
let c = fs.readFileSync(file, 'utf-8');
|
|
152
|
+
if (c.includes('LOCALHOST_SSL_BYPASS_IOS')) return console.log(chalk.green(' ✓ iOS WebView SSL 补丁已应用'));
|
|
153
|
+
|
|
154
|
+
// 在 "NSString* host = nil;" 那一段之后插入 localhost 放行
|
|
155
|
+
const anchor = ' if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodClientCertificate) {',
|
|
156
|
+
idx = c.indexOf(anchor);
|
|
157
|
+
if (idx === -1) return console.warn(chalk.yellow(' ⚠️ RNCWebViewImpl.m 里未找到锚点,跳过 iOS SSL 补丁'));
|
|
158
|
+
|
|
159
|
+
const injected = [
|
|
160
|
+
' // LOCALHOST_SSL_BYPASS_IOS: 对 localhost/127.0.0.1 放行自签名证书(仅本地测试用)',
|
|
161
|
+
' if (host != nil && ([host isEqualToString:@"127.0.0.1"] || [host isEqualToString:@"localhost"])) {',
|
|
162
|
+
' NSURLCredential *cred = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];',
|
|
163
|
+
' completionHandler(NSURLSessionAuthChallengeUseCredential, cred);',
|
|
164
|
+
' return;',
|
|
165
|
+
' }',
|
|
166
|
+
'',
|
|
167
|
+
].join('\n');
|
|
168
|
+
|
|
169
|
+
c = c.substring(0, idx) + injected + c.substring(idx);
|
|
170
|
+
fs.writeFileSync(file, c, 'utf-8');
|
|
171
|
+
console.log(chalk.green(' ✓ 已应用 iOS WebView SSL 补丁(允许 localhost 自签证书)'));
|
|
172
|
+
},
|
|
173
|
+
/**
|
|
174
|
+
* 装用户项目的生产依赖到 <buildDir>/nodejs-assets/nodejs-project/
|
|
175
|
+
* 只装 dependencies,不装 devDependencies
|
|
176
|
+
* 依赖 + 环境未变化且上次构建完成时跳过;否则按需重装 / 仅优化
|
|
177
|
+
* 快照记录在 buildDir/.deps-snapshot.json(与桌面版一致)
|
|
178
|
+
*/
|
|
179
|
+
installUserDeps = async (targetDir, userProjectDir, config, buildDir, targetPlatform) => {
|
|
180
|
+
const assetsDir = path.dirname(targetDir), bnmFile = path.join(assetsDir, 'BUILD_NATIVE_MODULES.txt'),
|
|
181
|
+
nodeModulesDir = path.join(targetDir, 'node_modules'), snapshotPath = path.join(buildDir, '.deps-snapshot.json'),
|
|
182
|
+
forceRebuild = process.env.NODE_MOBILE_FORCE_REBUILD === '1', hasNative = hasNativeModules(userProjectDir),
|
|
183
|
+
optimizeCfg = config.optimize || {}, thisOptimize = optimizeCfg.enabled !== false,
|
|
184
|
+
abiFilters = [...(config.android.abiFilters || [])].sort(),
|
|
185
|
+
runtimeMajor = resolveRuntimeMajor(userProjectDir, targetPlatform);
|
|
186
|
+
|
|
187
|
+
fs.writeFileSync(bnmFile, hasNative ? '1' : '0');
|
|
188
|
+
if (hasNative) console.log(chalk.gray(' ℹ️ 检测到原生模块(binding.gyp)'));
|
|
189
|
+
|
|
190
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(targetDir, 'package.json'), 'utf-8')), deps = pkg.dependencies || {},
|
|
191
|
+
sortedDeps = Object.keys(deps).sort().reduce((acc, k) => { acc[k] = deps[k]; return acc; }, {}),
|
|
192
|
+
installerHash = createHash('sha256').update(fs.readFileSync(new URL(import.meta.url), 'utf-8')).digest('hex').slice(0, 16),
|
|
193
|
+
snapshotData = { deps: sortedDeps, type: pkg.type || '', hasNative, forceRebuild };
|
|
194
|
+
|
|
195
|
+
let shouldInstall = true, shouldOptimize = thisOptimize, reasons = [];
|
|
196
|
+
if (fs.existsSync(snapshotPath)) {
|
|
197
|
+
const old = JSON.parse(fs.readFileSync(snapshotPath, 'utf-8')), verifiedOk = old.verified === true,
|
|
198
|
+
depsSame = JSON.stringify({
|
|
199
|
+
deps: old.deps, type: old.type, hasNative: old.hasNative, forceRebuild: old.forceRebuild
|
|
200
|
+
}) === JSON.stringify(snapshotData),
|
|
201
|
+
envSame = old.platform === targetPlatform && JSON.stringify(old.abiFilters) === JSON.stringify(abiFilters) &&
|
|
202
|
+
old.runtimeMajor === runtimeMajor,
|
|
203
|
+
lastOptimized = old.optimized !== false, selfHashSame = old.optimizeHash === SELF_HASH,
|
|
204
|
+
installerSame = old.installerHash === installerHash,
|
|
205
|
+
nmExists = fs.existsSync(nodeModulesDir);
|
|
206
|
+
if (!depsSame) reasons.push('依赖清单已变化');
|
|
207
|
+
if (!verifiedOk) reasons.push('上次构建未完成');
|
|
208
|
+
if (!envSame) reasons.push('运行环境已变化');
|
|
209
|
+
if (!nmExists) reasons.push('缓存目录缺失');
|
|
210
|
+
if (lastOptimized && !thisOptimize) reasons.push('优化已由开启转为关闭');
|
|
211
|
+
if (thisOptimize && !selfHashSame && lastOptimized) reasons.push('优化程序已更新');
|
|
212
|
+
if (!installerSame) reasons.push('安装器已更新');
|
|
213
|
+
if (reasons.length > 0) {
|
|
214
|
+
shouldInstall = true, shouldOptimize = thisOptimize;
|
|
215
|
+
console.log(chalk.gray(' ℹ️ ' + (thisOptimize ? '重新安装依赖+优化' : '重新安装依赖') + '(原因: ' + reasons.join(', ') + ')'));
|
|
216
|
+
}
|
|
217
|
+
else if (thisOptimize && !lastOptimized)
|
|
218
|
+
shouldInstall = false, shouldOptimize = true, console.log(chalk.gray(' ℹ️ 依赖与环境未变化,仅需优化'));
|
|
219
|
+
else {
|
|
220
|
+
shouldInstall = false, shouldOptimize = false, console.log(chalk.gray(' ✓ 依赖与环境未变化,跳过安装与优化'));
|
|
221
|
+
await clearVerified(buildDir);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
} else console.log(chalk.gray(' ℹ️ ' + (thisOptimize ? '重新安装依赖+优化' : '重新安装依赖') + '(原因: 缓存快照缺失)'));
|
|
225
|
+
|
|
226
|
+
if (shouldInstall) {
|
|
227
|
+
if (fs.existsSync(nodeModulesDir))
|
|
228
|
+
console.log(chalk.cyan(' 清理旧 node_modules...')), fs.rmSync(nodeModulesDir, { recursive: true, force: true });
|
|
229
|
+
|
|
230
|
+
try { fs.rmSync(snapshotPath, { force: true }); } catch { }
|
|
231
|
+
const env = { ...process.env };
|
|
232
|
+
if (hasNative) env.NODEJS_MOBILE_BUILD_NATIVE_MODULES = '1';
|
|
233
|
+
console.log(chalk.cyan(' 安装用户生产依赖(nodejs-assets/nodejs-project)...'));
|
|
234
|
+
run('npm', ['install', '--omit=dev', '--no-audit', '--no-fund'], { cwd: targetDir, env, quiet: true });
|
|
235
|
+
const snap = {
|
|
236
|
+
...snapshotData, platform: targetPlatform, abiFilters, runtimeMajor,
|
|
237
|
+
optimizeHash: SELF_HASH, installerHash, optimized: false, verified: false
|
|
238
|
+
};
|
|
239
|
+
fs.writeFileSync(snapshotPath, JSON.stringify(snap, null, 2));
|
|
240
|
+
console.log(chalk.green(' ✓ 用户依赖安装完成'));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (hasNative && targetPlatform === 'android' && shouldInstall) compileNativeModules(targetDir, userProjectDir, config, buildDir, abiFilters);
|
|
244
|
+
|
|
245
|
+
if (shouldOptimize) {
|
|
246
|
+
console.log(chalk.cyan(' [信息] 正在执行优化...'));
|
|
247
|
+
try {
|
|
248
|
+
await optimizeNodeModules(targetDir, {
|
|
249
|
+
targetPlatform, abiFilters, runtimeMajor, exclude: optimizeCfg.exclude || [], snapshotDir: buildDir
|
|
250
|
+
});
|
|
251
|
+
} catch (err) {
|
|
252
|
+
console.error(chalk.red(' [错误] node_modules 优化失败:' + err.message));
|
|
253
|
+
console.error(chalk.red(' [提示] 可在 mobileAppConfig.js 中设置 optimize: { enabled: false } 关闭优化,'));
|
|
254
|
+
console.error(chalk.red(' 或用 optimize: { exclude: ["包名"] } 排除问题包。')), process.exit(1);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
/**
|
|
259
|
+
* 修改 @flun/nodejs-mobile-react-native 插件 build.gradle 的 abiFilters
|
|
260
|
+
*/
|
|
261
|
+
patchNodejsMobilePlugin = (userProjectDir, config) => {
|
|
262
|
+
const pluginDir = findPluginDir(userProjectDir);
|
|
263
|
+
if (!pluginDir) return console.warn(chalk.yellow(' ⚠️ 未找到 @flun/nodejs-mobile-react-native 插件,跳过 patch'));
|
|
264
|
+
|
|
265
|
+
const file = path.join(pluginDir, 'android', 'build.gradle'),
|
|
266
|
+
abiList = config.android.abiFilters.map((x) => `"${x}"`).join(', ');
|
|
267
|
+
|
|
268
|
+
let c = fs.readFileSync(file, 'utf-8');
|
|
269
|
+
const before = c;
|
|
270
|
+
c = c.replace(/abiFilters\s*=\s*\[[^\]]*\]/, `abiFilters = [${abiList}]`);
|
|
271
|
+
|
|
272
|
+
if (c !== before) fs.writeFileSync(file, c, 'utf-8'), console.log(chalk.green(` ✓ 已设置插件 abiFilters = [${abiList}]`));
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* 让 react-native-webview 对 localhost 自签名证书放行
|
|
277
|
+
* Android: patch RNCWebViewClient.java
|
|
278
|
+
* iOS: patch RNCWebViewImpl.m
|
|
279
|
+
* 仅在 config.webview.allowSelfSignedCert === true 时应用
|
|
280
|
+
* 每次 test/build 都检查(npm 重装 webview 后会覆盖补丁)
|
|
281
|
+
*/
|
|
282
|
+
patchWebViewSsl = (userProjectDir, config) => {
|
|
283
|
+
if (!config.webview || config.webview.allowSelfSignedCert !== true) return;
|
|
284
|
+
|
|
285
|
+
const file = path.join(
|
|
286
|
+
userProjectDir, 'node_modules', 'react-native-webview', 'android', 'src', 'main', 'java', 'com',
|
|
287
|
+
'reactnativecommunity', 'webview', 'RNCWebViewClient.java'
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
if (fs.existsSync(file)) {
|
|
291
|
+
let c = fs.readFileSync(file, 'utf-8');
|
|
292
|
+
if (c.includes('LOCALHOST_SSL_BYPASS')) console.log(chalk.green(' ✓ Android WebView SSL 补丁已应用'));
|
|
293
|
+
else {
|
|
294
|
+
const old = 'handler.cancel();', methodIndex = c.indexOf('onReceivedSslError');
|
|
295
|
+
if (methodIndex !== -1) {
|
|
296
|
+
const cancelIndex = c.indexOf(old, methodIndex);
|
|
297
|
+
if (cancelIndex !== -1) {
|
|
298
|
+
const newCode = [
|
|
299
|
+
'// LOCALHOST_SSL_BYPASS: 对 localhost/127.0.0.1 放行自签名证书(仅本地测试用)',
|
|
300
|
+
' if (failingUrl != null',
|
|
301
|
+
' && (failingUrl.startsWith("https://127.0.0.1") || failingUrl.startsWith("https://localhost"))) {',
|
|
302
|
+
' handler.proceed();',
|
|
303
|
+
' return;',
|
|
304
|
+
' }',
|
|
305
|
+
' handler.cancel();',
|
|
306
|
+
].join('\n');
|
|
307
|
+
c = c.substring(0, cancelIndex) + newCode + c.substring(cancelIndex + old.length);
|
|
308
|
+
fs.writeFileSync(file, c, 'utf-8');
|
|
309
|
+
console.log(chalk.green(' ✓ 已应用 Android WebView SSL 补丁(允许 localhost 自签证书)'));
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
patchWebViewSslIos(userProjectDir); // iOS 分支
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
export { installUserDeps, patchNodejsMobilePlugin, patchWebViewSsl };
|