mm_statics 1.5.8 → 1.6.0

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 +53 -21
  2. package/package.json +3 -2
package/index.js CHANGED
@@ -49,6 +49,8 @@ class Static {
49
49
  */
50
50
  Static.prototype.setConfig = function (config) {
51
51
  Object.assign(this.config, config || {});
52
+ // 判断root是否绝对路径,不是则改成绝对路径
53
+ this.config.root = this.config.root.fullname();
52
54
  };
53
55
 
54
56
  /**
@@ -76,7 +78,7 @@ Static.prototype._isEs6Code = function (code) {
76
78
  if (!code || typeof code !== 'string') {
77
79
  return false;
78
80
  }
79
-
81
+
80
82
  // ES6语法特征检测
81
83
  const es6_patterns = [
82
84
  /\bimport\s+[^;]+;/g, // import语句
@@ -91,13 +93,13 @@ Static.prototype._isEs6Code = function (code) {
91
93
  /\basync\s+function\b/g, // async函数
92
94
  /\bawait\b/g // await关键字
93
95
  ];
94
-
96
+
95
97
  for (let i = 0; i < es6_patterns.length; i++) {
96
98
  if (es6_patterns[i].test(code)) {
97
99
  return true;
98
100
  }
99
101
  }
100
-
102
+
101
103
  return false;
102
104
  };
103
105
 
@@ -111,12 +113,12 @@ Static.prototype._formatCode = async function (code, file_type) {
111
113
  if (!this.config.format || !code || typeof code !== 'string') {
112
114
  return code;
113
115
  }
114
-
116
+
115
117
  try {
116
118
  // 根据文件类型设置prettier配置
117
119
  let parser = 'babel';
118
120
  let plugins = [];
119
-
121
+
120
122
  switch (file_type) {
121
123
  case 'js':
122
124
  parser = 'babel';
@@ -131,7 +133,7 @@ Static.prototype._formatCode = async function (code, file_type) {
131
133
  default:
132
134
  parser = 'babel';
133
135
  }
134
-
136
+
135
137
  // prettier.format是异步函数
136
138
  const formatted_code = await prettier.format(code, {
137
139
  parser: parser,
@@ -145,7 +147,7 @@ Static.prototype._formatCode = async function (code, file_type) {
145
147
  bracketSpacing: true,
146
148
  arrowParens: 'avoid'
147
149
  });
148
-
150
+
149
151
  return formatted_code;
150
152
  } catch (error) {
151
153
  // 格式化失败时返回原代码
@@ -154,6 +156,32 @@ Static.prototype._formatCode = async function (code, file_type) {
154
156
  }
155
157
  };
156
158
 
159
+ /**
160
+ * 处理require模块语句
161
+ * @param {string} script 包含require语句的代码
162
+ * @returns {string|null} 处理后的代码或null
163
+ */
164
+ Static.prototype._requireMod = function (script) {
165
+ if (script) {
166
+ var src = script.between('[', ']');
167
+ var imports = src;
168
+ var arr = imports.split(',');
169
+ var len = arr.length;
170
+ for (var i = 0; i < len; i++) {
171
+ var s = arr[i];
172
+ if (s.endsWith('.vue"')) {
173
+ imports = imports.replace(s, '"vue!' + s.trim().trim('"') + '"');
174
+ } else if (s.endsWith('.css"')) {
175
+ imports = imports.replace(s, '"css!' + s.trim().trim('"') + '"');
176
+ } else if (s.endsWith('.html"')) {
177
+ imports = imports.replace(s, '"text!' + s.trim().trim('"') + '"');
178
+ }
179
+ }
180
+ return script.replace(src, imports);
181
+ }
182
+ return null;
183
+ };
184
+
157
185
  /**
158
186
  * 转换为AMD模块
159
187
  * @param {string} file_type 文件类型
@@ -166,6 +194,8 @@ Static.prototype._toAmd = async function (file_type, text) {
166
194
  // 对JS文件,先检测是否为ES6代码,只有ES6代码才进行转换
167
195
  if (this._isEs6Code(txt)) {
168
196
  let code = this.convert.toAmd(txt);
197
+ // 处理require模块语句
198
+ code = this._requireMod(code);
169
199
  // 格式化代码
170
200
  return await this._formatCode(code, file_type);
171
201
  } else {
@@ -184,6 +214,8 @@ Static.prototype._toAmd = async function (file_type, text) {
184
214
  // 先检测script标签内的代码是否为ES6语法
185
215
  if (this._isEs6Code(inner_code)) {
186
216
  let amd_code = this.convert.toAmd(inner_code);
217
+ // 处理require模块语句
218
+ amd_code = this._requireMod(amd_code);
187
219
  // 移除type="module"属性,因为AMD模块不需要这个属性
188
220
  let new_script = script.replace(/type\s*=\s*["']module["']/g, '').replace(inner_code, amd_code);
189
221
  // 清理多余的空格
@@ -208,23 +240,23 @@ Static.prototype._toAmd = async function (file_type, text) {
208
240
  */
209
241
  Static.prototype._processTemplate = function (desc) {
210
242
  if (!desc.template) return '';
211
-
243
+
212
244
  const template_result = compile(desc.template.content, {
213
245
  mode: 'module'
214
246
  });
215
-
247
+
216
248
  // 提取render函数代码,去除import语句和export语句
217
249
  let render_function = template_result.code
218
250
  .replace(/^import[^;]+;\s*/gm, '')
219
251
  .replace(/^export[^;]+;\s*/gm, '')
220
252
  .trim();
221
-
253
+
222
254
  // 如果render函数包含函数定义,提取函数体
223
255
  let render_match = render_function.match(/function\s+render\s*\([^)]*\)\s*{([\s\S]*?)}\s*$/);
224
256
  if (render_match) {
225
257
  render_function = `function render(_ctx, _cache) {${render_match[1]}}`;
226
258
  }
227
-
259
+
228
260
  return render_function;
229
261
  };
