UNPKG

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