Skip to content

Commit 73f147d

Browse files
feat(swc): enhance TransformOutput with diagnostics (#10312)
* feat: enhance TransformOutput with diagnostics * chore: format rs
1 parent 0ed2193 commit 73f147d

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

crates/rspack_javascript_compiler/src/compiler/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ impl JavaScriptCompiler {
3232

3333
#[derive(Debug)]
3434
pub struct TransformOutput {
35+
/// The transformed code
3536
pub code: String,
37+
38+
/// The source map for the transformed code
3639
pub map: Option<SourceMap>,
40+
41+
/// The warning diagnostics for the transformed code
42+
pub diagnostics: Vec<String>,
43+
}
44+
45+
impl TransformOutput {
46+
pub fn with_diagnostics(mut self, diagnostics: Vec<String>) -> Self {
47+
self.diagnostics = diagnostics;
48+
self
49+
}
3750
}

crates/rspack_javascript_compiler/src/compiler/stringify.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ impl JavaScriptCompiler {
209209
None
210210
};
211211

212-
Ok(TransformOutput { code: src, map })
212+
Ok(TransformOutput {
213+
code: src,
214+
map,
215+
diagnostics: Default::default(),
216+
})
213217
}
214218
}
215219

crates/rspack_javascript_compiler/src/compiler/transform.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl<'a> JavaScriptTransformer<'a> {
314314
.input_source_map(&built_input.input_source_map)
315315
.to_rspack_result_from_anyhow()?;
316316

317-
let program = self.transform_with_built_input(built_input)?;
317+
let (program, diagnostics) = self.transform_with_built_input(built_input)?;
318318
let format_opt = JsMinifyFormatOptions {
319319
inline_script: false,
320320
ascii_only: true,
@@ -331,7 +331,10 @@ impl<'a> JavaScriptTransformer<'a> {
331331
format: &format_opt,
332332
};
333333

334-
self.javascript_compiler.print(&program, print_options)
334+
self
335+
.javascript_compiler
336+
.print(&program, print_options)
337+
.map(|o| o.with_diagnostics(diagnostics))
335338
}
336339

337340
fn parse_js(
@@ -420,13 +423,20 @@ impl<'a> JavaScriptTransformer<'a> {
420423
fn transform_with_built_input(
421424
&self,
422425
built_input: BuiltInput<impl Pass>,
423-
) -> Result<Program, Error> {
426+
) -> Result<(Program, Vec<String>), Error> {
424427
let program = built_input.program;
425428
let mut pass = built_input.pass;
429+
let mut diagnostics = vec![];
426430
let program = self.run(|| {
427431
helpers::HELPERS.set(&self.helpers, || {
428-
let result = try_with_handler(self.cm.clone(), Default::default(), |_handler| {
429-
Ok(program.apply(&mut pass))
432+
let result = try_with_handler(self.cm.clone(), Default::default(), |handler| {
433+
// Apply external plugin passes to the Program AST.
434+
// External plugins may emit warnings or inject helpers,
435+
// so we need a handler to properly process them.
436+
let program = program.apply(&mut pass);
437+
diagnostics.extend(handler.take_diagnostics());
438+
439+
Ok(program)
430440
});
431441

432442
result.map_err(|err| {
@@ -476,7 +486,9 @@ impl<'a> JavaScriptTransformer<'a> {
476486
);
477487
}
478488

479-
program.map_err(|e| e.into())
489+
program
490+
.map(|program| (program, diagnostics))
491+
.map_err(|e| e.into())
480492
}
481493

482494
pub fn input_source_map(

crates/rspack_loader_swc/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use options::SwcLoaderJsOptions;
1111
pub use plugin::SwcLoaderPlugin;
1212
use rspack_cacheable::{cacheable, cacheable_dyn};
1313
use rspack_core::{Mode, RunnerContext};
14-
use rspack_error::{Diagnostic, Result};
14+
use rspack_error::{miette, Diagnostic, Result};
1515
use rspack_javascript_compiler::{JavaScriptCompiler, TransformOutput};
1616
use rspack_loader_runner::{Identifiable, Identifier, Loader, LoaderContext};
1717
use swc_config::{config_types::MergingOption, merge::Merge};
@@ -86,14 +86,24 @@ impl SwcLoader {
8686

8787
let source = content.into_string_lossy();
8888

89-
let TransformOutput { code, map } = javascript_compiler.transform(
89+
let TransformOutput {
90+
code,
91+
map,
92+
diagnostics,
93+
} = javascript_compiler.transform(
9094
source,
9195
Some(filename),
9296
swc_options,
9397
Some(loader_context.context.module_source_map_kind),
9498
|_| transformer::transform(&self.options_with_additional.rspack_experiments),
9599
)?;
96100

101+
for diagnostic in diagnostics {
102+
loader_context.emit_diagnostic(
103+
miette::miette! { severity = miette::Severity::Warning, "{}", diagnostic }.into(),
104+
);
105+
}
106+
97107
loader_context.finish_with((code, map));
98108

99109
Ok(())

0 commit comments

Comments
 (0)