index.js 2.4 KB

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