mm_machine 3.4.0 → 3.4.1

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/lib/drive.js +24 -14
  2. package/package.json +4 -5
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.4.0",
3
+ "version": "3.4.1",
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": {
@@ -34,13 +34,12 @@
34
34
  "dependencies": {
35
35
  "@prettier/sync": "^0.6.1",
36
36
  "mm_config": "^2.4.6",
37
- "mm_expand": "^2.4.6",
38
37
  "mm_hot_reload": "^1.4.3",
39
- "mm_tpl": "^2.6.5"
38
+ "mm_tpl": "^2.6.6"
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
  }