mm_statics 1.6.6 → 1.6.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 +30 -134
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  let send = require('koa-send');
2
2
  const { EsToAmdConvert } = require('mm_es6_to_amd');
3
- const { parse } = require('@vue/compiler-sfc');
4
- const { compile } = require('@vue/compiler-dom');
3
+ const VueSFCCompiler = require('./vue_compiler.js');
5
4
  const prettier = require('prettier');
6
5
  const { marked } = require('marked');
7
6
 
7
+ var vueCompiler = new VueSFCCompiler();
8
+
8
9
  /**
9
10
  * 静态文件处理类
10
11
  */
@@ -45,11 +46,11 @@ class Static {
45
46
  // 需要转换的静态文件扩展名
46
47
  files: ['.js', '.vue', '.html'],
47
48
  // 是否转换ES6模块为AMD模块
48
- convert_amd: true,
49
+ convert_amd: false, // 修改为 false,避免 Vue 文件被错误转换为 AMD 格式
49
50
  // 是否开启文件监听
50
51
  watch: false,
51
52
  // 监听的文件扩展名
52
- watch_files: ['.js', '.css', '.html', '.vue', '.json', '.md', '.txt', '.xml']
53
+ watch_files: ['.js', '.css', '.html', '.vue', '.json', '.md']
53
54
  };
54
55
 
55
56
  /**
@@ -256,136 +257,27 @@ Static.prototype._toAmd = async function (file_type, text) {
256
257
  return result;
257
258
  };
258
259
 
259
- /**
260
- * 处理Vue模板部分
261
- * @param {object} desc Vue组件描述符
262
- * @returns {string} 编译后的render函数
263
- */
264
- Static.prototype._processTemplate = function (desc) {
265
- if (!desc.template) return '';
266
-
267
- const template_result = compile(desc.template.content, {
268
- mode: 'module'
269
- });
270
-
271
- // 提取render函数代码,去除import语句和export语句
272
- let render_function = template_result.code
273
- .replace(/^import[^;]+;\s*/gm, '')
274
- .replace(/^export[^;]+;\s*/gm, '')
275
- .trim();
276
-
277
- // 如果render函数包含函数定义,提取函数体
278
- let render_match = render_function.match(/function\s+render\s*\([^)]*\)\s*{([\s\S]*?)}\s*$/);
279
- if (render_match) {
280
- render_function = `function render(_ctx, _cache) {${render_match[1]}}`;
281
- }
282
-
283
- return render_function;
284
- };
285
-
286
- /**
287
- * 处理Vue脚本部分
288
- * @param {object} desc Vue组件描述符
289
- * @param {string} render_function render函数
290
- * @returns {string} 处理后的脚本代码
291
- */
292
- Static.prototype._processScript = function (desc, render_function) {
293
- if (!desc.script) {
294
- return this._createDefaultComp(render_function);
295
- }
296
-
297
- let script_content = desc.script.content;
298
-
299
- // 提取import语句
300
- let imports = '';
301
- let import_match = script_content.match(/import[^;]+;/g);
302
- if (import_match) {
303
- imports = import_match.join('\n') + '\n';
304
- }
305
-
306
- // 提取export default部分,保留完整的组件定义
307
- let export_match = script_content.match(/export\s+default\s*({[\s\S]*?})\s*$/s);
308
- if (export_match) {
309
- return this._buildCompWithExport(export_match[1], imports, render_function);
310
- } else {
311
- return this._buildDefaultComp(imports, render_function);
312
- }
313
- };
314
260
 
315
- /**
316
- * 创建默认组件结构
317
- * @param {string} render_function render函数
318
- * @returns {string} 默认组件代码
319
- */
320
- Static.prototype._createDefaultComp = function (render_function) {
321
- return `
322
- export default {
323
- name: 'VueComponent',
324
- data() {
325
- return {}
326
- }${render_function ? `,\n render: ${render_function}` : ''}\n};
327
- `;
328
- };
329
261
 
