index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. const t = __importStar(require("@babel/types"));
  26. const template_1 = __importDefault(require("@babel/template"));
  27. const plugin_syntax_jsx_1 = __importDefault(require("@babel/plugin-syntax-jsx"));
  28. const helper_module_imports_1 = require("@babel/helper-module-imports");
  29. const transform_vue_jsx_1 = __importDefault(require("./transform-vue-jsx"));
  30. const sugar_fragment_1 = __importDefault(require("./sugar-fragment"));
  31. const hasJSX = (parentPath) => {
  32. let fileHasJSX = false;
  33. parentPath.traverse({
  34. JSXElement(path) {
  35. // skip ts error
  36. fileHasJSX = true;
  37. path.stop();
  38. },
  39. JSXFragment(path) {
  40. fileHasJSX = true;
  41. path.stop();
  42. },
  43. });
  44. return fileHasJSX;
  45. };
  46. const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;
  47. exports.default = ({ types }) => ({
  48. name: 'babel-plugin-jsx',
  49. inherits: plugin_syntax_jsx_1.default,
  50. visitor: Object.assign(Object.assign(Object.assign({}, transform_vue_jsx_1.default), sugar_fragment_1.default), { Program: {
  51. enter(path, state) {
  52. if (hasJSX(path)) {
  53. const importNames = [
  54. 'createVNode',
  55. 'Fragment',
  56. 'resolveComponent',
  57. 'withDirectives',
  58. 'vShow',
  59. 'vModelSelect',
  60. 'vModelText',
  61. 'vModelCheckbox',
  62. 'vModelRadio',
  63. 'vModelText',
  64. 'vModelDynamic',
  65. 'resolveDirective',
  66. 'mergeProps',
  67. 'createTextVNode',
  68. 'isVNode',
  69. ];
  70. if ((0, helper_module_imports_1.isModule)(path)) {
  71. // import { createVNode } from "vue";
  72. const importMap = {};
  73. importNames.forEach((name) => {
  74. state.set(name, () => {
  75. if (importMap[name]) {
  76. return types.cloneNode(importMap[name]);
  77. }
  78. const identifier = (0, helper_module_imports_1.addNamed)(path, name, 'vue', {
  79. ensureLiveReference: true,
  80. });
  81. importMap[name] = identifier;
  82. return identifier;
  83. });
  84. });
  85. const { enableObjectSlots = true } = state.opts;
  86. if (enableObjectSlots) {
  87. state.set('@vue/babel-plugin-jsx/runtimeIsSlot', () => {
  88. if (importMap.runtimeIsSlot) {
  89. return importMap.runtimeIsSlot;
  90. }
  91. const { name: isVNodeName } = state.get('isVNode')();
  92. const isSlot = path.scope.generateUidIdentifier('isSlot');
  93. const ast = template_1.default.ast `
  94. function ${isSlot.name}(s) {
  95. return typeof s === 'function' || (Object.prototype.toString.call(s) === '[object Object]' && !${isVNodeName}(s));
  96. }
  97. `;
  98. const lastImport = path.get('body')
  99. .filter((p) => p.isImportDeclaration())
  100. .pop();
  101. if (lastImport) {
  102. lastImport.insertAfter(ast);
  103. }
  104. importMap.runtimeIsSlot = isSlot;
  105. return isSlot;
  106. });
  107. }
  108. }
  109. else {
  110. // var _vue = require('vue');
  111. let sourceName;
  112. importNames.forEach((name) => {
  113. state.set(name, () => {
  114. if (!sourceName) {
  115. sourceName = (0, helper_module_imports_1.addNamespace)(path, 'vue', {
  116. ensureLiveReference: true,
  117. });
  118. }
  119. return t.memberExpression(sourceName, t.identifier(name));
  120. });
  121. });
  122. const helpers = {};
  123. const { enableObjectSlots = true } = state.opts;
  124. if (enableObjectSlots) {
  125. state.set('@vue/babel-plugin-jsx/runtimeIsSlot', () => {
  126. if (helpers.runtimeIsSlot) {
  127. return helpers.runtimeIsSlot;
  128. }
  129. const isSlot = path.scope.generateUidIdentifier('isSlot');
  130. const { object: objectName } = state.get('isVNode')();
  131. const ast = template_1.default.ast `
  132. function ${isSlot.name}(s) {
  133. return typeof s === 'function' || (Object.prototype.toString.call(s) === '[object Object]' && !${objectName.name}.isVNode(s));
  134. }
  135. `;
  136. const nodePaths = path.get('body');
  137. const lastImport = nodePaths
  138. .filter((p) => p.isVariableDeclaration()
  139. && p.node.declarations.some((d) => { var _a; return ((_a = d.id) === null || _a === void 0 ? void 0 : _a.name) === sourceName.name; }))
  140. .pop();
  141. if (lastImport) {
  142. lastImport.insertAfter(ast);
  143. }
  144. return isSlot;
  145. });
  146. }
  147. }
  148. const { opts: { pragma = '' }, file, } = state;
  149. if (pragma) {
  150. state.set('createVNode', () => t.identifier(pragma));
  151. }
  152. if (file.ast.comments) {
  153. for (const comment of file.ast.comments) {
  154. const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value);
  155. if (jsxMatches) {
  156. state.set('createVNode', () => t.identifier(jsxMatches[1]));
  157. }
  158. }
  159. }
  160. }
  161. },
  162. exit(path) {
  163. const body = path.get('body');
  164. const specifiersMap = new Map();
  165. body
  166. .filter((nodePath) => t.isImportDeclaration(nodePath.node)
  167. && nodePath.node.source.value === 'vue')
  168. .forEach((nodePath) => {
  169. const { specifiers } = nodePath.node;
  170. let shouldRemove = false;
  171. specifiers.forEach((specifier) => {
  172. if (!specifier.loc
  173. && t.isImportSpecifier(specifier)
  174. && t.isIdentifier(specifier.imported)) {
  175. specifiersMap.set(specifier.imported.name, specifier);
  176. shouldRemove = true;
  177. }
  178. });
  179. if (shouldRemove) {
  180. nodePath.remove();
  181. }
  182. });
  183. const specifiers = [...specifiersMap.keys()].map((imported) => specifiersMap.get(imported));
  184. if (specifiers.length) {
  185. path.unshiftContainer('body', t.importDeclaration(specifiers, t.stringLiteral('vue')));
  186. }
  187. },
  188. } }),
  189. });
  190. //# sourceMappingURL=index.js.map