Skip to content

Commit 0403ac9

Browse files
committed
feat: is_dev() export
1 parent da95572 commit 0403ac9

5 files changed

Lines changed: 44 additions & 11 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
packages:
3+
- maudit
4+
release: minor
5+
---
6+
7+
Add `is_dev()` function to allow one to toggle off things whenever running in dev

crates/maudit-cli/src/dev.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ pub async fn start_dev_env(cwd: &str, host: bool) -> io::Result<()> {
2525
// Do initial sync build
2626
info!(name: "build", "Doing initial build…");
2727
let command = std::process::Command::new("cargo")
28-
.args(["run", "--quiet", "--", "--quiet"])
28+
.args(["run", "--quiet"])
29+
.envs([("MAUDIT_DEV", "true"), ("MAUDIT_QUIET", "true")])
2930
.output()
3031
.unwrap();
3132
let duration = start_time.elapsed();

crates/maudit/src/assets/image.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use image::GenericImageView;
66
use thumbhash::{rgba_to_thumb_hash, thumb_hash_to_average_rgba, thumb_hash_to_rgba};
77

88
use crate::assets::{Asset, InternalAsset};
9+
use crate::is_dev;
910

1011
#[derive(Clone, PartialEq, Eq, Hash)]
1112
pub enum ImageFormat {
@@ -130,7 +131,11 @@ fn get_placeholder(path: &PathBuf) -> Option<ImagePlaceholder> {
130131

131132
let thumbhash_rgba = thumb_hash_to_rgba(&thumb_hash).ok().unwrap();
132133
let thumbhash_png = thumbhash_to_png(&thumbhash_rgba);
133-
let optimized_png = oxipng::optimize_from_memory(&thumbhash_png, &Default::default()).unwrap();
134+
let optimized_png = if is_dev() {
135+
thumbhash_png
136+
} else {
137+
oxipng::optimize_from_memory(&thumbhash_png, &Default::default()).unwrap()
138+
};
134139

135140
let base64 = base64::engine::general_purpose::STANDARD.encode(&optimized_png);
136141
let data_uri = format!("data:image/png;base64,{}", base64);

crates/maudit/src/build.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
assets::{self},
1616
content::{Content, ContentSources},
1717
errors::BuildError,
18+
is_dev,
1819
logging::print_title,
1920
page::{DynamicRouteContext, FullPage, RenderResult, RouteContext, RouteParams, RouteType},
2021
route::{
@@ -71,18 +72,29 @@ impl Plugin for TailwindPlugin {
7172
.any(|entry| entry.canonicalize().unwrap().to_string_lossy() == args.id)
7273
{
7374
let start_tailwind = Instant::now();
74-
let tailwind_output =
75-
Command::new(&self.tailwind_path)
76-
.args(["--input", args.id])
77-
.arg("--minify") // TODO: Allow disabling minification
78-
.arg("--map") // TODO: Allow disabling source maps
79-
.output()
75+
let mut command = Command::new(&self.tailwind_path);
76+
command.args(["--input", args.id]);
77+
78+
// Add minify in production, source maps in development
79+
if !crate::is_dev() {
80+
command.arg("--minify");
81+
}
82+
if crate::is_dev() {
83+
command.arg("--map");
84+
}
85+
86+
let tailwind_output = command.output()
8087
.unwrap_or_else(|e| {
8188
// TODO: Return a proper error instead of panicking
89+
let args_str = if crate::is_dev() {
90+
format!("['--input', '{}', '--map']", args.id)
91+
} else {
92+
format!("['--input', '{}', '--minify']", args.id)
93+
};
8294
panic!(
83-
"Failed to execute Tailwind CSS command, is it installed and is the path to its binary correct?\nCommand: '{}', Args: ['--input', '{}', '--minify', '--map']. Error: {}",
95+
"Failed to execute Tailwind CSS command, is it installed and is the path to its binary correct?\nCommand: '{}', Args: {}. Error: {}",
8496
&self.tailwind_path,
85-
args.id,
97+
args_str,
8698
e
8799
)
88100
});
@@ -381,7 +393,7 @@ pub async fn build(
381393
let mut bundler = Bundler::with_plugins(
382394
BundlerOptions {
383395
input: Some(bundler_inputs),
384-
minify: Some(rolldown::RawMinifyOptions::Bool(true)),
396+
minify: Some(rolldown::RawMinifyOptions::Bool(!is_dev())),
385397
dir: Some(assets_dir.to_string_lossy().to_string()),
386398
module_types: Some(module_types_hashmap),
387399

crates/maudit/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ use content::ContentSources;
5959
use logging::init_logging;
6060
use page::FullPage;
6161

62+
pub fn is_dev() -> bool {
63+
if option_env!("MAUDIT_DEV") == Some("true") {
64+
return true;
65+
}
66+
67+
env::var("MAUDIT_DEV").map(|v| v == "true").unwrap_or(false)
68+
}
69+
6270
#[macro_export]
6371
/// Helps to define every route that should be build by [`coronate()`].
6472
///

0 commit comments

Comments
 (0)