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

feat: add module_id comments for debug #849

Merged
merged 1 commit into from
Jan 9, 2024
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
3 changes: 2 additions & 1 deletion crates/mako/src/chunk_pot/ast_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub(crate) fn render_normal_js_chunk(
let module = pot_to_chunk_module(
chunk_pot,
context.config.output.chunk_loading_global.clone(),
context,
)?;

let mut ast = GLOBALS.set(&context.meta.script.globals, || Ast {
Expand Down Expand Up @@ -280,7 +281,7 @@ fn render_entry_chunk_js_without_full_hash(
let modules_lit: Stmt = {
mako_core::mako_profile_scope!("to_module_object");

pot_to_module_object(pot)?
pot_to_module_object(pot, context)?
.into_var_decl(VarDeclKind::Var, quote_ident!("m").into())
.into()
};
Expand Down
58 changes: 44 additions & 14 deletions crates/mako/src/chunk_pot/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ use mako_core::swc_ecma_codegen::text_writer::JsWriter;
use mako_core::swc_ecma_codegen::{Config as JsCodegenConfig, Emitter};
use mako_core::swc_ecma_utils::{quote_ident, quote_str, ExprFactory};
use mako_core::twox_hash::XxHash64;
use swc_core::base::try_with_handler;
use swc_core::common::comments::{Comment, CommentKind, Comments};
use swc_core::common::errors::HANDLER;
use swc_core::common::{Span, GLOBALS};

use crate::chunk_pot::ChunkPot;
use crate::compiler::Context;
use crate::config::Mode;
use crate::load::file_content_hash;
use crate::module::{Module, ModuleAst};
use crate::module::{relative_to_root, Module, ModuleAst};
use crate::runtime::AppRuntimeTemplate;
use crate::sourcemap::build_source_map;

Expand Down Expand Up @@ -150,7 +154,7 @@ pub(super) fn to_array_lit(elems: Vec<ExprOrSpread>) -> ArrayLit {
}
}

pub(crate) fn pot_to_module_object(pot: &ChunkPot) -> Result<ObjectLit> {
pub(crate) fn pot_to_module_object(pot: &ChunkPot, context: &Arc<Context>) -> Result<ObjectLit> {
mako_core::mako_profile_function!();

let mut sorted_kv = pot
Expand All @@ -162,28 +166,54 @@ pub(crate) fn pot_to_module_object(pot: &ChunkPot) -> Result<ObjectLit> {

let mut props = Vec::new();

for (module_id_str, module) in sorted_kv {
let fn_expr = to_module_fn_expr(module.0)?;
let origin_comments = context.meta.script.origin_comments.read().unwrap();
let comments = origin_comments.get_swc_comments();

let pv: PropOrSpread = Prop::KeyValue(KeyValueProp {
key: quote_str!(module_id_str.clone()).into(),
value: fn_expr.into(),
})
.into();
let cm = context.meta.script.cm.clone();
GLOBALS.set(&context.meta.script.globals, || {
try_with_handler(cm.clone(), Default::default(), |handler| {
HANDLER.set(handler, || {
for (module_id_str, module) in sorted_kv {
let fn_expr = to_module_fn_expr(module.0)?;

props.push(pv);
}
let span = Span::dummy_with_cmt();
let id = module.0.id.id.clone();
let id = relative_to_root(id, &context.root);
comments.add_leading(
span.hi,
Comment {
kind: CommentKind::Block,
span: DUMMY_SP,
text: id.into(),
},
);
let pv: PropOrSpread = Prop::KeyValue(KeyValueProp {
key: quote_str!(span, module_id_str.clone()).into(),
value: fn_expr.into(),
})
.into();

props.push(pv);
}
Ok(())
})
})
})?;

Ok(ObjectLit {
span: DUMMY_SP,
props,
})
}

pub(crate) fn pot_to_chunk_module(pot: &ChunkPot, global: String) -> Result<SwcModule> {
pub(crate) fn pot_to_chunk_module(
pot: &ChunkPot,
global: String,
context: &Arc<Context>,
) -> Result<SwcModule> {
mako_core::mako_profile_function!();

let module_object = pot_to_module_object(pot)?;
let module_object = pot_to_module_object(pot, context)?;

// (globalThis['makoChunk_global'] = globalThis['makoChunk_global'] || []).push([["module_id"], { module object }])
let chunk_global_expr =
Expand Down Expand Up @@ -227,7 +257,7 @@ pub(crate) fn pot_to_chunk_module(pot: &ChunkPot, global: String) -> Result<SwcM
create = "{ SizedCache::with_size(20000) }",
convert = r#"{format!("{}.{:x}",file_content_hash(&module.id.id),module.info.as_ref().unwrap().raw_hash)}"#
)]
pub fn to_module_fn_expr(module: &Module) -> Result<FnExpr> {
fn to_module_fn_expr(module: &Module) -> Result<FnExpr> {
mako_core::mako_profile_function!(&module.id.id);

match &module.info.as_ref().unwrap().ast {
Expand Down
14 changes: 14 additions & 0 deletions crates/mako/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ pub fn generate_module_id(origin_module_id: String, context: &Arc<Context>) -> S
}
}

pub fn relative_to_root(module_path: String, root: &PathBuf) -> String {
let absolute_path = PathBuf::from(module_path);
let relative_path = diff_paths(&absolute_path, root).unwrap_or(absolute_path);
// diff_paths result always starts with ".."/"." or not
if relative_path.starts_with("..") || relative_path.starts_with(".") {
relative_path.to_string_lossy().to_string()
} else {
PathBuf::from(".")
.join(relative_path)
.to_string_lossy()
.to_string()
}
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct ModuleId {
pub id: String,
Expand Down
1 change: 1 addition & 0 deletions examples/dead-simple/mako.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"minify": false,
"moduleIdStrategy": "hashed",
"resolve": {
"alias": {
"@": "./foo"
Expand Down