mm_statics 1.5.8 → 1.5.9

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