@htyf-mp/cli 0.4.4 → 0.4.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htyf-mp/cli",
3
- "version": "0.4.4",
3
+ "version": "0.4.6",
4
4
  "description": "红糖云服app小程序模板-cli",
5
5
  "type": "module",
6
6
  "bin": {
@@ -82,5 +82,5 @@
82
82
  "publishConfig": {
83
83
  "access": "public"
84
84
  },
85
- "gitHead": "9fa75aa969dbaf756146b0f603f32b67b64bedbc"
85
+ "gitHead": "af36922676c23f9998c5e42cecafad16bad2b621"
86
86
  }
@@ -181,10 +181,10 @@ function trimTemplateStrings(strings, ...values) {
181
181
  */
182
182
  export default function injectAppInfoLoader(source) {
183
183
  this.cacheable?.();
184
-
184
+
185
185
  // 将 Buffer 转换为字符串
186
186
  const originalSource = Buffer.isBuffer(source) ? source.toString('utf-8') : source;
187
-
187
+
188
188
  // 注入依赖信息属性
189
189
  return `
190
190
  ${originalSource}
@@ -214,7 +214,7 @@ export class HtyfModulesPlugin {
214
214
  apply(compiler) {
215
215
  const config = this.config;
216
216
 
217
- // 分析第三方依赖并生成报告
217
+ // 分析第三方依赖,校验并在 emit 阶段替换占位符
218
218
  compiler.hooks.emit.tapAsync('AnalyzeThirdPartyModulesPlugin', (compilation, callback) => {
219
219
  try {
220
220
  const { dependencies, nativeModules } = this.collectDependencyInfo(compilation);
@@ -223,9 +223,6 @@ export class HtyfModulesPlugin {
223
223
  const outputPath = compilation.outputOptions.path || compiler.options.output?.path;
224
224
  const manifestPath = this.config.manifest || (outputPath ? path.join(outputPath, 'manifest.json') : null);
225
225
 
226
- // 持久化依赖报告
227
- this.persistDependencyReport(dependencies, manifestPath);
228
-
229
226
  // 筛选出不支持的 native 包
230
227
  const unsupportedPackages = Array.from(nativeModules).filter(
231
228
  pkg => !isWhitelisted(pkg) && !isInSharedConfig(pkg)
@@ -233,9 +230,10 @@ export class HtyfModulesPlugin {
233
230
 
234
231
  // 如果存在不支持的包,抛出错误
235
232
  if (unsupportedPackages.length > 0) {
233
+ this._pendingDependencyReport = null;
236
234
  const errorMessage = formatErrorMessage(unsupportedPackages);
237
235
  console.error(errorMessage);
238
-
236
+
239
237
  const error = new Error(
240
238
  `发现 ${unsupportedPackages.length} 个不支持的第三方 native 组件。\n${errorMessage}`
241
239
  );
@@ -246,9 +244,29 @@ export class HtyfModulesPlugin {
246
244
 
247
245
  // 替换依赖占位符
248
246
  this.replaceDependencyPlaceholders(compilation, dependencies);
247
+
248
+ // 暂存依赖报告,待所有 emit 任务完成后再持久化
249
+ this._pendingDependencyReport = { dependencies, manifestPath };
249
250
  callback();
250
251
  } catch (error) {
252
+ this._pendingDependencyReport = null;
251
253
  callback(error);
254
+ process.exit(1);
255
+ }
256
+ });
257
+
258
+ // 所有 emit 钩子执行完毕且资源写入磁盘后,再持久化依赖报告
259
+ compiler.hooks.afterEmit.tapAsync('AnalyzeThirdPartyModulesPlugin', (_compilation, callback) => {
260
+ try {
261
+ const pending = this._pendingDependencyReport;
262
+ this._pendingDependencyReport = null;
263
+ if (pending) {
264
+ this.persistDependencyReport(pending.dependencies, pending.manifestPath);
265
+ }
266
+ callback();
267
+ } catch (error) {
268
+ callback(error);
269
+ process.exit(1);
252
270
  }
253
271
  });
254
272
 
@@ -277,6 +295,7 @@ export class HtyfModulesPlugin {
277
295
  callback();
278
296
  } catch (error) {
279
297
  callback(error);
298
+ process.exit(1);
280
299
  }
281
300
  });
282
301
  }
@@ -325,7 +344,7 @@ export class HtyfModulesPlugin {
325
344
  };
326
345
 
327
346
  dependencyMap.set(packageName, dependencyInfo);
328
-
347
+
329
348
  if (nativeModule) {
330
349
  nativeModules.add(packageName);
331
350
  }
@@ -359,7 +378,7 @@ export class HtyfModulesPlugin {
359
378
  const packageJsonPath = path.join(nodeModulesPath, '../package.json');
360
379
  if (fse.pathExistsSync(packageJsonPath)) {
361
380
  const packageJson = fse.readJsonSync(packageJsonPath);
362
- const reactNativeVersion = packageJson.dependencies?.['react-native'] ||
381
+ const reactNativeVersion = packageJson.dependencies?.['react-native'] ||
363
382
  packageJson.devDependencies?.['react-native'];
364
383
  if (reactNativeVersion) {
365
384
  const cleanVersion = reactNativeVersion.replace(/^[~^]/, '');
@@ -374,7 +393,7 @@ export class HtyfModulesPlugin {
374
393
  // 静默失败
375
394
  }
376
395
  }
377
-
396
+
378
397
  const reportDir = path.dirname(manifest);
379
398
  fse.ensureDirSync(reportDir);
380
399
  fse.writeJsonSync(
@@ -383,7 +402,7 @@ export class HtyfModulesPlugin {
383
402
  { spaces: 2 }
384
403
  );
385
404
  } catch (error) {
386
- console.warn(`[HtyfModulesPlugin] 写入依赖报告失败: ${error.message}`);
405
+ throw new Error(`[HtyfModulesPlugin] 写入依赖报告失败: ${error.message}`);
387
406
  }
388
407
  }
389
408