330
262
  /**
331
- * 构建带有export的组件
332
- * @param {string} code_lib 组件代码
333
- * @param {string} imports import语句
334
- * @param {string} render_function render函数
335
- * @returns {string} 完整的组件代码
336
- */
337
- Static.prototype._buildCompWithExport = function (code_lib, imports, render_function) {
338
- let proc_code = code_lib;
339
-
340
- // 确保组件字符串是完整的对象结构
341
- if (!proc_code.trim().endsWith('}')) {
342
- proc_code = proc_code + '\n}';
343
- }
344
-
345
- // 如果render函数存在,将其添加到组件选项中
346
- if (render_function) {
347
- // 在最后一个属性之前插入render函数
348
- if (proc_code.trim().endsWith('}')) {
349
- proc_code = proc_code.replace(/}\s*$/, '');
350
- // 确保最后一个属性后有逗号
351
- if (!proc_code.trim().endsWith(',')) {
352
- proc_code += ',';
353
- }
354
- proc_code += `\n render: ${render_function}\n}`;
263
+ * 编译Vue组件
264
+ * @param {string} code Vue组件代码
265
+ * @param {string} filePath 文件路径
266
+ * @returns {Promise<string>} 可直接调用的JavaScript组件代码
267
+ */
268
+ Static.prototype._compileVue = async function (code, filePath = '') {
269
+ try {
270
+ // 使用修复后的 Vue 编译器
271
+ const { code: compiledCode } = await vueCompiler.toJS(code, filePath);
272
+
273
+ // Vue 编译后的代码是 JavaScript,应该使用 babel 解析器格式化
274
+ const result = await this._formatCode(compiledCode, 'babel');
275
+ return result;
276
+ } catch (error) {
277
+ console.error('Vue编译失败:', error);
278
+ return `console.error('Vue编译失败: ${error.message}');\nexport default { name: 'ErrorComponent' };`;
355
279
  }
356
- }
357
-
358
- return `${imports}
359
- export default ${proc_code};
360
- `;
361
- };
362
-
363
- /**
364
- * 构建默认组件
365
- * @param {string} imports import语句
366
- * @param {string} render_function render函数
367
- * @returns {string} 默认组件代码
368
- */
369
- Static.prototype._buildDefaultComp = function (imports, render_function) {
370
- return `${imports}
371
- export default {
372
- name: 'VueComponent',
373
- data() {
374
- return {}
375
- }${render_function ? `,\n render: ${render_function}` : ''}\n};
376
- `;
377
- };
378
-
379
- /**
380
- * 编译Vue组件
381
- * @param {string} code Vue组件代码
382
- * @returns {Promise<string>} 可直接调用的JavaScript组件代码
383
- */
384
- Static.prototype._compileVue = async function (code) {
385
- let desc = parse(code).descriptor;
386
- let render_function = this._processTemplate(desc);
387
- return await this._processScript(desc, render_function);
388
- };
280
+ };
389
281
 
390
282
  /**
391
283
  * 发送文件
@@ -433,7 +325,7 @@ Static.prototype._runVue = async function (path) {
433
325
  let file = `.${path}`.fullname(this.config.root);
434
326
  let text = file.loadText();
435
327
  if (text) {
436
- code = await this._compileVue(text);
328
+ code = await this._compileVue(text, file);
437
329
  }
438
330
  return code;
439
331
  };
@@ -451,11 +343,11 @@ Static.prototype._extractMarkdownTitle = function (text) {
451
343
  // 匹配一级标题格式:# 标题
452
344
  var title_regex = /^#\s+(.+)$/m;
453
345
  var match = text.match(title_regex);
454
-
346
+
455
347
  if (match && match[1]) {
456
348
  return match[1].trim();
457
349
  }
458
-
350
+
459
351
  return null;
460
352
  };
461
353
 
@@ -473,7 +365,7 @@ Static.prototype._runMarkdown = async function (path) {
473
365
  try {
474
366
  // 使用marked将markdown转换为HTML
475
367
  let markdown_html = marked.parse(text);
476
-
368
+
477
369
  // 优先从Markdown内容中提取一级标题,后备使用文件名
478
370
  var title_from_content = this._extractMarkdownTitle(text);
479
371
  var title_from_file = path.split('/').pop().replace('.md', '');
@@ -621,6 +513,10 @@ Static.prototype._getHeaders = function (file_type) {
621
513
  else if (this.config.compile_vue) {
622
514
  headers['content-type'] = 'application/javascript; charset=utf-8';
623
515
  }
516
+ else {
517
+ // 当不编译 Vue 文件时,设置为 JavaScript MIME 类型
518
+ headers['content-type'] = 'application/javascript; charset=utf-8';
519
+ }
624
520
  break;
625
521
  case 'html':
626
522
  headers['content-type'] = 'text/html; charset=utf-8';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_statics",
3
- "version": "1.6.6",
3
+ "version": "1.6.7",
4
4
  "description": "这是超级美眉statics函数模块,用于web服务端statics缓存",
5
5
  "main": "index.js",
6
6
  "scripts": {