forked from trunk-rs/trunk
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmod.rs
409 lines (379 loc) · 13.7 KB
/
mod.rs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
mod copy_dir;
#[cfg(test)]
mod copy_dir_test;
mod copy_file;
#[cfg(test)]
mod copy_file_test;
mod css;
mod html;
mod icon;
mod inline;
mod js;
mod rust;
mod sass;
mod tailwind_css;
use crate::common::path_exists;
use crate::config::RtcBuild;
use crate::pipelines::copy_dir::{CopyDir, CopyDirOutput};
use crate::pipelines::copy_file::{CopyFile, CopyFileOutput};
use crate::pipelines::css::{Css, CssOutput};
use crate::pipelines::icon::{Icon, IconOutput};
use crate::pipelines::inline::{Inline, InlineOutput};
use crate::pipelines::js::{Js, JsOutput};
use crate::pipelines::rust::{RustApp, RustAppOutput};
use crate::pipelines::sass::{Sass, SassOutput};
use crate::pipelines::tailwind_css::{TailwindCss, TailwindCssOutput};
use crate::processing::minify::{minify_css, minify_js};
use anyhow::{bail, ensure, Context, Result};
pub use html::HtmlPipeline;
use minify_js::TopLevelMode;
use nipper::Document;
use oxipng::Options;
use serde::Deserialize;
use std::collections::HashMap;
use std::ffi::OsString;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::fs;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
const ATTR_INLINE: &str = "data-inline";
const ATTR_HREF: &str = "href";
const ATTR_SRC: &str = "src";
const ATTR_TYPE: &str = "type";
const ATTR_REL: &str = "rel";
const ATTR_MINIFY: &str = "data-no-minify";
const ATTR_TARGET_PATH: &str = "data-target-path";
const SNIPPETS_DIR: &str = "snippets";
const TRUNK_ID: &str = "data-trunk-id";
const PNG_OPTIMIZATION_LEVEL: u8 = 6;
/// A mapping of all attrs associated with a specific `<link data-trunk .../>` element.
pub type Attrs = HashMap<String, String>;
/// A reference to a trunk asset.
pub enum TrunkAssetReference {
Link(Attrs),
Script(Attrs),
}
/// A model of all of the supported Trunk asset links expressed in the source HTML as
/// `<trunk-link/>` elements.
///
/// Trunk will remove all `<trunk-link .../>` elements found in the HTML. It is the responsibility
/// of each pipeline to implement a pipeline finalizer method for its pipeline output in order to
/// update the finalized HTML for asset links and the like.
#[allow(clippy::large_enum_variant)]
pub enum TrunkAsset {
Css(Css),
Sass(Sass),
TailwindCss(TailwindCss),
Js(Js),
Icon(Icon),
Inline(Inline),
CopyFile(CopyFile),
CopyDir(CopyDir),
RustApp(RustApp),
}
impl TrunkAsset {
/// Construct a new instance.
pub async fn from_html(
cfg: Arc<RtcBuild>,
html_dir: Arc<PathBuf>,
ignore_chan: Option<mpsc::Sender<PathBuf>>,
reference: TrunkAssetReference,
id: usize,
) -> Result<Self> {
match reference {
TrunkAssetReference::Link(attrs) => {
let rel = attrs.get(ATTR_REL).context(
"all <link data-trunk .../> elements must have a `rel` attribute indicating \
the asset type",
)?;
Ok(match rel.as_str() {
Sass::TYPE_SASS | Sass::TYPE_SCSS => {
Self::Sass(Sass::new(cfg, html_dir, attrs, id).await?)
}
Icon::TYPE_ICON => Self::Icon(Icon::new(cfg, html_dir, attrs, id).await?),
Inline::TYPE_INLINE => Self::Inline(Inline::new(html_dir, attrs, id).await?),
Css::TYPE_CSS => Self::Css(Css::new(cfg, html_dir, attrs, id).await?),
CopyFile::TYPE_COPY_FILE => {
Self::CopyFile(CopyFile::new(cfg, html_dir, attrs, id).await?)
}
CopyDir::TYPE_COPY_DIR => {
Self::CopyDir(CopyDir::new(cfg, html_dir, attrs, id).await?)
}
RustApp::TYPE_RUST_APP => {
Self::RustApp(RustApp::new(cfg, html_dir, ignore_chan, attrs, id).await?)
}
TailwindCss::TYPE_TAILWIND_CSS => {
Self::TailwindCss(TailwindCss::new(cfg, html_dir, attrs, id).await?)
}
_ => bail!(
r#"unknown <link data-trunk .../> attr value `rel="{}"`; please ensure the value is lowercase and is a supported asset type"#,
rel
),
})
}
TrunkAssetReference::Script(attrs) => {
Ok(Self::Js(Js::new(cfg, html_dir, attrs, id).await?))
}
}
}
/// Spawn the build pipeline for this asset.
pub fn spawn(self) -> JoinHandle<Result<TrunkAssetPipelineOutput>> {
match self {
Self::Css(inner) => inner.spawn(),
Self::Sass(inner) => inner.spawn(),
Self::TailwindCss(inner) => inner.spawn(),
Self::Js(inner) => inner.spawn(),
Self::Icon(inner) => inner.spawn(),
Self::Inline(inner) => inner.spawn(),
Self::CopyFile(inner) => inner.spawn(),
Self::CopyDir(inner) => inner.spawn(),
Self::RustApp(inner) => inner.spawn(),
}
}
}
/// The output of a `<trunk-link/>` asset pipeline.
pub enum TrunkAssetPipelineOutput {
Css(CssOutput),
Sass(SassOutput),
TailwindCss(TailwindCssOutput),
Js(JsOutput),
Icon(IconOutput),
Inline(InlineOutput),
CopyFile(CopyFileOutput),
CopyDir(CopyDirOutput),
RustApp(RustAppOutput),
}
impl TrunkAssetPipelineOutput {
pub async fn finalize(self, dom: &mut Document) -> Result<()> {
match self {
TrunkAssetPipelineOutput::Css(out) => out.finalize(dom).await,
TrunkAssetPipelineOutput::Sass(out) => out.finalize(dom).await,
TrunkAssetPipelineOutput::TailwindCss(out) => out.finalize(dom).await,
TrunkAssetPipelineOutput::Js(out) => out.finalize(dom).await,
TrunkAssetPipelineOutput::Icon(out) => out.finalize(dom).await,
TrunkAssetPipelineOutput::Inline(out) => out.finalize(dom).await,
TrunkAssetPipelineOutput::CopyFile(out) => out.finalize(dom).await,
TrunkAssetPipelineOutput::CopyDir(out) => out.finalize(dom).await,
TrunkAssetPipelineOutput::RustApp(out) => out.finalize(dom).await,
}
}
}
pub enum AssetFileType {
Css,
Icon(ImageType),
Js,
Mjs,
Other,
}
pub enum ImageType {
Png,
Other,
}
/// An asset file to be processed by some build pipeline.
pub struct AssetFile {
/// The canonicalized path to the target file.
pub path: PathBuf,
/// The name of the file itself.
pub file_name: OsString,
/// The file stem of the asset file.
pub file_stem: OsString,
/// The extension of the file.
pub ext: Option<String>,
}
impl AssetFile {
/// Create a new instance.
///
/// The given path will be validated to ensure the following:
/// - that the full canonicalized path points to a file on the FS.
/// - that the file has a filename.
/// - that the file has an extension.
///
/// Any errors returned from this constructor indicate that one of these invariants was not
/// upheld.
pub async fn new(rel_dir: &Path, mut path: PathBuf) -> Result<Self> {
// If the given path is not absolute, then we join it with the directory from which the
// relative path should be based.
if !path.is_absolute() {
path = rel_dir.join(path);
}
// Take the path to referenced resource, if it is actually an FS path, then we continue.
let path = fs::canonicalize(&path)
.await
.with_context(|| format!("error getting canonical path for {:?}", &path))?;
ensure!(
path_exists(&path).await?,
"target file does not appear to exist on disk {:?}",
&path
);
let file_name = match path.file_name() {
Some(file_name) => file_name.to_owned(),
None => bail!("asset has no file name {:?}", &path),
};
let file_stem = match path.file_stem() {
Some(file_stem) => file_stem.to_owned(),
None => bail!("asset has no file name stem {:?}", &path),
};
let ext = path
.extension()
.map(|ext| ext.to_owned().to_string_lossy().to_string());
Ok(Self {
path,
file_name,
file_stem,
ext,
})
}
/// Copy this asset to the target dir. If hashing is enabled, create a hash from the file
/// contents and include it as hex string in the destination file name.
///
/// The base file name (stripped path, without any parent folders) is returned if the operation
/// was successful.
pub async fn copy(
&self,
to_dir: &Path,
with_hash: bool,
minify: bool,
file_type: AssetFileType,
) -> Result<String> {
let mut bytes = fs::read(&self.path)
.await
.with_context(|| format!("error reading file for copying {:?}", &self.path))?;
bytes = if minify {
match file_type {
AssetFileType::Css => minify_css(bytes),
AssetFileType::Icon(image_type) => match image_type {
ImageType::Png => oxipng::optimize_from_memory(
bytes.as_ref(),
&Options::from_preset(PNG_OPTIMIZATION_LEVEL),
)
.with_context(|| format!("error optimizing PNG {:?}", &self.path))?,
ImageType::Other => bytes,
},
AssetFileType::Js => minify_js(bytes, TopLevelMode::Global),
AssetFileType::Mjs => minify_js(bytes, TopLevelMode::Module),
_ => bytes,
}
} else {
bytes
};
let file_name = if with_hash {
format!(
"{}-{:x}.{}",
&self.file_stem.to_string_lossy(),
seahash::hash(bytes.as_ref()),
&self.ext.as_deref().unwrap_or_default()
)
} else {
self.file_name.to_string_lossy().into_owned()
};
let file_path = to_dir.join(&file_name);
fs::write(&file_path, bytes)
.await
.with_context(|| format!("error copying file {:?} to {:?}", &self.path, &file_path))?;
Ok(file_name)
}
/// Read the content of this asset to a String.
pub async fn read_to_string(&self) -> Result<String> {
fs::read_to_string(&self.path)
.await
.with_context(|| format!("error reading file {:?} to string", self.path))
}
}
/// A stage in the build process.
///
/// This is used to specify when a hook will run.
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PipelineStage {
/// The stage before asset builds are executed.
PreBuild,
/// The stage where all asset builds are executed.
Build,
/// The stage after asset builds are executed.
PostBuild,
}
/// Create the CSS selector for selecting a trunk link by ID.
fn trunk_id_selector(id: usize) -> String {
format!(r#"link[{}="{}"]"#, TRUNK_ID, id)
}
/// Create the CSS selector for selecting a trunk script by ID.
fn trunk_script_id_selector(id: usize) -> String {
format!(r#"script[{}="{}"]"#, TRUNK_ID, id)
}
/// A Display impl that writes out a hashmap of attributes into an html tag.
///
/// Details:
///
/// - It begins with a space.
/// - Any values are HTML-escaped.
/// - It sorts the keys by name for deterministic results.
/// - It ignores the data-trunk attributes
/// - It ignores anything in the `exclude` list
/// - Values that are an empty string are written with the empty `<link ... disabled ... />` syntax
/// instead of `disabled=""`.
struct AttrWriter<'a> {
pub(self) attrs: &'a Attrs,
pub(self) exclude: &'a [&'a str],
}
impl<'a> AttrWriter<'a> {
/// Note: we additionally exclude `type="text/css"` etc on inline, because on a <style>
/// element it is a deprecated attribute.
pub(self) const EXCLUDE_CSS_INLINE: &'static [&'static str] = &[
TRUNK_ID,
ATTR_HREF,
ATTR_REL,
ATTR_INLINE,
ATTR_SRC,
ATTR_TYPE,
ATTR_MINIFY,
];
/// Whereas on link elements, the MIME type for css is A-OK. You can even specify a custom
/// MIME type.
pub(self) const EXCLUDE_CSS_LINK: &'static [&'static str] = &[
TRUNK_ID,
ATTR_HREF,
ATTR_REL,
ATTR_INLINE,
ATTR_SRC,
ATTR_MINIFY,
];
/// Attributes to ignore for <script> tags
pub(self) const EXCLUDE_SCRIPT: &'static [&'static str] = &[ATTR_SRC, ATTR_MINIFY];
pub(self) fn new(attrs: &'a Attrs, exclude: &'a [&'a str]) -> Self {
Self { attrs, exclude }
}
}
impl fmt::Display for AttrWriter<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut filtered: Vec<&str> = self
.attrs
.keys()
.map(|x| x.as_str())
.filter(|name| !name.starts_with("data-trunk"))
.filter(|name| !self.exclude.contains(name))
.collect();
// Sort for consistency
filtered.sort();
for name in filtered {
// Assume the name doesn't need to be escaped, as if we managed to parse it as HTML,
// then it's probably fine.
write!(f, " {name}")?;
let value = &self.attrs[name];
if !value.is_empty() {
let encoded = htmlescape::encode_attribute(value);
write!(f, "=\"{}\"", encoded)?;
}
}
Ok(())
}
}
/// Get the target path for an asset
fn data_target_path(attrs: &Attrs) -> Result<Option<PathBuf>> {
Ok(attrs
.get(ATTR_TARGET_PATH)
.map(|val| val.parse())
.transpose()?)
}