UNPKG

2.99 kBTypeScriptView Raw
1import * as t from "babel-types";
2
3/**
4 * Turns an AST into code, maintaining sourcemaps, user preferences, and valid output.
5 * @param ast - the abstract syntax tree from which to generate output code.
6 * @param opts - used for specifying options for code generation.
7 * @param code - the original source code, used for source maps.
8 * @returns - an object containing the output code and source map.
9 */
10export default function generate(
11 ast: t.Node,
12 opts?: GeneratorOptions,
13 code?: string | { [filename: string]: string },
14): GeneratorResult;
15
16export interface GeneratorOptions {
17 /**
18 * Optional string to add as a block comment at the start of the output file.
19 */
20 auxiliaryCommentBefore?: string | undefined;
21
22 /**
23 * Optional string to add as a block comment at the end of the output file.
24 */
25 auxiliaryCommentAfter?: string | undefined;
26
27 /**
28 * Function that takes a comment (as a string) and returns true if the comment should be included in the output.
29 * By default, comments are included if `opts.comments` is `true` or if `opts.minifed` is `false` and the comment
30 * contains `@preserve` or `@license`.
31 */
32 shouldPrintComment?(comment: string): boolean;
33
34 /**
35 * Attempt to use the same line numbers in the output code as in the source code (helps preserve stack traces).
36 * Defaults to `false`.
37 */
38 retainLines?: boolean | undefined;
39
40 /**
41 * Should comments be included in output? Defaults to `true`.
42 */
43 comments?: boolean | undefined;
44
45 /**
46 * Set to true to avoid adding whitespace for formatting. Defaults to the value of `opts.minified`.
47 */
48 compact?: boolean | "auto" | undefined;
49
50 /**
51 * Should the output be minified. Defaults to `false`.
52 */
53 minified?: boolean | undefined;
54
55 /**
56 * Set to true to reduce whitespace (but not as much as opts.compact). Defaults to `false`.
57 */
58 concise?: boolean | undefined;
59
60 /**
61 * The type of quote to use in the output. If omitted, autodetects based on `ast.tokens`.
62 */
63 quotes?: "single" | "double" | undefined;
64
65 /**
66 * Used in warning messages
67 */
68 filename?: string | undefined;
69
70 /**
71 * Enable generating source maps. Defaults to `false`.
72 */
73 sourceMaps?: boolean | undefined;
74
75 /**
76 * The filename of the generated code that the source map will be associated with.
77 */
78 sourceMapTarget?: string | undefined;
79
80 /**
81 * A root for all relative URLs in the source map.
82 */
83 sourceRoot?: string | undefined;
84
85 /**
86 * The filename for the source code (i.e. the code in the `code` argument).
87 * This will only be used if `code` is a string.
88 */
89 sourceFileName?: string | undefined;
90
91 /**
92 * Set to true to run jsesc with "json": true to print "\u00A9" vs. "©";
93 */
94 jsonCompatibleStrings?: boolean | undefined;
95}
96
97export interface GeneratorResult {
98 map: {};
99 code: string;
100}