index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 plugin_syntax_jsx_1 = __importDefault(require("@babel/plugin-syntax-jsx"));
  26. const t = __importStar(require("@babel/types"));
  27. const transform_vue_jsx_1 = __importDefault(require("./transform-vue-jsx"));
  28. const sugar_fragment_1 = __importDefault(require("./sugar-fragment"));
  29. const utils_1 = require("./utils");
  30. exports.default = () => ({
  31. name: 'babel-plugin-jsx',
  32. inherits: plugin_syntax_jsx_1.default,
  33. visitor: Object.assign(Object.assign({ Program: {
  34. exit(path, state) {
  35. const helpers = state.get(utils_1.JSX_HELPER_KEY);
  36. if (!helpers) {
  37. return;
  38. }
  39. const body = path.get('body');
  40. const specifierNames = new Set();
  41. body
  42. .filter((nodePath) => t.isImportDeclaration(nodePath.node)
  43. && nodePath.node.source.value === 'vue')
  44. .forEach((nodePath) => {
  45. let shouldKeep = false;
  46. const newSpecifiers = nodePath.node.specifiers
  47. .filter((specifier) => {
  48. if (t.isImportSpecifier(specifier)) {
  49. const { imported, local } = specifier;
  50. if (local.name === imported.name) {
  51. specifierNames.add(imported.name);
  52. return false;
  53. }
  54. return true;
  55. }
  56. if (t.isImportNamespaceSpecifier(specifier)) {
  57. // should keep when `import * as Vue from 'vue'`
  58. shouldKeep = true;
  59. }
  60. return false;
  61. });
  62. if (newSpecifiers.length) {
  63. nodePath.replaceWith(t.importDeclaration(newSpecifiers, t.stringLiteral('vue')));
  64. }
  65. else if (!shouldKeep) {
  66. nodePath.remove();
  67. }
  68. });
  69. const importedHelperKeys = new Set([...specifierNames, ...helpers]);
  70. const specifiers = [...importedHelperKeys].map((imported) => t.importSpecifier(t.identifier(imported), t.identifier(imported)));
  71. const expression = t.importDeclaration(specifiers, t.stringLiteral('vue'));
  72. path.unshiftContainer('body', expression);
  73. },
  74. } }, transform_vue_jsx_1.default()), sugar_fragment_1.default()),
  75. });