1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use derivative::Derivative;
use self::file_name::FileNameTemplate;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum InternalModuleFormat {
Esm,
Cjs,
}
impl InternalModuleFormat {
pub fn is_es(self) -> bool {
self == InternalModuleFormat::Esm
}
pub fn is_cjs(self) -> bool {
self == InternalModuleFormat::Cjs
}
}
impl TryFrom<&str> for InternalModuleFormat {
type Error = String;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"esm" => Ok(InternalModuleFormat::Esm),
"cjs" => Ok(InternalModuleFormat::Cjs),
_ => Err(format!("Invalid module format: {}", value)),
}
}
}
#[derive(Derivative)]
#[derivative(Debug)]
pub struct OutputOptions {
pub entry_file_names: FileNameTemplate,
pub chunk_file_names: FileNameTemplate,
pub dir: Option<String>,
pub format: InternalModuleFormat,
}
impl Default for OutputOptions {
fn default() -> Self {
Self {
entry_file_names: FileNameTemplate::from("[name].js".to_string()),
chunk_file_names: FileNameTemplate::from("[name]-[hash].js".to_string()),
dir: None,
format: InternalModuleFormat::Esm,
}
}
}
pub mod file_name {
#[derive(Debug)]
pub struct FileNameTemplate {
template: String,
}
impl FileNameTemplate {
pub fn new(template: String) -> Self {
Self { template }
}
}
impl From<String> for FileNameTemplate {
fn from(template: String) -> Self {
Self { template }
}
}
#[derive(Debug, Default)]
pub struct RenderOptions<'me> {
pub name: Option<&'me str>,
}
impl FileNameTemplate {
pub fn render(&self, options: RenderOptions) -> String {
let mut tmp = self.template.clone();
if let Some(name) = options.name {
tmp = tmp.replace("[name]", name);
}
tmp
}
}
}