file-vue.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const parseSfc_1 = require("../utils/parseSfc");
  4. const plugin = (_ctx) => {
  5. return {
  6. version: 1,
  7. parseSFC(_fileName, content) {
  8. return (0, parseSfc_1.parse)(content);
  9. },
  10. updateSFC(sfc, change) {
  11. const blocks = [
  12. sfc.descriptor.template,
  13. sfc.descriptor.script,
  14. sfc.descriptor.scriptSetup,
  15. ...sfc.descriptor.styles,
  16. ...sfc.descriptor.customBlocks,
  17. ].filter((block) => !!block);
  18. const hitBlock = blocks.find(block => change.start >= block.loc.start.offset && change.end <= block.loc.end.offset);
  19. if (!hitBlock) {
  20. return;
  21. }
  22. const oldContent = hitBlock.content;
  23. const newContent = hitBlock.content =
  24. hitBlock.content.substring(0, change.start - hitBlock.loc.start.offset)
  25. + change.newText
  26. + hitBlock.content.substring(change.end - hitBlock.loc.start.offset);
  27. // #3449
  28. const endTagRegex = new RegExp(`</\\s*${hitBlock.type}\\s*>`);
  29. const insertedEndTag = !!oldContent.match(endTagRegex) !== !!newContent.match(endTagRegex);
  30. if (insertedEndTag) {
  31. return;
  32. }
  33. const lengthDiff = change.newText.length - (change.end - change.start);
  34. for (const block of blocks) {
  35. if (block.loc.start.offset > change.end) {
  36. block.loc.start.offset += lengthDiff;
  37. }
  38. if (block.loc.end.offset >= change.end) {
  39. block.loc.end.offset += lengthDiff;
  40. }
  41. }
  42. return sfc;
  43. },
  44. };
  45. };
  46. exports.default = plugin;
  47. //# sourceMappingURL=file-vue.js.map