index.js 2.4 KB

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