mm_machine 3.5.1 → 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 +66 -17
  2. package/package.json +1 -1
@@ -1,6 +1,8 @@
1
1
  require('mm_hot_reload');
2
2
 
3
3
  const _AUTO_INST = {};
4
+ // 内置模块名集合
5
+ const BUILTIN_MODULES = new Set(require('module').builtinModules);
4
6
 
5
7
  /**
6
8
  * 从模块标识符中提取npm包名
@@ -36,6 +38,16 @@ function _isLocal(name) {
36
38
  return name.startsWith('.') || name.startsWith('/') || /^[a-zA-Z]:/.test(name);
37
39
  }
38
40
 
41
+ /**
42
+ * 是否为Node内置模块
43
+ * @param {string} name 模块名
44
+ * @returns {boolean} 是否为内置模块
45
+ */
46
+ function _isBuiltin(name) {
47
+ let raw_name = name.startsWith('node:') ? name.slice(5) : name;
48
+ return BUILTIN_MODULES.has(raw_name);
49
+ }
50
+
39
51
  /**
40
52
  * 清除指定包的require缓存
41
53
  * @param {string} name 包名
@@ -69,6 +81,27 @@ function _npmInstall(name, cwd) {
69
81
  { cwd: install_dir, stdio: 'pipe' });
70
82
  }
71
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
+
72
105
  /**
73
106
  * 分析 $.mod.load 失败原因,若是缺少npm依赖则安装
74
107
  * @param {string} file 文件路径
@@ -83,24 +116,17 @@ function _tryInstall(file) {
83
116
  return false;
84
117
  }
85
118
  let raw_name = _extractErrName(e);
86
- if (!raw_name || _isLocal(raw_name)) {
87
- return false;
88
- }
89
- let pkg_name = _extractPkgName(raw_name);
90
- if (!pkg_name || _AUTO_INST[pkg_name]) {
119
+ if (!raw_name || _isLocal(raw_name) || _isBuiltin(raw_name)) {
91
120
  return false;
92
121
  }
93
- _AUTO_INST[pkg_name] = true;
94
- _npmInstall(pkg_name);
95
- _clearPkgCache(pkg_name);
96
- return true;
122
+ return _installByName(raw_name);
97
123
  }
98
124
  }
99
125
 
100
126
  /**
101
- * 增强require函数,支持脚本文件(.js)热重载和依赖自动安装
127
+ * 增强require函数,支持脚本文件(.js)热重载、内置模块和npm包加载
102
128
  * 当引用的目标模块缺少npm依赖时,自动npm install并重试加载
103
- * @param {string} file 脚本文件路径(.js)
129
+ * @param {string} file 文件路径、内置模块名或npm包名
104
130
  * @param {Function} func 热更新回调函数,文件变化时触发
105
131
  * @returns {object} 加载的模块对象
106
132
  * @throws {TypeError} 参数类型错误时抛出
@@ -113,13 +139,36 @@ $.require = function (file, func) {
113
139
  throw new TypeError('第二个参数(func)必须是函数');
114
140
  }
115
141
 
116
- let mod = $.mod.load(file, func);
117
- if (mod !== null) {
118
- return mod;
142
+ if (_isBuiltin(file)) {
143
+ return require(file);
144
+ }
145
+
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;
119
156
  }
120
157
 
121
- if (_tryInstall(file)) {
122
- 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;
123
173
  }
124
- return null;
125
174
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.5.1",
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": {