index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. exports.CodeGenerator = void 0;
  7. var _sourceMap = _interopRequireDefault(require("./source-map"));
  8. var _printer = _interopRequireDefault(require("./printer"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  11. var Generator = function (_Printer) {
  12. _inheritsLoose(Generator, _Printer);
  13. function Generator(ast, opts, code) {
  14. var _this;
  15. if (opts === void 0) {
  16. opts = {};
  17. }
  18. var format = normalizeOptions(code, opts);
  19. var map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null;
  20. _this = _Printer.call(this, format, map) || this;
  21. _this.ast = ast;
  22. return _this;
  23. }
  24. var _proto = Generator.prototype;
  25. _proto.generate = function generate() {
  26. return _Printer.prototype.generate.call(this, this.ast);
  27. };
  28. return Generator;
  29. }(_printer.default);
  30. function normalizeOptions(code, opts) {
  31. var format = {
  32. auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
  33. auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
  34. shouldPrintComment: opts.shouldPrintComment,
  35. retainLines: opts.retainLines,
  36. retainFunctionParens: opts.retainFunctionParens,
  37. comments: opts.comments == null || opts.comments,
  38. compact: opts.compact,
  39. minified: opts.minified,
  40. concise: opts.concise,
  41. jsonCompatibleStrings: opts.jsonCompatibleStrings,
  42. indent: {
  43. adjustMultilineComment: true,
  44. style: " ",
  45. base: 0
  46. }
  47. };
  48. if (format.minified) {
  49. format.compact = true;
  50. format.shouldPrintComment = format.shouldPrintComment || function () {
  51. return format.comments;
  52. };
  53. } else {
  54. format.shouldPrintComment = format.shouldPrintComment || function (value) {
  55. return format.comments || value.indexOf("@license") >= 0 || value.indexOf("@preserve") >= 0;
  56. };
  57. }
  58. if (format.compact === "auto") {
  59. format.compact = code.length > 500000;
  60. if (format.compact) {
  61. console.error("[BABEL] Note: The code generator has deoptimised the styling of " + (opts.filename + " as it exceeds the max of " + "500KB" + "."));
  62. }
  63. }
  64. if (format.compact) {
  65. format.indent.adjustMultilineComment = false;
  66. }
  67. return format;
  68. }
  69. var CodeGenerator = function () {
  70. function CodeGenerator(ast, opts, code) {
  71. this._generator = new Generator(ast, opts, code);
  72. }
  73. var _proto2 = CodeGenerator.prototype;
  74. _proto2.generate = function generate() {
  75. return this._generator.generate();
  76. };
  77. return CodeGenerator;
  78. }();
  79. exports.CodeGenerator = CodeGenerator;
  80. function _default(ast, opts, code) {
  81. var gen = new Generator(ast, opts, code);
  82. return gen.generate();
  83. }