Skip to content

Commit

Permalink
feat(experimental): dev simple speed up (#1186)
Browse files Browse the repository at this point in the history
* feat: 🎉  call it su-plus

* refactor: 🎨 in case of module#info is none

* refactor: 🎨  add suplus plugin

* refactor: 🎨 use build after

* refactor: 🎨 quick exit in pot to file

* refactor: 🎨 add more aspect in plugins

* feat: ✨ poc version of ssu

* revert: ⏪ example code

* feat: ✨ add ssu module

* feat: ✨ invalid with config hash

* feat: ✨ patch hmr runtime manually

* chore: 🚨 lint happy

* refactor: 🎨

* refactor: 🎨 extract lock operation function

* feat: ✨ for cache chunks from mako dev server

* refactor: 🎨 compiler happy

* fix: 🐛 missing only css chunk
  • Loading branch information
stormslowly authored May 31, 2024
1 parent ec3d7c0 commit 6d787c7
Show file tree
Hide file tree
Showing 24 changed files with 769 additions and 67 deletions.
33 changes: 22 additions & 11 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/mako/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test = false
[dependencies]
bitflags = { version = "2.4.2", features = ["serde"] }
cached = { workspace = true }
dashmap = "4.0.1"
delegate = "0.12.0"
fixedbitset = "0.4.2"
glob-match = "0.2.1"
Expand Down
23 changes: 18 additions & 5 deletions crates/mako/src/ast/file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::hash::Hasher;
use std::hash::{Hash, Hasher};
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};
Expand All @@ -17,13 +17,13 @@ use url::Url;
use crate::compiler::Context;
use crate::utils::base64_decode;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Asset {
pub path: String,
pub content: String,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct JsContent {
pub is_jsx: bool,
pub content: String,
Expand All @@ -38,7 +38,7 @@ impl Default for JsContent {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Content {
Js(JsContent),
Css(String),
Expand All @@ -54,7 +54,7 @@ enum FileError {
ToBase64Error { path: String },
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq)]
pub struct File {
pub path: PathBuf,
pub relative_path: PathBuf,
Expand Down Expand Up @@ -89,6 +89,18 @@ impl Default for File {
}
}

impl Hash for File {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write(self.pathname.to_string_lossy().as_bytes());
}
}

impl PartialEq for File {
fn eq(&self, other: &Self) -> bool {
self.pathname.eq(&other.pathname)
}
}

const VIRTUAL: &str = "virtual:";

fn css_source_map_regex() -> &'static Regex {
Expand Down Expand Up @@ -320,6 +332,7 @@ type PathName = String;
type Search = String;
type Params = Vec<(String, String)>;
type Fragment = Option<String>;

fn parse_path(path: &str) -> Result<(PathName, Search, Params, Fragment)> {
let base = "http://a.com/";
let base_url = Url::parse(base)?;
Expand Down
20 changes: 15 additions & 5 deletions crates/mako/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ use crate::ast::file::{Content, File, JsContent};
use crate::compiler::{Compiler, Context};
use crate::generate::chunk_pot::util::hash_hashmap;
use crate::module::{Module, ModuleAst, ModuleId, ModuleInfo};
use crate::plugin::NextBuildParam;
use crate::resolve::ResolverResource;
use crate::utils::thread_pool;

#[derive(Debug, Error)]
pub enum BuildError {
#[error("{:}\n{:}", "Build failed.".to_string().red().to_string(), errors.iter().map(|e| e.to_string()).collect::<Vec<_>>().join("\n"))]
#[error(
"{:}\n{:}", "Build failed.".to_string().red().to_string(), errors.iter().map(| e | e.to_string()).collect::< Vec < _ >> ().join("\n")
)]
BuildTasksError { errors: Vec<anyhow::Error> },
}

Expand Down Expand Up @@ -105,10 +108,16 @@ impl Compiler {
if !module_graph.has_module(&dep_module_id) {
let module = match dep.resolver_resource {
ResolverResource::Virtual(_) | ResolverResource::Resolved(_) => {
count += 1;

let file = File::new(path.clone(), self.context.clone());
build_with_pool(file, Some(dep.resolver_resource.clone()));

if self.context.plugin_driver.next_build(&NextBuildParam {
current_module: &module_id,
next_file: &file,
resource: &dep.resolver_resource,
}) {
count += 1;
build_with_pool(file, Some(dep.resolver_resource.clone()));
}

Self::create_empty_module(&dep_module_id)
}
Expand Down Expand Up @@ -305,7 +314,8 @@ __mako_require__.loadScript('{}', (e) => e.type === 'load' ? resolve() : reject(
file,
deps,
ast,
resolved_resource: parent_resource, /* TODO: rename */
// TODO: rename
resolved_resource: parent_resource,
source_map_chain,
top_level_await,
is_async,
Expand Down
13 changes: 13 additions & 0 deletions crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use mako_core::regex::Regex;
use mako_core::swc_common::sync::Lrc;
use mako_core::swc_common::{Globals, SourceMap, DUMMY_SP};
use mako_core::swc_ecma_ast::Ident;
use mako_core::tracing::debug;

use crate::ast::comments::Comments;
use crate::config::{Config, OutputMode};
Expand Down Expand Up @@ -249,6 +250,10 @@ impl Compiler {
plugins.push(Arc::new(plugins::graphviz::Graphviz {}));
}

if args.watch && std::env::var("SSU").is_ok_and(|v| v == "true") {
plugins.push(Arc::new(plugins::ssu::SUPlus::new()));
}

if let Some(minifish_config) = &config._minifish {
let inject = if let Some(inject) = &minifish_config.inject {
let mut map = HashMap::new();
Expand Down Expand Up @@ -355,7 +360,15 @@ impl Compiler {
crate::ast::file::File::new_entry(entry, self.context.clone())
})
.collect();
self.context.plugin_driver.build_start(&self.context)?;

self.build(files)?;

debug!("start after build");

self.context
.plugin_driver
.after_build(&self.context, self)?;
}
let result = {
mako_core::mako_profile_scope!("Generate Stage");
Expand Down
14 changes: 8 additions & 6 deletions crates/mako/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,19 @@ pub struct TransformImportConfig {
pub style: Option<TransformImportStyle>,
}

#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, Hash)]
pub enum ExternalAdvancedSubpathConverter {
PascalCase,
}

#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, Hash)]
#[serde(untagged)]
pub enum ExternalAdvancedSubpathTarget {
Empty,
Tpl(String),
}

#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, Hash)]
pub struct ExternalAdvancedSubpathRule {
pub regex: String,
#[serde(with = "external_target_format")]
Expand Down Expand Up @@ -306,13 +306,14 @@ mod external_target_format {
}
}
}
#[derive(Deserialize, Serialize, Debug)]

#[derive(Deserialize, Serialize, Debug, Hash)]
pub struct ExternalAdvancedSubpath {
pub exclude: Option<Vec<String>>,
pub rules: Vec<ExternalAdvancedSubpathRule>,
}

#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, Hash)]
pub struct ExternalAdvanced {
pub root: String,
#[serde(rename = "type")]
Expand All @@ -321,7 +322,7 @@ pub struct ExternalAdvanced {
pub subpath: Option<ExternalAdvancedSubpath>,
}

#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, Hash)]
#[serde(untagged)]
pub enum ExternalConfig {
Basic(String),
Expand Down Expand Up @@ -567,6 +568,7 @@ impl Default for OptimizeChunkGroup {
}
}
}

/**
* custom formatter for convert string to regex
* @see https://serde.rs/custom-date-format.html
Expand Down
34 changes: 26 additions & 8 deletions crates/mako/src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,43 @@ impl DevServer {
}
_ => {
// for bundle outputs

let ext = path.rsplit('.').next();
let content_type = match ext {
None => "text/plain; charset=utf-8",
Some("js") => "application/javascript; charset=utf-8",
Some("css") => "text/css; charset=utf-8",
Some("map") | Some("json") => "application/json; charset=utf-8",
Some(_) => "text/plain; charset=utf-8",
};

// staticfile has 302 problems when modify tooooo fast in 1 second
// it will response 302 and we will get the old file
// TODO: fix the 302 problem?
if let Some(res) = context.get_static_content(path_without_slash_start) {
debug!("serve with context.get_static_content: {}", path);
let ext = path.rsplit('.').next();
let content_type = match ext {
None => "text/plain; charset=utf-8",
Some("js") => "application/javascript; charset=utf-8",
Some("css") => "text/css; charset=utf-8",
Some("map") | Some("json") => "application/json; charset=utf-8",
Some(_) => "text/plain; charset=utf-8",
};

return Ok(hyper::Response::builder()
.status(hyper::StatusCode::OK)
.header(CONTENT_TYPE, content_type)
.body(hyper::Body::from(res))
.unwrap());
}
// for cached dep
let abs_path = context
.root
.join("node_modules/.cache_mako/chunks")
.join(path_without_slash_start);
if !path_without_slash_start.is_empty() && abs_path.exists() {
return std::fs::read(abs_path).map_or(Ok(not_found_response()), |bytes| {
Ok(hyper::Response::builder()
.status(hyper::StatusCode::OK)
.header(CONTENT_TYPE, content_type)
.body(hyper::Body::from(bytes))
.unwrap())
});
}

// for hmr files
debug!("serve with staticfile server: {}", path);
let res = staticfile.serve(req).await;
Expand Down
Loading

0 comments on commit 6d787c7

Please sign in to comment.