230
262
 
@@ -238,16 +270,16 @@ Static.prototype._processScript = function (desc, render_function) {
238
270
  if (!desc.script) {
239
271
  return this._createDefaultComp(render_function);
240
272
  }
241
-
273
+
242
274
  let script_content = desc.script.content;
243
-
275
+
244
276
  // 提取import语句
245
277
  let imports = '';
246
278
  let import_match = script_content.match(/import[^;]+;/g);
247
279
  if (import_match) {
248
280
  imports = import_match.join('\n') + '\n';
249
281
  }
250
-
282
+
251
283
  // 提取export default部分,保留完整的组件定义
252
284
  let export_match = script_content.match(/export\s+default\s*({[\s\S]*?})\s*$/s);
253
285
  if (export_match) {
@@ -281,12 +313,12 @@ export default {
281
313
  */
282
314
  Static.prototype._buildCompWithExport = function (code_lib, imports, render_function) {
283
315
  let proc_code = code_lib;
284
-
316
+
285
317
  // 确保组件字符串是完整的对象结构
286
318
  if (!proc_code.trim().endsWith('}')) {
287
319
  proc_code = proc_code + '\n}';
288
320
  }
289
-
321
+
290
322
  // 如果render函数存在,将其添加到组件选项中
291
323
  if (render_function) {
292
324
  // 在最后一个属性之前插入render函数
@@ -299,7 +331,7 @@ Static.prototype._buildCompWithExport = function (code_lib, imports, render_func
299
331
  proc_code += `\n render: ${render_function}\n}`;
300
332
  }
301
333
  }
302
-
334
+
303
335
  return `${imports}
304
336
  export default ${proc_code};
305
337
  `;
@@ -539,7 +571,7 @@ Static.prototype._delCache = async function (path) {
539
571
  */
540
572
  Static.prototype._handleCache = async function (url, ctx) {
541
573
  if (!this.config.cache) return false;
542
-
574
+
543
575
  let ret = await this._getCache(url);
544
576
  if (ret) {
545
577
  this._return(ctx, ret.body, ret.headers);
@@ -555,17 +587,17 @@ Static.prototype._handleCache = async function (url, ctx) {
555
587
  */
556
588
  Static.prototype._handleFileCompile = async function (path) {
557
589
  if (!path.startsWith(this.config.path)) return null;
558
-
590
+
559
591
  let file_type = this._getType(path);
560
592
  let { compile_vue, convert_amd } = this.config;
561
-
593
+
562
594
  if (convert_amd && this.config.files.includes('.' + file_type)) {
563
595
  return await this._runAmd(path, file_type);
564
596
  }
565
597
  else if (compile_vue && file_type === 'vue') {
566
598
  return await this._runVue(path);
567
599
  }
568
-
600
+
569
601
  return null;
570
602
  };
571
603
 
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "mm_statics",
3
- "version": "1.5.8",
3
+ "version": "1.6.0",
4
4
  "description": "这是超级美眉statics函数模块,用于web服务端statics缓存",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "mocha tests/*.js"
7
+ "test": "node ./tests/index.js",
8
+ "tests": "mocha tests/*.js"
8
9
  },
9
10
  "repository": {
10
11
  "type": "git",