mm_machine 3.5.2 → 3.5.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.
Files changed (2) hide show
  1. package/lib/auto_require.js +49 -16
  2. package/package.json +1 -1
@@ -81,6 +81,27 @@ function _npmInstall(name, cwd) {
81
81
  { cwd: install_dir, stdio: 'pipe' });
82
82
  }
83
83
 
84
+ /**
85
+ * 安装指定npm包
86
+ * @param {string} name 包名或模块标识符
87
+ * @returns {boolean} 是否已安装
88
+ */
89
+ function _installByName(name) {
90
+ let pkg_name = _extractPkgName(name);
91
+ if (!pkg_name || _AUTO_INST[pkg_name]) {
92
+ return false;
93
+ }
94
+ _AUTO_INST[pkg_name] = true;
95
+ try {
96
+ _npmInstall(pkg_name);
97
+ _clearPkgCache(pkg_name);
98
+ return true;
99
+ } catch (e) {
100
+ _AUTO_INST[pkg_name] = false;
101
+ return false;
102
+ }
103
+ }
104
+
84
105
  /**
85
106
  * 分析 $.mod.load 失败原因,若是缺少npm依赖则安装
86
107
  * @param {string} file 文件路径
@@ -98,21 +119,14 @@ function _tryInstall(file) {
98
119
  if (!raw_name || _isLocal(raw_name) || _isBuiltin(raw_name)) {
99
120
  return false;
100
121
  }
101
- let pkg_name = _extractPkgName(raw_name);
102
- if (!pkg_name || _AUTO_INST[pkg_name]) {
103
- return false;
104
- }
105
- _AUTO_INST[pkg_name] = true;
106
- _npmInstall(pkg_name);
107
- _clearPkgCache(pkg_name);
108
- return true;
122
+ return _installByName(raw_name);
109
123
  }
110
124
  }
111
125
 
112
126
  /**
113
- * 增强require函数,支持脚本文件(.js)热重载和依赖自动安装
127
+ * 增强require函数,支持脚本文件(.js)热重载、内置模块和npm包加载
114
128
  * 当引用的目标模块缺少npm依赖时,自动npm install并重试加载
115
- * @param {string} file 脚本文件路径(.js)
129
+ * @param {string} file 文件路径、内置模块名或npm包名
116
130
  * @param {Function} func 热更新回调函数,文件变化时触发
117
131
  * @returns {object} 加载的模块对象
118
132
  * @throws {TypeError} 参数类型错误时抛出
@@ -129,13 +143,32 @@ $.require = function (file, func) {
129
143
  return require(file);
130
144
  }
131
145
 
132
- let mod = $.mod.load(file, func);
133
- if (mod !== null) {
134
- return mod;
146
+ // 本地文件路径:热重载加载
147
+ if (_isLocal(file)) {
148
+ let mod = $.mod.load(file, func);
149
+ if (mod !== null) {
150
+ return mod;
151
+ }
152
+ if (_tryInstall(file)) {
153
+ return $.mod.load(file, func);
154
+ }
155
+ return null;
135
156
  }
136
157
 
137
- if (_tryInstall(file)) {
138
- return $.mod.load(file, func);
158
+ // npm包名:原生加载,失败时先按文件再自动安装
159
+ try {
160
+ return require(file);
161
+ } catch (e) {
162
+ if (e.code !== 'MODULE_NOT_FOUND') {
163
+ return null;
164
+ }
165
+ let mod = $.mod.load(file, func);
166
+ if (mod !== null) {
167
+ return mod;
168
+ }
169
+ if (_installByName(file)) {
170
+ return require(file);
171
+ }
172
+ return null;
139
173
  }
140
- return null;
141
174
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.5.2",
3
+ "version": "3.5.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": {