diff --git a/crates/mako/src/chunk_pot/ast_impl.rs b/crates/mako/src/chunk_pot/ast_impl.rs index 39759e091..5d7d1f58d 100644 --- a/crates/mako/src/chunk_pot/ast_impl.rs +++ b/crates/mako/src/chunk_pot/ast_impl.rs @@ -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 { @@ -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() }; diff --git a/crates/mako/src/chunk_pot/util.rs b/crates/mako/src/chunk_pot/util.rs index 005a6a944..69ce8e603 100644 --- a/crates/mako/src/chunk_pot/util.rs +++ b/crates/mako/src/chunk_pot/util.rs @@ -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; @@ -150,7 +154,7 @@ pub(super) fn to_array_lit(elems: Vec) -> ArrayLit { } } -pub(crate) fn pot_to_module_object(pot: &ChunkPot) -> Result { +pub(crate) fn pot_to_module_object(pot: &ChunkPot, context: &Arc) -> Result { mako_core::mako_profile_function!(); let mut sorted_kv = pot @@ -162,17 +166,39 @@ pub(crate) fn pot_to_module_object(pot: &ChunkPot) -> Result { 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, @@ -180,10 +206,14 @@ pub(crate) fn pot_to_module_object(pot: &ChunkPot) -> Result { }) } -pub(crate) fn pot_to_chunk_module(pot: &ChunkPot, global: String) -> Result { +pub(crate) fn pot_to_chunk_module( + pot: &ChunkPot, + global: String, + context: &Arc, +) -> Result { 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 = @@ -227,7 +257,7 @@ pub(crate) fn pot_to_chunk_module(pot: &ChunkPot, global: String) -> Result Result { +fn to_module_fn_expr(module: &Module) -> Result { mako_core::mako_profile_function!(&module.id.id); match &module.info.as_ref().unwrap().ast { diff --git a/crates/mako/src/module.rs b/crates/mako/src/module.rs index cfbfa687c..adee8bd60 100644 --- a/crates/mako/src/module.rs +++ b/crates/mako/src/module.rs @@ -125,6 +125,20 @@ pub fn generate_module_id(origin_module_id: String, context: &Arc) -> 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, diff --git a/examples/dead-simple/mako.config.json b/examples/dead-simple/mako.config.json index 1ef7c6d19..38bcaabf4 100644 --- a/examples/dead-simple/mako.config.json +++ b/examples/dead-simple/mako.config.json @@ -1,5 +1,6 @@ { "minify": false, + "moduleIdStrategy": "hashed", "resolve": { "alias": { "@": "./foo"