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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use Path;
use ;
use ;
use crate::;
/// Serve `config.dir` without watching or building.
/// Optional `config.base` prefix.
///
/// # Examples
///
/// ```
/// use std::net::IpAddr;
///
/// use bundla::{ServerConfig, serve};
/// use tempfile::tempdir;
///
/// #[tokio::main]
/// async fn main() {
/// let dir = tempdir().unwrap();
/// std::fs::write(dir.path().join("index.html"), "hello from serve").unwrap();
///
/// let config = ServerConfig {
/// dir: dir.path().into(),
/// ip: IpAddr::from([127, 0, 0, 1]),
/// port: 0,
/// headers: vec![],
/// index: "index.html".into(),
/// base: String::new(),
/// };
/// let server = serve(&config).await.unwrap();
/// let url = server.url.clone();
/// let handle = tokio::spawn(async move { server.run().await });
///
/// let body = reqwest::get(&url).await.unwrap().text().await.unwrap();
/// assert!(body.contains("hello from serve"));
///
/// handle.abort();
/// }
/// ```
pub async
/// Watch and serve:
/// - watch `src`,
/// - build it into `config.dir` on startup and on every change,
/// - serve it with live reload.
///
/// `options` selects the build profile.
/// `filter` narrows the build to files whose name matches an item; see
/// [`watch`](crate::watch) for the matching rules. An empty `filter`
/// watches every file.
/// `config.base` is ignored and overwritten by `options.base`
/// so the served prefix matches the build prefix.
///
/// Runtime logs are reported on `tx_log`.
///
/// This is the [`watch`](crate::watch) with a server on top.
/// On every successful build the connected browsers reload:
/// - hot-reload for css when filenames are stable,
/// - full page reload otherwise.
///
/// A failed build keeps the previous build.
/// The reload script is served, never written to the output files.
///
/// # Examples
///
/// ```
/// use std::net::IpAddr;
/// use std::path::Path;
///
/// use bundla::{BuildOptions, ServerConfig, watch_serve};
/// use tempfile::tempdir;
///
/// #[tokio::main]
/// async fn main() {
/// let dir = tempdir().unwrap();
/// let src = dir.path().join("src");
/// std::fs::create_dir_all(src.join("styles")).unwrap();
/// std::fs::write(src.join("styles/main.css"), "body { color: black; }\n").unwrap();
/// std::fs::write(
/// src.join("index.html"),
/// "<html><head><link rel=\"stylesheet\" href=\"./styles/main.css\"></head>\
/// <body><h1>hi</h1></body></html>",
/// )
/// .unwrap();
///
/// let config = ServerConfig {
/// dir: dir.path().join("dev"),
/// ip: IpAddr::from([127, 0, 0, 1]),
/// port: 0,
/// headers: vec![],
/// index: "index.html".into(),
/// base: String::new(),
/// };
///
/// let (tx_log, _rx_log) = tokio::sync::broadcast::channel(100);
/// let server = watch_serve(Path::new(&src), &config, &BuildOptions::DEV, tx_log, &[])
/// .await
/// .unwrap();
/// let url = server.url.clone();
/// let handle = tokio::spawn(async move { server.run().await });
///
/// let body = reqwest::get(&url).await.unwrap().text().await.unwrap();
/// assert!(body.contains("<h1>hi</h1>"));
/// assert!(body.contains("main.css"));
///
/// handle.abort();
/// }
/// ```
pub async