mm_statics 1.7.6 → 1.7.7

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/index.js +91 -2
  2. package/package.json +9 -8
package/index.js CHANGED
@@ -107,8 +107,8 @@ Static.prototype._isEs6Code = function (code) {
107
107
 
108
108
  // ES6语法特征检测
109
109
  const es6_patterns = [
110
- /\bimport\s+[^;]+;/g, // import语句
111
- /\bexport\s+[^;]+;/g, // export语句
110
+ /\bimport\s+/g, // import语句(支持多行和无分号)
111
+ /\bexport\s+/g, // export语句(支持多行和无分号)
112
112
  /\bclass\s+\w+/g, // class声明
113
113
  /\bconst\s+\w+/g, // const声明
114
114
  /\blet\s+\w+/g, // let声明
@@ -222,6 +222,8 @@ Static.prototype._toAmd = async function (file_type, text) {
222
222
  let code = this.convert.toAmd(txt);
223
223
  // 处理require模块语句
224
224
  code = this._requireMod(code);
225
+ // 添加return语句确保AMD模块正确导出
226
+ code = this._addAmdReturn(code);
225
227
  // 格式化代码
226
228
  return await this._formatCode(code, file_type);
227
229
  } else {
@@ -242,6 +244,8 @@ Static.prototype._toAmd = async function (file_type, text) {
242
244
  let amd_code = this.convert.toAmd(inner_code);
243
245
  // 处理require模块语句
244
246
  amd_code = this._requireMod(amd_code);
247
+ // 添加return语句确保AMD模块正确导出
248
+ amd_code = this._addAmdReturn(amd_code);
245
249
  // 移除type="module"属性,因为AMD模块不需要这个属性
246
250
  let new_script = script.replace(/type\s*=\s*["']module["']/g, '').replace(inner_code, amd_code);
247
251
  // 清理多余的空格
@@ -259,6 +263,91 @@ Static.prototype._toAmd = async function (file_type, text) {
259
263
  return result;
260
264
  };
261
265
 
266
+ /**
267
+ * 为AMD模块添加return语句
268
+ * @param {string} code AMD模块代码
269
+ * @returns {string} 添加return后的代码
270
+ */
271
+ Static.prototype._addAmdReturn = function (code) {
272
+ if (!code || typeof code !== 'string') {
273
+ return code;
274
+ }
275
+
276
+ // 检查是否已经是简洁格式(有return语句)
277
+ if (code.includes('return {')) {
278
+ return code;
279
+ }
280
+
281
+ // 解析 mm_es6_to_amd 生成的格式并转换为简洁的 AMD 格式
282
+ // 原始格式: define(["exports", "countup"], function (_exports, _countup) { ... });
283
+ // 目标格式: define(["countup"], function (_countup) { ... return { ... }; });
284
+
285
+ // 1. 提取依赖数组(去掉 exports)
286
+ const deps_match = code.match(/define\(\s*\[([^\]]+)\]/);
287
+ if (!deps_match) {
288
+ return code;
289
+ }
290
+ let deps_str = deps_match[1];
291
+ // 移除 exports 依赖
292
+ deps_str = deps_str.replace(/\s*(["'])exports\1\s*,?\s*/g, '');
293
+ deps_str = deps_str.replace(/,\s*$/, ''); // 移除末尾逗号
294
+
295
+ // 2. 提取函数参数(去掉 _exports)
296
+ const params_match = code.match(/function\s*\(\s*([^)]+)\s*\)/);
297
+ if (!params_match) {
298
+ return code;
299
+ }
300
+ let params_str = params_match[1];
301
+ // 移除 _exports 参数
302
+ params_str = params_str.replace(/\s*_exports\s*,?\s*/g, '');
303
+ params_str = params_str.replace(/,\s*$/, ''); // 移除末尾逗号
304
+
305
+ // 3. 提取模块内容(去掉 exports 相关代码)
306
+ const body_match = code.match(/function\s*\([^)]+\)\s*\{([\s\S]*)\}\s*\);$/);
307
+ if (!body_match) {
308
+ return code;
309
+ }
310
+ let body = body_match[1];
311
+
312
+ // 移除 'use strict'
313
+ body = body.replace(/^\s*["']use strict["']\s*;\s*/, '');
314
+
315
+ // 移除 exports 相关代码
316
+ body = body.replace(/Object\.defineProperty\(_exports,\s*["']__esModule["']\s*,\s*\{[^}]+\}\s*\);\s*/g, '');
317
+ body = body.replace(/_exports\[["']default["']\s*=\s*void\s*0;\s*/g, '');
318
+
319
+ // 提取并转换 default 导出对象
320
+ // 使用字符串处理方法来处理嵌套的花括号
321
+ const default_start = body.indexOf('_exports["default"] =');
322
+ if (default_start === -1) {
323
+ return code;
324
+ }
325
+
326
+ // 找到赋值后的第一个 {
327
+ const obj_start = body.indexOf('{', default_start);
328
+ if (obj_start === -1) {
329
+ return code;
330
+ }
331
+
332
+ // 匹配花括号(处理嵌套)
333
+ let brace_count = 1;
334
+ let i = obj_start + 1;
335
+ while (i < body.length && brace_count > 0) {
336
+ if (body[i] === '{') brace_count++;
337
+ if (body[i] === '}') brace_count--;
338
+ i++;
339
+ }
340
+
341
+ // 提取对象字面量
342
+ const default_obj = body.substring(obj_start, i);
343
+ body = 'return ' + default_obj + ';';
344
+
345
+ // 4. 重新组装简洁的 AMD 格式
346
+ const new_amd = `define([${deps_str}], function(${params_str}) {\n${body}\n});`;
347
+
348
+ return new_amd;
349
+ };
350
+
262
351
 
263
352
 
264
353
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_statics",
3
- "version": "1.7.6",
3
+ "version": "1.7.7",
4
4
  "description": "这是超级美眉statics函数模块,用于web服务端statics缓存",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -37,19 +37,20 @@
37
37
  "static/"
38
38
  ],
39
39
  "dependencies": {
40
- "@vue/compiler-sfc": "^3.5.33",
40
+ "@vue/compiler-sfc": "^3.5.34",
41
41
  "abstract-syntax-tree": "^2.22.0",
42
42
  "chokidar": "^5.0.0",
43
- "marked": "^18.0.2",
44
- "mm_cache": "^1.4.8",
45
- "mm_es6_to_amd": "^1.4.7",
43
+ "koa-send": "^5.0.1",
44
+ "marked": "^18.0.4",
45
+ "mm_cache": "^1.4.9",
46
+ "mm_es6_to_amd": "^1.4.9",
46
47
  "prettier": "^3.8.3"
47
48
  },
48
49
  "devDependencies": {
49
- "eslint": "^10.2.1",
50
- "eslint-plugin-jsdoc": "^62.9.0",
50
+ "eslint": "^10.4.0",
51
+ "eslint-plugin-jsdoc": "^63.0.0",
51
52
  "mm_eslint": "^1.7.1",
52
- "mocha": "^11.7.5",
53
+ "mocha": "^11.7.6",
53
54
  "supertest": "^7.2.2"
54
55
  }
55
56
  }