index.js 3.0 KB

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