prest_build/
lib.rs

1use anyhow::Result;
2
3#[cfg(feature = "pwa")]
4mod pwa;
5#[cfg(feature = "pwa")]
6pub use pwa::*;
7
8#[cfg(feature = "typescript")]
9mod swc_bundler;
10#[cfg(feature = "typescript")]
11mod typescript {
12    pub fn bundle_ts() {
13        let exports = prest_npm::run().unwrap();
14        for (name, path) in exports {
15            crate::swc_bundler::bundle_js(&path, &name).unwrap();
16        }
17    }
18}
19#[cfg(feature = "typescript")]
20pub use typescript::bundle_ts;
21
22#[cfg(feature = "sass")]
23mod sass {
24    use std::{fs::write, path::Path};
25    pub fn bundle_sass(path: &str) -> anyhow::Result<()> {
26        let opts = grass::Options::default().style(grass::OutputStyle::Compressed);
27        let css = grass::from_path(path, &opts)?;
28        let scss_filename = Path::new(path).file_name().unwrap().to_str().unwrap();
29        let css_filename = scss_filename
30            .replace(".scss", ".css")
31            .replace(".sass", ".css");
32        let out_file = super::out_path(&css_filename);
33        write(out_file, css)?;
34        Ok(())
35    }
36}
37#[cfg(feature = "sass")]
38pub use sass::bundle_sass;
39
40pub use cfg_aliases::cfg_aliases;
41
42pub fn default_cfg_aliases() {
43    cfg_aliases! {
44        wasm: { target_arch = "wasm32" },
45        sw: { wasm },
46        not_wasm: { not(wasm) },
47        host: { not_wasm },
48        debug: { debug_assertions },
49        release: { not(debug_assertions) },
50    }
51}
52
53pub fn read_lib_name() -> Result<String> {
54    use toml::{Table, Value};
55    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")?;
56    let manifest_path = &format!("{manifest_dir}/Cargo.toml");
57    let manifest = std::fs::read_to_string(manifest_path)?;
58    let parsed = manifest.parse::<Table>()?;
59    let lib_name = if parsed.contains_key("lib") {
60        let Value::Table(lib_table) = &parsed["lib"] else {
61            panic!("should be unreachable");
62        };
63        if lib_table.contains_key("name") {
64            lib_table["name"].as_str().unwrap().to_owned()
65        } else {
66            parsed["package"]["name"].as_str().unwrap().to_owned()
67        }
68    } else {
69        parsed["package"]["name"].as_str().unwrap().to_owned()
70    };
71    Ok(lib_name.replace("-", "_"))
72}
73
74/// Utility that attempts to find the path of the current build's target path
75pub fn find_target_dir() -> Option<String> {
76    use std::{ffi::OsStr, path::PathBuf};
77    if let Some(target_dir) = std::env::var_os("CARGO_TARGET_DIR") {
78        let target_dir = PathBuf::from(target_dir);
79        if target_dir.is_absolute() {
80            if let Some(str) = target_dir.to_str() {
81                return Some(str.to_owned());
82            } else {
83                return None;
84            }
85        } else {
86            return None;
87        };
88    }
89
90    let mut dir = PathBuf::from(out_path(""));
91    loop {
92        if dir.join(".rustc_info.json").exists()
93            || dir.join("CACHEDIR.TAG").exists()
94            || dir.file_name() == Some(OsStr::new("target"))
95                && dir
96                    .parent()
97                    .map_or(false, |parent| parent.join("Cargo.toml").exists())
98        {
99            if let Some(str) = dir.to_str() {
100                return Some(str.to_owned());
101            } else {
102                return None;
103            }
104        }
105        if dir.pop() {
106            continue;
107        }
108        return None;
109    }
110}
111
112/// Utility for composition of paths to build artifacts
113pub fn out_path(filename: &str) -> String {
114    let dir = std::env::var("OUT_DIR").unwrap();
115    format!("{dir}/{filename}")
116}