file-html.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const sfcBlockReg = /\<(script|style)\b([\s\S]*?)\>([\s\S]*?)\<\/\1\>/g;
  4. const langReg = /\blang\s*=\s*(['\"]?)(\S*)\b\1/;
  5. const plugin = () => {
  6. return {
  7. version: 1,
  8. parseSFC(fileName, content) {
  9. if (fileName.endsWith('.html')) {
  10. let sfc = {
  11. descriptor: {
  12. filename: fileName,
  13. source: content,
  14. template: null,
  15. script: null,
  16. scriptSetup: null,
  17. styles: [],
  18. customBlocks: [],
  19. cssVars: [],
  20. shouldForceReload: () => false,
  21. slotted: false,
  22. },
  23. errors: [],
  24. };
  25. let templateContent = content;
  26. for (const match of content.matchAll(sfcBlockReg)) {
  27. const matchText = match[0];
  28. const tag = match[1];
  29. const attrs = match[2];
  30. const lang = attrs.match(langReg)?.[2];
  31. const content = match[3];
  32. const contentStart = match.index + matchText.indexOf(content);
  33. if (tag === 'style') {
  34. sfc.descriptor.styles.push({
  35. attrs: {},
  36. content,
  37. loc: {
  38. start: { column: -1, line: -1, offset: contentStart },
  39. end: { column: -1, line: -1, offset: contentStart + content.length },
  40. source: content,
  41. },
  42. type: 'style',
  43. lang,
  44. });
  45. }
  46. // ignore `<script src="...">`
  47. else if (tag === 'script' && attrs.indexOf('src=') === -1) {
  48. let type = attrs.indexOf('type=') >= 0 ? 'scriptSetup' : 'script';
  49. sfc.descriptor[type] = {
  50. attrs: {},
  51. content,
  52. loc: {
  53. start: { column: -1, line: -1, offset: contentStart },
  54. end: { column: -1, line: -1, offset: contentStart + content.length },
  55. source: content,
  56. },
  57. type: 'script',
  58. lang,
  59. };
  60. }
  61. templateContent = templateContent.substring(0, match.index) + ' '.repeat(matchText.length) + templateContent.substring(match.index + matchText.length);
  62. }
  63. sfc.descriptor.template = {
  64. attrs: {},
  65. content: templateContent,
  66. loc: {
  67. start: { column: -1, line: -1, offset: 0 },
  68. end: { column: -1, line: -1, offset: templateContent.length },
  69. source: templateContent,
  70. },
  71. type: 'template',
  72. ast: {},
  73. };
  74. return sfc;
  75. }
  76. ;
  77. }
  78. };
  79. };
  80. exports.default = plugin;
  81. //# sourceMappingURL=file-html.js.map