From d84f9bfa627c44f66be36c3528039c80f03a60c7 Mon Sep 17 00:00:00 2001 From: Hana Date: Mon, 29 Apr 2024 16:21:26 +0800 Subject: [PATCH] fix: wrong module source (#6396) --- .../src/parser_and_generator/mod.rs | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs b/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs index 031408b8164..25da337c08f 100644 --- a/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs +++ b/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs @@ -111,26 +111,27 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator { let original_map = source.map(&MapOptions::new(use_source_map)); let source = source.source(); - let gen_terminate_res = |diagnostics: Vec>| { - Ok( - ParseResult { - source: create_source( - source.to_string(), - resource_data.resource_path.to_string_lossy().to_string(), - enable_source_map, - ), - dependencies: vec![], - blocks: vec![], - presentational_dependencies: vec![], - analyze_result: Default::default(), - side_effects_bailout: None, - } - .with_diagnostic(map_box_diagnostics_to_module_parse_diagnostics( - diagnostics, - loaders, - )), - ) - }; + let result_with_diagnostics = + |source: String, diagnostics: Vec>| { + Ok( + ParseResult { + source: create_source( + source, + resource_data.resource_path.to_string_lossy().to_string(), + enable_source_map, + ), + dependencies: vec![], + blocks: vec![], + presentational_dependencies: vec![], + analyze_result: Default::default(), + side_effects_bailout: None, + } + .with_diagnostic(map_box_diagnostics_to_module_parse_diagnostics( + diagnostics, + loaders, + )), + ) + }; let mut ast = if let Some(RspackAst::JavaScript(loader_ast)) = additional_data.remove::() { @@ -145,7 +146,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator { Ok(ast) => ast, Err(e) => { diagnostics.append(&mut e.into_iter().map(|e| e.boxed()).collect()); - return gen_terminate_res(diagnostics); + return result_with_diagnostics(source.to_string(), diagnostics); } } .0 @@ -169,7 +170,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator { Ok(parse_result) => parse_result, Err(e) => { diagnostics.append(&mut e.into_iter().map(|e| e.boxed()).collect()); - return gen_terminate_res(diagnostics); + return result_with_diagnostics(output.code.clone(), diagnostics); } }; @@ -209,7 +210,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator { Ok(result) => result, Err(mut e) => { diagnostics.append(&mut e); - return gen_terminate_res(diagnostics); + return result_with_diagnostics(output.code.clone(), diagnostics); } }; diagnostics.append(&mut warning_diagnostics);