Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 70 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn build(env: Env, build_params: BuildParams) -> napi::Result<JsObject> {
}
let d = DevServer::new(root.clone(), Arc::new(compiler));
deferred.resolve(move |env| env.get_undefined());
d.serve(move |_params| {}).await;
d.serve().await;
Ok(())
},
move |&mut _, _res| Ok(()),
Expand Down
69 changes: 44 additions & 25 deletions crates/binding/src/tsfn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,47 @@ pub struct JsHooks {
ts_type = "(filePath: string) => Promise<{ content: string, type: 'css'|'js' } | void> | void;"
)]
pub load: Option<JsFunction>,
#[napi(ts_type = "(data: {isFirstCompile: boolean; time: number; stats: {
startTime: number;
endTime: number;
}}) =>void ;")]
#[napi(ts_type = r#"(data: {
isFirstCompile: boolean;
time: number;
stats: {
hash: number;
builtAt: number;
rootPath: string;
outputPath: string;
assets: { type: string; name: string; path: string; size: number }[];
chunkModules: {
type: string;
id: string;
chunks: string[];
size: number;
}[];
modules: Record<
string,
{ id: string; dependents: string[]; dependencies: string[] }
>;
chunks: {
type: string;
id: string;
files: string[];
entry: boolean;
modules: { type: string; id: string; size: number; chunks: string[] }[];
siblings: string[];
origin: {
module: string;
moduleIdentifier: string;
moduleName: string;
loc: string;
request: string;
}[];
}[];
entrypoints: Record<string, { name: string; chunks: string[] }>;
rscClientComponents: { path; string; moduleId: string }[];
rscCSSModules: { path; string; moduleId: string; modules: boolean }[];
startTime: number;
endTime: number;
};
}) => void"#)]
pub generate_end: Option<JsFunction>,
#[napi(ts_type = "(path: string, content: Buffer) => Promise<void>;")]
pub _on_generate_file: Option<JsFunction>,
Expand Down Expand Up @@ -67,27 +104,9 @@ impl TsFnHooks {
|ctx: threadsafe_function::ThreadSafeCallContext<
ReadMessage<PluginGenerateEndParams, ()>,
>| {
let mut obj = ctx.env.create_object()?;
let mut stats = ctx.env.create_object()?;
stats.set_named_property(
"startTime",
ctx.env
.create_int64(ctx.value.message.stats.start_time as i64)?,
)?;
stats.set_named_property(
"endTime",
ctx.env
.create_int64(ctx.value.message.stats.end_time as i64)?,
)?;
obj.set_named_property(
"isFirstCompile",
ctx.value.message.is_first_compile,
)?;
obj.set_named_property(
"time",
ctx.env.create_int64(ctx.value.message.time as i64),
)?;
obj.set_named_property("stats", stats)?;
let obj = ctx
.env
.to_js_value(&serde_json::to_value(ctx.value.message)?)?;
let result = ctx.callback.unwrap().call(None, &[obj])?;
await_promise_with_void(ctx.env, result, ctx.value.tx).unwrap();
Ok(())
Expand Down
1 change: 1 addition & 0 deletions crates/mako/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ swc_node_comments = "0.19.1"

anyhow = "1.0.71"
base64 = "0.21.2"
chrono = "0.4.38"
clap = { version = "4.3.11", features = ["derive"] }
colored = "2"
config = "0.13.3"
Expand Down
60 changes: 31 additions & 29 deletions crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Instant, UNIX_EPOCH};
use std::time::Instant;

use anyhow::{anyhow, Error, Result};
use colored::Colorize;
Expand All @@ -17,7 +17,7 @@ use crate::config::{Config, OutputMode};
use crate::generate::chunk_graph::ChunkGraph;
use crate::generate::optimize_chunk::OptimizeChunksInfo;
use crate::module_graph::ModuleGraph;
use crate::plugin::{Plugin, PluginDriver, PluginGenerateEndParams, PluginGenerateStats};
use crate::plugin::{Plugin, PluginDriver, PluginGenerateEndParams};
use crate::plugins;
use crate::resolve::{get_resolvers, Resolvers};
use crate::stats::StatsInfo;
Expand Down Expand Up @@ -339,7 +339,7 @@ impl Compiler {
}

let t_compiler = Instant::now();
let start_time = std::time::SystemTime::now();
let start_time = chrono::Local::now().timestamp_millis();
let building_with_message = format!(
"Building with {} for {}...",
"mako".to_string().cyan(),
Expand Down Expand Up @@ -378,40 +378,42 @@ impl Compiler {
.plugin_driver
.after_build(&self.context, self)?;
}

self.context.plugin_driver.before_generate(&self.context)?;

let result = {
crate::mako_profile_scope!("Generate Stage");
// need to put all rayon parallel iterators run in the existed scope, or else rayon
// will create a new thread pool for those parallel iterators
thread_pool::scope(|_| self.generate())
};
let t_compiler_duration = t_compiler.elapsed();
if result.is_ok() {
println!(
"{}",
format!(
"✓ Built in {}",
format!("{}ms", t_compiler_duration.as_millis()).bold()
)
.green()
);
if !self.context.args.watch {
println!("{}", "Complete!".bold());
match result {
Ok(mut stats) => {
stats.start_time = start_time;
stats.end_time = chrono::Local::now().timestamp_millis();
println!(
"{}",
format!(
"✓ Built in {}",
format!("{}ms", t_compiler_duration.as_millis()).bold()
)
.green()
);
if !self.context.args.watch {
println!("{}", "Complete!".bold());
}
let params = PluginGenerateEndParams {
is_first_compile: true,
time: t_compiler.elapsed().as_millis() as i64,
stats,
};
self.context
.plugin_driver
.generate_end(&params, &self.context)?;
Ok(())
}
let end_time = std::time::SystemTime::now();
let params = PluginGenerateEndParams {
is_first_compile: true,
time: t_compiler.elapsed().as_millis() as u64,
stats: PluginGenerateStats {
start_time: start_time.duration_since(UNIX_EPOCH)?.as_millis() as u64,
end_time: end_time.duration_since(UNIX_EPOCH)?.as_millis() as u64,
},
};
self.context
.plugin_driver
.generate_end(&params, &self.context)?;
Ok(())
} else {
result
Err(e) => Err(e),
}
}

Expand Down
Loading