Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimize type and deserialize for config #837

Merged
merged 12 commits into from
Jan 3, 2024
15 changes: 7 additions & 8 deletions crates/mako/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,21 @@ pub fn js_ast_to_code(
}

let sourcemap = match context.config.devtool {
DevtoolConfig::SourceMap | DevtoolConfig::InlineSourceMap => {
Some(DevtoolConfig::SourceMap | DevtoolConfig::InlineSourceMap) => {
let src_buf = build_source_map(&source_map_buf, cm);
String::from_utf8(src_buf).unwrap()
}
DevtoolConfig::None => "".to_string(),
None => "".to_string(),
};

if matches!(context.config.devtool, DevtoolConfig::SourceMap) {
if matches!(context.config.devtool, Some(DevtoolConfig::SourceMap)) {
// separate sourcemap file
buf.append(
&mut format!("\n//# sourceMappingURL={filename}.map")
.as_bytes()
.to_vec(),
);
} else if matches!(context.config.devtool, DevtoolConfig::InlineSourceMap) {
} else if matches!(context.config.devtool, Some(DevtoolConfig::InlineSourceMap)) {
// inline sourcemap
buf.append(
&mut format!(
Expand Down Expand Up @@ -257,10 +257,10 @@ pub fn css_ast_to_code(
let src_buf = build_source_map(&source_map, &context.meta.css.cm);
let sourcemap = String::from_utf8(src_buf).unwrap();

if matches!(context.config.devtool, DevtoolConfig::SourceMap) {
if matches!(context.config.devtool, Some(DevtoolConfig::SourceMap)) {
// separate sourcemap file
css_code.push_str(format!("\n/*# sourceMappingURL={filename}.map*/").as_str());
} else if matches!(context.config.devtool, DevtoolConfig::InlineSourceMap) {
} else if matches!(context.config.devtool, Some(DevtoolConfig::InlineSourceMap)) {
// inline sourcemap
css_code.push_str(
format!(
Expand Down Expand Up @@ -316,7 +316,6 @@ mod tests {
use crate::assert_debug_snapshot;
use crate::ast::js_ast_to_code;
use crate::compiler::Context;
use crate::config::DevtoolConfig;
use crate::test_helper::create_mock_module;

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -331,7 +330,7 @@ export const bar = {
"#,
);
let mut context = Context::default();
context.config.devtool = DevtoolConfig::None;
context.config.devtool = None;
let (code, _) = js_ast_to_code(
module.info.unwrap().ast.as_script_mut(),
&Arc::new(context),
Expand Down
8 changes: 3 additions & 5 deletions crates/mako/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::analyze_deps::analyze_deps;
use crate::ast::{build_js_ast, generate_code_frame};
use crate::chunk_pot::util::{hash_hashmap, hash_vec};
use crate::compiler::{Compiler, Context};
use crate::config::{DevtoolConfig, Mode};
use crate::config::Mode;
use crate::load::{ext_name, load, Content};
use crate::module::{
Dependency, ExportInfo, ExportSpecifierInfo, ImportInfo, ImportSpecifierInfo, Module,
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Compiler {
.iter()
.map(|entry| {
let mut entry = entry.to_str().unwrap().to_string();
if self.context.config.hmr
if self.context.config.hmr.is_some()
&& self.context.config.mode == Mode::Development
&& self.context.args.watch
{
Expand Down Expand Up @@ -594,9 +594,7 @@ lazy_static! {
}

fn load_source_map(context: &Arc<Context>, content: &Content) -> Option<Vec<u8>> {
if matches!(context.config.devtool, DevtoolConfig::None) {
return None;
}
context.config.devtool.as_ref()?;

// TODO support load js source map
if !matches!(content, Content::Css(_)) {
Expand Down
4 changes: 2 additions & 2 deletions crates/mako/src/chunk_pot/ast_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::chunk::{Chunk, ChunkType};
use crate::chunk_pot::util::{pot_to_chunk_module, pot_to_module_object, runtime_code};
use crate::chunk_pot::{get_css_chunk_filename, util, ChunkPot};
use crate::compiler::Context;
use crate::config::{DevtoolConfig, Mode};
use crate::config::Mode;
use crate::generate_chunks::{ChunkFile, ChunkFileType};
use crate::load::file_content_hash;
use crate::minify::{minify_css, minify_js};
Expand Down Expand Up @@ -79,7 +79,7 @@ pub(crate) fn render_css_chunk(

let cm = &context.meta.css.cm;
let source_map = match context.config.devtool {
DevtoolConfig::None => None,
None => None,
_ => {
mako_profile_scope!("build_source_map");
// source map chain
Expand Down
10 changes: 3 additions & 7 deletions crates/mako/src/chunk_pot/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use mako_core::twox_hash::XxHash64;

use crate::chunk_pot::ChunkPot;
use crate::compiler::Context;
use crate::config::{DevtoolConfig, Mode};
use crate::config::Mode;
use crate::load::file_content_hash;
use crate::module::{Module, ModuleAst};
use crate::runtime::AppRuntimeTemplate;
Expand Down Expand Up @@ -52,7 +52,7 @@ pub(crate) fn render_module_js(
let source_map = {
mako_core::mako_profile_scope!("build_source_map");
match context.config.devtool {
DevtoolConfig::None => None,
None => None,
_ => Some(build_source_map(&source_map_buf, cm)),
}
};
Expand Down Expand Up @@ -92,11 +92,7 @@ pub(crate) fn empty_module_fn_expr() -> FnExpr {
create = "{ SizedCache::with_size(5) }"
)]
pub(crate) fn runtime_code(context: &Arc<Context>) -> Result<String> {
let umd = if context.config.umd != "none" {
Some(context.config.umd.clone())
} else {
None
};
let umd = context.config.umd.clone();
let chunk_graph = context.chunk_graph.read().unwrap();
let has_dynamic_chunks = chunk_graph.get_all_chunks().len() > 1;
let has_hmr = context.args.watch;
Expand Down
Loading