mm_machine 2.2.2 → 2.2.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.
Files changed (2) hide show
  1. package/item.js +90 -32
  2. package/package.json +1 -1
package/item.js CHANGED
@@ -17,22 +17,64 @@ if (!$.tpl) {
17
17
  }
18
18
 
19
19
  /**
20
- * 增强require函数,支持热更新
21
- * @param {string} file 文件路径
22
- * @param {Function} func 回调函数
23
- * @returns {object} 加载的模块
20
+ * 增强require函数,专门用于脚本文件(.js)热重载
21
+ * 当脚本文件内容发生变化时,自动重新加载并触发回调
22
+ * @param {string} file 脚本文件路径(.js)
23
+ * @param {Function} func 热更新回调函数,文件变化时触发
24
+ * @returns {object} 加载的模块对象
25
+ * @throws {TypeError} 文件路径不是字符串或回调不是函数时抛出
26
+ * @example
27
+ * // 加载并监听脚本文件
28
+ * const module = $.require('./module.js', (newModule) => {
29
+ * console.log('模块已重新加载');
30
+ * });
24
31
  */
25
32
  $.require = function (file, func) {
33
+ // 参数校验
34
+ if (typeof file !== 'string') {
35
+ throw new TypeError('文件路径必须是字符串');
36
+ }
37
+
38
+ if (func && typeof func !== 'function') {
39
+ throw new TypeError('回调函数必须是函数');
40
+ }
41
+
42
+ // 确保文件是.js文件
43
+ if (!file.endsWith('.js')) {
44
+ throw new TypeError('$.require 只能用于.js脚本文件');
45
+ }
46
+
26
47
  return $.mod.load(file, func);
27
48
  };
28
49
 
29
50
  /**
30
- * 增强JSON加载函数,支持热更新
31
- * @param {string} file 文件路径
32
- * @param {Function} func 回调函数
51
+ * 增强JSON加载函数,专门用于配置文件(.json)热重载
52
+ * 当配置文件内容发生变化时,自动重新加载并触发回调
53
+ * @param {string} file 配置文件路径(.json)
54
+ * @param {Function} func 热更新回调函数,文件变化时触发
33
55
  * @returns {object} 解析的JSON对象
56
+ * @throws {TypeError} 文件路径不是字符串或回调不是函数时抛出
57
+ * @example
58
+ * // 加载并监听配置文件
59
+ * const config = $.loadJson('./config.json', (newConfig) => {
60
+ * console.log('配置已更新');
61
+ * });
34
62
  */
35
63
  $.loadJson = function (file, func) {
64
+ // 参数校验
65
+ if (typeof file !== 'string') {
66
+ throw new TypeError('文件路径必须是字符串');
67
+ }
68
+
69
+ if (func && typeof func !== 'function') {
70
+ throw new TypeError('回调函数必须是函数');
71
+ }
72
+
73
+ // 确保文件是.json文件
74
+ if (!file.endsWith('.json')) {
75
+ throw new TypeError('$.loadJson 只能用于.json配置文件');
76
+ }
77
+
36
78
  return $.mod.load(file, func);
37
79
  };
38
80
 
