index.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. jsonCompatibleStrings: opts.jsonCompatibleStrings,
  34. indent: {
  35. adjustMultilineComment: true,
  36. style: " ",
  37. base: 0
  38. },
  39. decoratorsBeforeExport: !!opts.decoratorsBeforeExport,
  40. jsescOption: Object.assign({
  41. quotes: "double",
  42. wrap: true
  43. }, opts.jsescOption),
  44. recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType
  45. };
  46. if (format.minified) {
  47. format.compact = true;
  48. format.shouldPrintComment = format.shouldPrintComment || (() => format.comments);
  49. } else {
  50. format.shouldPrintComment = format.shouldPrintComment || (value => format.comments || value.indexOf("@license") >= 0 || value.indexOf("@preserve") >= 0);
  51. }
  52. if (format.compact === "auto") {
  53. format.compact = code.length > 500000;
  54. if (format.compact) {
  55. console.error("[BABEL] Note: The code generator has deoptimised the styling of " + `${opts.filename} as it exceeds the max of ${"500KB"}.`);
  56. }
  57. }
  58. if (format.compact) {
  59. format.indent.adjustMultilineComment = false;
  60. }
  61. return format;
  62. }
  63. class CodeGenerator {
  64. constructor(ast, opts, code) {
  65. this._generator = new Generator(ast, opts, code);
  66. }
  67. generate() {
  68. return this._generator.generate();
  69. }
  70. }
  71. exports.CodeGenerator = CodeGenerator;
  72. function _default(ast, opts, code) {
  73. const gen = new Generator(ast, opts, code);
  74. return gen.generate();
  75. }