提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

  1. This library aims to provide codegen helpers and data structure for Vue language plugin API v1.x that does not depend on Volar runtime.
  2. ## Why "Muggle"?
  3. A better situation is Volar can use magic-string on the Vue plugin API, but currently can't do this easily.
  4. This solution is just for Vue language plugin API v1.x and planned to be deprecate in v2.
  5. ## Usage
  6. The example is base-on [magic-string readme](https://github.com/rich-harris/magic-string#usage).
  7. ```html
  8. <script>problems = 99</script>
  9. <more-script lang="js">console.log( answer )</more-script>
  10. ```
  11. ```ts
  12. import {
  13. toString,
  14. replace,
  15. } from 'muggle-string';
  16. /** @type {import('@volar/vue-language-core').VueLanguagePlugin} */
  17. const plugin = () => {
  18. return {
  19. name: 'example-vue-language-plugin',
  20. version: 1,
  21. resolveEmbeddedFile(fileName, sfc, embeddedFile) {
  22. if (embeddedFile.fileName.replace(fileName, '').match(/^\.(js|ts|jsx|tsx)$/)) {
  23. const s = embeddedFile.content;
  24. toString(s); // 'problems = 99'
  25. replace(s, 'problems', 'answer');
  26. toString(s); // 'answer = 99'
  27. replace(s, '99', '42');
  28. toString(s); // 'answer = 42'
  29. // add string by Array method directly
  30. s.unshift('var ');
  31. s.push(';');
  32. toString(s); // 'var answer = 42;'
  33. for (const block of sfc.customBlocks) {
  34. if (block.type === 'more-script') {
  35. s.push([
  36. block.content, // text to add
  37. block.name, // source
  38. 0, // content offset in source
  39. {
  40. // language capabilities to enable in this segment
  41. hover: true,
  42. references: true,
  43. definition: true,
  44. diagnostic: true,
  45. rename: true,
  46. completion: true,
  47. semanticTokens: true,
  48. },
  49. ]);
  50. toString(s); // 'var answer = 42;console.log( answer )'
  51. }
  52. }
  53. }
  54. }
  55. };
  56. };
  57. module.exports = plugin;
  58. ```