版博士V2.0程序
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.
 
 
 
 

114 lines
3.1 KiB

  1. // src/index.ts
  2. import { createUnplugin } from "unplugin";
  3. import {
  4. REGEX_SETUP_SFC,
  5. REGEX_VUE_SFC,
  6. REGEX_VUE_SUB,
  7. createFilter,
  8. detectVueVersion
  9. } from "@vue-macros/common";
  10. // src/core/index.ts
  11. import {
  12. MagicString,
  13. addNormalScript,
  14. getTransformResult,
  15. isStaticExpression,
  16. parseSFC
  17. } from "@vue-macros/common";
  18. var MAGIC_COMMENT = "hoist-static";
  19. function transformHoistStatic(code, id) {
  20. function moveToScript(decl, prefix = "") {
  21. if (scriptOffset === void 0)
  22. scriptOffset = normalScript.start();
  23. const text = `
  24. ${prefix}${s.sliceNode(decl, { offset: setupOffset })}`;
  25. s.appendRight(scriptOffset, text);
  26. s.removeNode(decl, { offset: setupOffset });
  27. }
  28. const sfc = parseSFC(code, id);
  29. const { scriptSetup, getSetupAst } = sfc;
  30. if (!scriptSetup)
  31. return;
  32. const setupOffset = scriptSetup.loc.start.offset;
  33. const setupOffsetEnd = scriptSetup.loc.end.offset;
  34. const s = new MagicString(code);
  35. const program = getSetupAst();
  36. let normalScript = addNormalScript(sfc, s);
  37. let scriptOffset;
  38. for (const stmt of program.body) {
  39. if (stmt.type === "VariableDeclaration" && stmt.kind === "const") {
  40. const decls = stmt.declarations;
  41. let count = 0;
  42. for (const [i, decl] of decls.entries()) {
  43. if (!decl.init || !isStaticExpression(decl.init, {
  44. unary: true,
  45. magicComment: MAGIC_COMMENT
  46. }))
  47. continue;
  48. count++;
  49. moveToScript(decl, "const ");
  50. if (decls.length > 1) {
  51. const isLast = i === decls.length - 1;
  52. const start = isLast ? decls[i - 1].end : decl.end;
  53. const end = isLast ? decl.start : decls[i + 1].start;
  54. s.remove(setupOffset + start, setupOffset + end);
  55. }
  56. }
  57. if (count === decls.length) {
  58. s.removeNode(stmt, { offset: setupOffset });
  59. }
  60. } else if (stmt.type === "TSEnumDeclaration") {
  61. const isAllConstant = stmt.members.every(
  62. (member) => !member.initializer || isStaticExpression(member.initializer, {
  63. unary: true,
  64. magicComment: MAGIC_COMMENT
  65. })
  66. );
  67. if (!isAllConstant)
  68. continue;
  69. moveToScript(stmt);
  70. }
  71. }
  72. const restSetup = s.slice(setupOffset, setupOffsetEnd);
  73. if (restSetup.trim().length === 0) {
  74. s.appendLeft(setupOffsetEnd, "/* hoist static placeholder */");
  75. }
  76. if (scriptOffset !== void 0)
  77. normalScript.end();
  78. return getTransformResult(s, id);
  79. }
  80. // src/index.ts
  81. function resolveOption(options, framework) {
  82. const version = options.version || detectVueVersion();
  83. return {
  84. include: [REGEX_VUE_SFC, REGEX_SETUP_SFC].concat(
  85. version === 2 && framework === "webpack" ? REGEX_VUE_SUB : []
  86. ),
  87. ...options,
  88. version
  89. };
  90. }
  91. var name = "unplugin-vue-hoist-static";
  92. var src_default = createUnplugin(
  93. (userOptions = {}, { framework }) => {
  94. const options = resolveOption(userOptions, framework);
  95. const filter = createFilter(options);
  96. return {
  97. name,
  98. enforce: "pre",
  99. transformInclude(id) {
  100. return filter(id);
  101. },
  102. transform(code, id) {
  103. return transformHoistStatic(code, id);
  104. }
  105. };
  106. }
  107. );
  108. export {
  109. src_default
  110. };