@@ -208,16 +250,16 @@ Item.prototype._remove = function (module) {
208
250
  * @param {string} file 脚本文件路径
209
251
  */
210
252
  Item.prototype.unloadScript = function (file) {
211
- let target_file = file;
212
- if (!target_file) {
253
+ let filename = file;
254
+ if (!filename) {
213
255
  let main = this.config.main;
214
256
  if (main) {
215
- target_file = main.fullname(this.dir);
257
+ filename = main.fullname(this.dir);
216
258
  }
217
259
  }
218
260
 
219
- if (target_file) {
220
- this._remove(target_file);
261
+ if (filename) {
262
+ this._remove(filename);
221
263
  }
222
264
 
223
265
  this.complete = false;
@@ -225,10 +267,11 @@ Item.prototype.unloadScript = function (file) {
225
267
 
226
268
  /**
227
269
  * 重新加载脚本
270
+ * @returns {object | null} 返回加载的模块对象
228
271
  */
229
272
  Item.prototype.reloadScript = function () {
230
273
  this.unloadScript();
231
- this.loadScript();
274
+ return this.loadScript();
232
275
  };
233
276
 
234
277
  /**
@@ -257,18 +300,15 @@ Item.prototype.unloadAfter = async function (remove) {
257
300
  * @returns {object | null} 返回加载的模块对象
258
301
  */
259
302
  Item.prototype.loadScript = function (file, name = '') {
260
- let target_file = this._getScriptFile(file);
261
- if (!target_file) return null;
303
+ let filename = this._getScriptFile(file);
304
+ if (!filename) return null;
262
305
 
263
- let target_name = this._getScriptName(name);
306
+ let func_name = this._getScriptName(name);
264
307
 
265
308
  try {
266
- let cs = this._loadScript(target_file, target_name);
267
- this._setMainMethod(cs, target_name);
268
- return cs;
309
+ this._loadScript(filename, func_name);
269
310
  } catch (err) {
270
311
  this.log('error', `加载脚本失败: `, err);
271
- return null;
272
312
  }
273
313
  };
274
314
 
@@ -283,12 +323,12 @@ Item.prototype._getScriptFile = function (file) {
283
323
  let main = this.config.main;
284
324
  if (!main) return null;
285
325
 
286
- let target_file = main.fullname(this.dir);
287
- if (!target_file.hasFile()) {
288
- this.newScript(target_file);
326
+ let filename = main.fullname(this.dir);
327
+ if (!filename.hasFile()) {
328
+ this.newScript(filename);
289
329
  }
290
330
 
291
- return target_file;
331
+ return filename;
292
332
  };
293
333
 
294
334
  /**
@@ -301,16 +341,27 @@ Item.prototype._getScriptName = function (name) {
301
341
  };
302
342
 
303
343
  /**
304
- * 加载脚本模块
305
- * @param {string} file 文件路径
306
- * @param {string} name 函数名
307
- * @returns {object | null} 模块对象
344
+ * 加载脚本文件,支持热重载模式
345
+ * 在开发模式(mode=3或4)下使用 $.require 进行热重载
346
+ * 在生产模式下使用标准的 require 加载
347
+ * @param {string} file 脚本文件路径
348
+ * @param {string} func_name 函数名
349
+ * @returns {object | null} 加载的模块对象
308
350
  */
309
- Item.prototype._loadScript = function (file, name) {
351
+ Item.prototype._loadScript = function (file, func_name) {
310
352
  if (this.mode === 3 || this.mode === 4) {
311
- return $.require(file, this._handleHotReload.bind(this, name));
353
+ // 开发模式:使用 $.require 进行热重载,绑定热更新回调
354
+ var cs = $.require(file, (mod) => {
355
+ this._setMainMethod(mod, func_name);
356
+ });
357
+ this._setMainMethod(cs, func_name);
358
+ return cs;
359
+ }
360
+ else {
361
+ var cs = require(file);
362
+ this._setMainMethod(cs, func_name);
363
+ return cs;
312
364
  }
313
- return require(file);
314
365
  };
315
366
 
316
367
  /**
@@ -351,6 +402,13 @@ Item.prototype.newConfig = function (file) {
351
402
  this._createConfigFile(file, this.config, this.dir_base);
352
403
  };
353
404
 
405
+ /**
406
+ * 创建配置文件
407
+ * @param {string} file 目标文件路径
408
+ * @param {object} model 配置模型对象
409
+ * @param {string} tpl_dir 模板文件路径
410
+ * @returns {string} 配置文件内容
411
+ */
354
412
  Item.prototype._createConfigFile = function (file, model = {}, tpl_dir = '') {
355
413
  var tpl = this.getConfigTpl(tpl_dir);
356
414
  var content = $.tpl.render(tpl, model);
@@ -534,7 +592,7 @@ Item.prototype._removeConfigFile = function (file, name) {
534
592
  * @param {string} name 配置名称
535
593
  * @returns {string | null} 错误消息
536
594
  */
537
- Item.prototype._removeArrayItem = function (file, config, name) {
595
+ Item.prototype._removeArray = function (file, config, name) {
538
596
  let index = config.findIndex((item) => item.name === name);
539
597
  if (index === -1) return null;
540
598
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "description": "A flexible Node.js plugin mechanism system for dynamic loading, management and execution of modules. Supports hot reload, lifecycle management, and modern JavaScript features.",
5
5
  "main": "index.js",
6
6
  "scripts": {