index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _assert = require('assert'); var _assert2 = _interopRequireDefault(_assert);
  2. var _Plugin = require('./Plugin'); var _Plugin2 = _interopRequireDefault(_Plugin);
  3. exports. default = function({types}) {
  4. let plugins = null;
  5. global.__clearBabelAntdPlugin = () => {
  6. plugins = null;
  7. };
  8. function applyInstance(method, args, context) {
  9. for (const plugin of plugins) {
  10. if (plugin[method]) {
  11. plugin[method].apply(plugin, [...args, context]);
  12. }
  13. }
  14. }
  15. const Program = {
  16. enter(path, {opts = {}}) {
  17. if (!plugins) {
  18. if (Array.isArray(opts)) {
  19. plugins = opts.map(({
  20. libraryName,
  21. libraryDirectory,
  22. style,
  23. styleLibraryDirectory,
  24. customStyleName,
  25. camel2DashComponentName,
  26. camel2UnderlineComponentName,
  27. fileName,
  28. customName,
  29. transformToDefaultImport
  30. }, index) => {
  31. _assert2.default.call(void 0, libraryName, "libraryName should be provided");
  32. return new (0, _Plugin2.default)(libraryName, libraryDirectory, style, styleLibraryDirectory, customStyleName, camel2DashComponentName, camel2UnderlineComponentName, fileName, customName, transformToDefaultImport, types, index);
  33. });
  34. } else {
  35. _assert2.default.call(void 0, opts.libraryName, "libraryName should be provided");
  36. plugins = [
  37. new (0, _Plugin2.default)(opts.libraryName, opts.libraryDirectory, opts.style, opts.styleLibraryDirectory, opts.customStyleName, opts.camel2DashComponentName, opts.camel2UnderlineComponentName, opts.fileName, opts.customName, opts.transformToDefaultImport, types)
  38. ];
  39. }
  40. }
  41. applyInstance("ProgramEnter", arguments, this);
  42. },
  43. exit() {
  44. applyInstance("ProgramExit", arguments, this);
  45. }
  46. };
  47. const methods = [
  48. "ImportDeclaration",
  49. "CallExpression",
  50. "MemberExpression",
  51. "Property",
  52. "VariableDeclarator",
  53. "ArrayExpression",
  54. "LogicalExpression",
  55. "ConditionalExpression",
  56. "IfStatement",
  57. "ExpressionStatement",
  58. "ReturnStatement",
  59. "ExportDefaultDeclaration",
  60. "BinaryExpression",
  61. "NewExpression",
  62. "ClassDeclaration",
  63. "SwitchStatement",
  64. "SwitchCase"
  65. ];
  66. const ret = {
  67. visitor: {Program}
  68. };
  69. for (const method of methods) {
  70. ret.visitor[method] = function() {
  71. applyInstance(method, arguments, ret.visitor);
  72. };
  73. }
  74. return ret;
  75. }