index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. class Generator extends _printer.default {
  11. constructor(ast, opts = {}, code) {
  12. const format = normalizeOptions(code, opts);
  13. const map = opts.sourceMaps ? new _sourceMap.default(opts, code) : null;
  14. super(format, map);
  15. this.ast = void 0;
  16. this.ast = ast;
  17. }
  18. generate() {
  19. return super.generate(this.ast);
  20. }
  21. }
  22. function normalizeOptions(code, opts) {
  23. const format = {
  24. auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
  25. auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
  26. shouldPrintComment: opts.shouldPrintComment,
  27. retainLines: opts.retainLines,
  28. retainFunctionParens: opts.retainFunctionParens,
  29. comments: opts.comments == null || opts.comments,
  30. compact: opts.compact,
  31. minified: opts.minified,
  32. concise: opts.concise,
  33. indent: {
  34. adjustMultilineComment: true,
  35. style: " ",
  36. base: 0
  37. },
  38. decoratorsBeforeExport: !!opts.decoratorsBeforeExport,
  39. jsescOption: Object.assign({
  40. quotes: "double",
  41. wrap: true
  42. }, opts.jsescOption),
  43. recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType
  44. };
  45. {
  46. format.jsonCompatibleStrings = opts.jsonCompatibleStrings;
  47. }
  48. if (format.minified) {
  49. format.compact = true;
  50. format.shouldPrintComment = format.shouldPrintComment || (() => format.comments);
  51. } else {
  52. format.shouldPrintComment = format.shouldPrintComment || (value => format.comments || value.indexOf("@license") >= 0 || value.indexOf("@preserve") >= 0);
  53. }
  54. if (format.compact === "auto") {
  55. format.compact = code.length > 500000;
  56. if (format.compact) {
  57. console.error("[BABEL] Note: The code generator has deoptimised the styling of " + `${opts.filename} as it exceeds the max of ${"500KB"}.`);
  58. }
  59. }
  60. if (format.compact) {
  61. format.indent.adjustMultilineComment = false;
  62. }
  63. return format;
  64. }
  65. class CodeGenerator {
  66. constructor(ast, opts, code) {
  67. this._generator = new Generator(ast, opts, code);
  68. }
  69. generate() {
  70. return this._generator.generate();
  71. }
  72. }
  73. exports.CodeGenerator = CodeGenerator;
  74. function _default(ast, opts, code) {
  75. const gen = new Generator(ast, opts, code);
  76. return gen.generate();
  77. }