index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _helperPluginUtils() {
  7. const data = require("@babel/helper-plugin-utils");
  8. _helperPluginUtils = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _core() {
  14. const data = require("@babel/core");
  15. _core = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _default = (0, _helperPluginUtils().declare)((api, options) => {
  21. api.assertVersion(7);
  22. const {
  23. loose
  24. } = options;
  25. let helperName = "taggedTemplateLiteral";
  26. if (loose) helperName += "Loose";
  27. function buildConcatCallExpressions(items) {
  28. let avail = true;
  29. return items.reduce(function (left, right) {
  30. let canBeInserted = _core().types.isLiteral(right);
  31. if (!canBeInserted && avail) {
  32. canBeInserted = true;
  33. avail = false;
  34. }
  35. if (canBeInserted && _core().types.isCallExpression(left)) {
  36. left.arguments.push(right);
  37. return left;
  38. }
  39. return _core().types.callExpression(_core().types.memberExpression(left, _core().types.identifier("concat")), [right]);
  40. });
  41. }
  42. return {
  43. name: "transform-template-literals",
  44. visitor: {
  45. TaggedTemplateExpression(path) {
  46. const {
  47. node
  48. } = path;
  49. const {
  50. quasi
  51. } = node;
  52. const strings = [];
  53. const raws = [];
  54. let isStringsRawEqual = true;
  55. for (const elem of quasi.quasis) {
  56. const {
  57. raw,
  58. cooked
  59. } = elem.value;
  60. const value = cooked == null ? path.scope.buildUndefinedNode() : _core().types.stringLiteral(cooked);
  61. strings.push(value);
  62. raws.push(_core().types.stringLiteral(raw));
  63. if (raw !== cooked) {
  64. isStringsRawEqual = false;
  65. }
  66. }
  67. const scope = path.scope.getProgramParent();
  68. const templateObject = scope.generateUidIdentifier("templateObject");
  69. const helperId = this.addHelper(helperName);
  70. const callExpressionInput = [_core().types.arrayExpression(strings)];
  71. if (!isStringsRawEqual) {
  72. callExpressionInput.push(_core().types.arrayExpression(raws));
  73. }
  74. const lazyLoad = _core().template.ast`
  75. function ${templateObject}() {
  76. const data = ${_core().types.callExpression(helperId, callExpressionInput)};
  77. ${templateObject} = function() { return data };
  78. return data;
  79. }
  80. `;
  81. scope.path.unshiftContainer("body", lazyLoad);
  82. path.replaceWith(_core().types.callExpression(node.tag, [_core().types.callExpression(_core().types.cloneNode(templateObject), []), ...quasi.expressions]));
  83. },
  84. TemplateLiteral(path) {
  85. const nodes = [];
  86. const expressions = path.get("expressions");
  87. let index = 0;
  88. for (const elem of path.node.quasis) {
  89. if (elem.value.cooked) {
  90. nodes.push(_core().types.stringLiteral(elem.value.cooked));
  91. }
  92. if (index < expressions.length) {
  93. const expr = expressions[index++];
  94. const node = expr.node;
  95. if (!_core().types.isStringLiteral(node, {
  96. value: ""
  97. })) {
  98. nodes.push(node);
  99. }
  100. }
  101. }
  102. const considerSecondNode = !loose || !_core().types.isStringLiteral(nodes[1]);
  103. if (!_core().types.isStringLiteral(nodes[0]) && considerSecondNode) {
  104. nodes.unshift(_core().types.stringLiteral(""));
  105. }
  106. let root = nodes[0];
  107. if (loose) {
  108. for (let i = 1; i < nodes.length; i++) {
  109. root = _core().types.binaryExpression("+", root, nodes[i]);
  110. }
  111. } else if (nodes.length > 1) {
  112. root = buildConcatCallExpressions(nodes);
  113. }
  114. path.replaceWith(root);
  115. }
  116. }
  117. };
  118. });
  119. exports.default = _default;