mm_machine 3.4.0 → 3.4.3

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/lib/drive.js CHANGED
@@ -3,10 +3,15 @@ const { conf } = require('mm_config');
3
3
  const { Mod } = require('./mod');
4
4
 
5
5
  const MODE = {
6
+ // 生产模式
6
7
  PRODUCTION: 1,
8
+ // 热更新配置模式
7
9
  HOT_CONFIG: 2,
10
+ // 热更新脚本模式
8
11
  HOT_RELOAD: 3,
12
+ // 热更新配置+脚本模式
9
13
  HOT_BOTH: 4,
14
+ // 始终重载模式
10
15
  RELOAD_ALWAYS: 5
11
16
  };
12
17
 
@@ -122,12 +127,8 @@ Drive.prototype.loadConfig = function (file) {
122
127
  * @returns {object} 配置项
123
128
  */
124
129
  Drive.prototype.setConfig = function (config) {
130
+ var old_script = this.script_file;
125
131
  if (config) {
126
- // 如果main改变,重置加载状态
127
- if (config.main !== this.config.main) {
128
- this.isLoaded(false);
129
- }
130
-
131
132
  if (this.config_file) {
132
133
  // 合并配置并应用配置处理
133
134
  this.config = conf(
@@ -141,10 +142,14 @@ Drive.prototype.setConfig = function (config) {
141
142
  Object.assign(this.config, config);
142
143
  }
143
144
  }
144
- let file = this.config.main;
145
+ var file = this.config.main;
145
146
  if (file) {
146
147
  this.script_file = file.fullname(this.getDir());
147
148
  }
149
+ // 用 script_file 字符串比较,避免对象引用比较导致误判
150
+ if (old_script !== this.script_file) {
151
+ this.isLoaded(false);
152
+ }
148
153
  this._preset();
149
154
  this._initOptions();
150
155
  return this.config;
@@ -317,7 +322,7 @@ Drive.prototype._remove = function () {
317
322
  if (!file) return;
318
323
 
319
324
  try {
320
- if (this.mode >= MODE.HOT_RELOAD) {
325
+ if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
321
326
  $.mod.unload(file);
322
327
  } else {
323
328
  // 移除模块缓存
@@ -668,11 +673,12 @@ Drive.prototype._findConfigByName = function (config, name) {
668
673
  * @private
669
674
  */
670
675
  Drive.prototype._handleHotReload = function (config) {
671
- let old_config = { ...this.config };
676
+ var old_script = this.script_file;
677
+ var old_config = { ...this.config };
672
678
  this.setConfig(config);
673
679
 
674
680
  // 调用 _onConfigChange 钩子(如果脚本中定义了)
675
- let on_config_change = this.getMethod('_onConfigChange');
681
+ var on_config_change = this.getMethod('_onConfigChange');
676
682
  if (typeof on_config_change === 'function') {
677
683
  on_config_change.call(this, old_config, this.config);
678
684
  }
@@ -680,16 +686,20 @@ Drive.prototype._handleHotReload = function (config) {
680
686
  // 触发 config:change 事件
681
687
  this.emit('config:change', old_config, this.config);
682
688
 
683
- this._reloadIfNeeded();
689
+ this._reloadIfNeeded(old_script);
684
690
  return config;
685
691
  };
686
692
 
687
693
  /**
688
694
  * 根据模式决定是否重新加载脚本
695
+ * @param {string} old_script 旧的脚本文件路径
689
696
  */
690
- Drive.prototype._reloadIfNeeded = function () {
691
- if (this.mode >= MODE.HOT_RELOAD) {
692
- this.reloadScript();
697
+ Drive.prototype._reloadIfNeeded = function (old_script) {
698
+ if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
699
+ // 只在 main 发生变化时才重载脚本
700
+ if (!old_script || old_script !== this.script_file) {
701
+ this.reloadScript();
702
+ }
693
703
  }
694
704
  };
695
705
 
@@ -809,7 +819,7 @@ Drive.prototype.loadBefore = function () {
809
819
  /**
810
820
  * 加载处理
811
821
  */
812
- Drive.prototype._loadCore = function () { };
822
+ Drive.prototype._loadCore = function () {};
813
823
 
814
824
  /**
815
825
  * 保存配置
package/lib/manager.js CHANGED
@@ -82,8 +82,16 @@ class Manager extends Mod {
82
82
  * @type {string}
83
83
  */
84
84
  scope: $.val && $.val.scope ? $.val.scope + '' : 'server',
85
- // 事件触发器
86
- trigger_key: []
85
+ /**
86
+ * 最大搜索深度 -1表示不限制深度
87
+ * @type {number}
88
+ */
89
+ max_depth: 3,
90
+ /**
91
+ * 忽略目录数组
92
+ * @type {Array}
93
+ */
94
+ ignore: ['node_modules', '.git']
87
95
  };
88
96
 
89
97
  /**
@@ -102,7 +110,7 @@ class Manager extends Mod {
102
110
  parent
103
111
  );
104
112
 
105
- this.scanner = new Scanner();
113
+ this.scanner = new Scanner(this.config.ignore, this.config.max_depth);
106
114
  // 模块
107
115
  this.mods = mods;
108
116
  // 不使用注册表时,用内部缓存记录模块条目信息
package/lib/scanner.js CHANGED
@@ -1,3 +1,5 @@
1
+ require('mm_expand');
2
+
1
3
  /**
2
4
  * 扫描类
3
5
  * 负责按目录或文件名模式检索文件
@@ -5,15 +7,10 @@
5
7
  class Scanner {
6
8
  /**
7
9
  * 构造函数
8
- * @param {object} config 配置项
9
10
  */
10
- constructor(config) {
11
- this.config = Object.assign(
12
- {
13
- ignore: ['*/node_modules/*', '*/.git/*', '*/cache/*', '*/backup/*']
14
- },
15
- config
16
- );
11
+ constructor(ignore, max_depth = 3) {
12
+ this.ignore = ignore || ['node_modules', '.git'];
13
+ this.max_depth = max_depth;
17
14
  }
18
15
  }
19
16
 
@@ -23,13 +20,15 @@ class Scanner {
23
20
  * @param {string} dir 根目录
24
21
  * @param {string} filename 目标文件名
25
22
  * @param {string} dirname 目标目录名
23
+ * @param {number} max_depth 最大搜索深度 -1表示不限制深度
24
+ * @param {Array} ignore 忽略目录数组
26
25
  * @returns {Array} 文件路径数组
27
26
  */
28
- Scanner.prototype.scan = async function (dir, filename, dirname) {
29
- let dirs = await this.scanDir(dir, dirname);
27
+ Scanner.prototype.scan = async function (dir, filename, dirname, ignore = this.ignore, max_depth = this.max_depth) {
28
+ let dirs = await this.scanDir(dir, dirname, ignore, max_depth);
30
29
  let results = await Promise.all(
31
30
  dirs.map((d) => {
32
- return $.file.getAllAsync(d, filename);
31
+ return $.file.getAllAsync(d, filename, undefined, ignore, max_depth);
33
32
  })
34
33
  );
35
34
  return results.flat();
@@ -40,10 +39,12 @@ Scanner.prototype.scan = async function (dir, filename, dirname) {
40
39
  * 在指定目录下找所有匹配 dirname 的子目录
41
40
  * @param {string} dir 根目录
42
41
  * @param {string} dirname 目标目录名
42
+ * @param {number} max_depth 最大搜索深度 -1表示不限制深度
43
+ * @param {Array} ignore 忽略目录数组
43
44
  * @returns {Array} 目录路径数组
44
45
  */
45
- Scanner.prototype.scanDir = async function (dir, dirname) {
46
- return await $.dir.getAllAsync(dir, dirname);
46
+ Scanner.prototype.scanDir = async function (dir, dirname, ignore = this.ignore, max_depth = this.max_depth) {
47
+ return await $.dir.getAllAsync(dir, dirname, ignore, max_depth);
47
48
  };
48
49
 
49
50
  /**
@@ -51,10 +52,12 @@ Scanner.prototype.scanDir = async function (dir, dirname) {
51
52
  * 在指定目录下找所有匹配 filename 的文件
52
53
  * @param {string} dir 根目录
53
54
  * @param {string} filename 目标文件名
55
+ * @param {number} max_depth 最大搜索深度 -1表示不限制深度
56
+ * @param {Array} ignore 忽略目录数组
54
57
  * @returns {Array} 文件路径数组
55
58
  */
56
- Scanner.prototype.scanFile = async function (dir, filename) {
57
- return await $.file.getAllAsync(dir, filename);
59
+ Scanner.prototype.scanFile = async function (dir, filename, ignore = this.ignore, max_depth = this.max_depth) {
60
+ return await $.file.getAllAsync(dir, filename, undefined, ignore, max_depth);
58
61
  };
59
62
 
60
63
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.4.0",
3
+ "version": "3.4.3",
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": {
@@ -33,14 +33,13 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "@prettier/sync": "^0.6.1",
36
- "mm_config": "^2.4.6",
37
- "mm_expand": "^2.4.6",
38
- "mm_hot_reload": "^1.4.3",
39
- "mm_tpl": "^2.6.5"
36
+ "mm_config": "^2.4.8",
37
+ "mm_hot_reload": "^1.4.7",
38
+ "mm_tpl": "^2.6.8"
40
39
  },
41
40
  "devDependencies": {
42
- "eslint": "^10.8.0",
43
- "eslint-plugin-jsdoc": "^63.3.2",
41
+ "eslint": "^10.8.1",
42
+ "eslint-plugin-jsdoc": "^64.0.1",
44
43
  "mm_eslint": "^1.7.5"
45
44
  }
46
45
  }