swc_ecma_codegen/
config.rs

1#[cfg(feature = "serde-impl")]
2use serde::{Deserialize, Serialize};
3use swc_ecma_ast::EsVersion;
4
5#[cfg(feature = "serde-impl")]
6const fn true_by_default() -> bool {
7    true
8}
9
10#[derive(Debug, Clone, Copy)]
11#[cfg_attr(feature = "serde-impl", derive(Serialize, Deserialize))]
12#[cfg_attr(feature = "serde-impl", serde(rename_all = "camelCase"))]
13#[non_exhaustive]
14pub struct Config {
15    /// The target runtime environment.
16    ///
17    /// This defaults to [EsVersion::latest] because it preserves input as much
18    /// as possible.
19    ///
20    /// Note: This does not verifies if output is valid for the target runtime.
21    /// e.g. `const foo = 1;` with [EsVersion::Es3] will emitted as `const foo =
22    /// 1` without verification.
23    /// This is because it's not a concern of the code generator.
24    #[cfg_attr(feature = "serde-impl", serde(default = "EsVersion::latest"))]
25    pub target: EsVersion,
26
27    /// Forces the code generator to use only ascii characters.
28    ///
29    /// This is useful for environments that do not support unicode.
30    #[cfg_attr(feature = "serde-impl", serde(default))]
31    pub ascii_only: bool,
32
33    #[cfg_attr(feature = "serde-impl", serde(default))]
34    pub minify: bool,
35
36    /// If true, the code generator will emit the lastest semicolon.
37    ///
38    /// Defaults to `false`.
39    #[cfg_attr(feature = "serde-impl", serde(default))]
40    pub omit_last_semi: bool,
41
42    #[cfg_attr(feature = "serde-impl", serde(default))]
43    pub emit_assert_for_import_attributes: bool,
44
45    #[cfg_attr(feature = "serde-impl", serde(default))]
46    pub inline_script: bool,
47
48    /// Transform escaped newline `'\n'` of `TplElement` to a newline character
49    /// to reduce text size of javascript code
50    ///
51    /// Defaults to `true`
52    #[cfg_attr(feature = "serde-impl", serde(default = "true_by_default"))]
53    pub reduce_escaped_newline: bool,
54}
55
56impl Default for Config {
57    fn default() -> Self {
58        Self {
59            target: EsVersion::latest(),
60            minify: false,
61            ascii_only: false,
62            omit_last_semi: false,
63            emit_assert_for_import_attributes: false,
64            inline_script: false,
65            reduce_escaped_newline: true,
66        }
67    }
68}
69
70impl Config {
71    pub fn with_target(mut self, target: EsVersion) -> Self {
72        self.target = target;
73        self
74    }
75
76    pub fn with_minify(mut self, minify: bool) -> Self {
77        self.minify = minify;
78        self
79    }
80
81    pub fn with_ascii_only(mut self, ascii_only: bool) -> Self {
82        self.ascii_only = ascii_only;
83        self
84    }
85
86    pub fn with_omit_last_semi(mut self, omit_last_semi: bool) -> Self {
87        self.omit_last_semi = omit_last_semi;
88        self
89    }
90
91    pub fn with_emit_assert_for_import_attributes(
92        mut self,
93        emit_assert_for_import_attributes: bool,
94    ) -> Self {
95        self.emit_assert_for_import_attributes = emit_assert_for_import_attributes;
96        self
97    }
98
99    pub fn with_inline_script(mut self, inline_script: bool) -> Self {
100        self.inline_script = inline_script;
101        self
102    }
103
104    /// Transform escaped newline `'\n'` of `TplElement` to a newline character
105    /// to reduce text size of javascript code
106    ///
107    /// Defaults to `true`
108    pub fn with_reduce_escaped_newline(mut self, reduce_escaped_newline: bool) -> Self {
109        self.reduce_escaped_newline = reduce_escaped_newline;
110        self
111    }
112}