Spaces:
Runtime error
Runtime error
neon_arch
commited on
Commit
•
da03037
1
Parent(s):
b123fbb
⚡️ perf: add code minify `js` and `css` during the initial build process when the `pkg_env` enviroment variable is set to `prod` (#359)
Browse files
build.rs
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
//! A build module of the application which minifies the project's css and js files on build which
|
2 |
+
//! helps reduce the initial page by loading the files faster.
|
3 |
+
|
4 |
+
#![forbid(unsafe_code, clippy::panic)]
|
5 |
+
#![deny(missing_docs, clippy::missing_docs_in_private_items, clippy::perf)]
|
6 |
+
#![warn(clippy::cognitive_complexity, rust_2018_idioms)]
|
7 |
+
|
8 |
+
// ------- Imports -------
|
9 |
+
use lightningcss::stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet};
|
10 |
+
use minify_js::{minify, Session, TopLevelMode};
|
11 |
+
use std::{
|
12 |
+
fs::{read_dir, read_to_string, File, OpenOptions},
|
13 |
+
io::{Read, Write},
|
14 |
+
};
|
15 |
+
|
16 |
+
// ------- Constants -------
|
17 |
+
const COMMON_STATIC_SOURCE_CODE_FOLDER: &str = "./public/static/";
|
18 |
+
const STYLE_FOLDERS: [&str; 2] = ["themes", "colorschemes"];
|
19 |
+
const PACKAGE_ENVIRONMENT_VARIABLE: &str = "PKG_ENV";
|
20 |
+
const PRODUCTION_PKG_ENV_VARIABLE_VALUE: &str = "prod";
|
21 |
+
|
22 |
+
/// A main function which minifies both css and js files using `lightningcss` and `minify_js` when
|
23 |
+
/// the `PKG_ENV` environment and it is set to the value of `prod`.
|
24 |
+
///
|
25 |
+
/// # Error
|
26 |
+
///
|
27 |
+
/// This function returns the unit type when the minification process runs successfully otherwise
|
28 |
+
/// it returns a standard error.
|
29 |
+
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
30 |
+
if let Ok(pkg_env_var) = std::env::var(PACKAGE_ENVIRONMENT_VARIABLE) {
|
31 |
+
if pkg_env_var.to_lowercase() == PRODUCTION_PKG_ENV_VARIABLE_VALUE {
|
32 |
+
// A for loop that loops over each file name containing in the `colorschemes` and `themes` folders
|
33 |
+
// and minifies it using the `lightningcss` minifier.
|
34 |
+
for folder_name in STYLE_FOLDERS {
|
35 |
+
for file in read_dir(format!("{COMMON_STATIC_SOURCE_CODE_FOLDER}{folder_name}/"))? {
|
36 |
+
let file_path = file?.path();
|
37 |
+
let source = read_to_string(file_path.clone())?;
|
38 |
+
|
39 |
+
let mut stylesheet = StyleSheet::parse(&source, ParserOptions::default())
|
40 |
+
.map_err(|err| format!("{err}\n{:?}", file_path.file_name().unwrap()))?;
|
41 |
+
|
42 |
+
stylesheet.minify(MinifyOptions::default())?;
|
43 |
+
let minified_css = stylesheet.to_css(PrinterOptions::default())?;
|
44 |
+
|
45 |
+
let mut old_css_file = OpenOptions::new()
|
46 |
+
.write(true)
|
47 |
+
.truncate(true)
|
48 |
+
.open(file_path)?;
|
49 |
+
old_css_file.write_all(minified_css.code.as_bytes())?;
|
50 |
+
old_css_file.flush()?;
|
51 |
+
}
|
52 |
+
}
|
53 |
+
|
54 |
+
// A for loop that loops over each file name containing in the `public/static` folder and minifies
|
55 |
+
// it using the `minify-js` minifier.
|
56 |
+
for file in read_dir(COMMON_STATIC_SOURCE_CODE_FOLDER)? {
|
57 |
+
let file_path = file?.path();
|
58 |
+
if file_path.is_file() {
|
59 |
+
let mut code = Vec::new();
|
60 |
+
let mut js_file = File::open(file_path.clone())?;
|
61 |
+
js_file.read_to_end(&mut code)?;
|
62 |
+
|
63 |
+
drop(js_file);
|
64 |
+
|
65 |
+
let mut out = Vec::new();
|
66 |
+
minify(&Session::new(), TopLevelMode::Global, &code, &mut out)
|
67 |
+
.map_err(|err| format!("{err}\n{:?}", file_path.file_name().unwrap()))?;
|
68 |
+
|
69 |
+
let mut old_js_file = OpenOptions::new()
|
70 |
+
.write(true)
|
71 |
+
.truncate(true)
|
72 |
+
.open(file_path)?;
|
73 |
+
old_js_file.write_all(&out)?;
|
74 |
+
old_js_file.flush()?;
|
75 |
+
}
|
76 |
+
}
|
77 |
+
}
|
78 |
+
}
|
79 |
+
Ok(())
|
80 |
+
}
|