swc_ecma_codegen/
config.rs

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