Skip to content
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
84 changes: 45 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ inventory = { version = "0.3.17" }
rkyv = { version = "=0.8.8" }

# Must be pinned with the same swc versions
swc = { version = "=18.0.0" }
swc = { version = "=21.0.0" }
swc_config = { version = "=2.0.0" }
swc_core = { version = "=18.0.0", default-features = false, features = ["parallel_rayon"] }
swc_ecma_minifier = { version = "=14.0.0", default-features = false }
swc_error_reporters = { version = "=9.1.1" }
swc_html = { version = "=14.0.0" }
swc_html_minifier = { version = "=14.0.0", default-features = false }
swc_core = { version = "=22.4.0", default-features = false, features = ["parallel_rayon"] }
swc_ecma_minifier = { version = "=16.0.1", default-features = false }
swc_error_reporters = { version = "=10.0.0" }
swc_html = { version = "=16.0.0" }
swc_html_minifier = { version = "=16.0.0", default-features = false }
swc_node_comments = { version = "=8.0.0" }
swc_parallel = { version = "1.3.0", default-features = false, features = ["rayon"] }

Expand Down
8 changes: 6 additions & 2 deletions crates/rspack_ast/src/ast/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use swc_core::{
visit::{Fold, FoldWith, Visit, VisitMut, VisitMutWith, VisitWith},
},
};
use swc_error_reporters::handler::try_with_handler;
use swc_error_reporters::{handler::try_with_handler, TWithDiagnosticArray};
use swc_node_comments::SwcComments;

/// Program is a wrapper for SwcProgram
Expand Down Expand Up @@ -177,7 +177,11 @@ impl Ast {
})
}

pub fn transform_with_handler<F, R>(&mut self, cm: Lrc<SourceMap>, f: F) -> Result<R, Error>
pub fn transform_with_handler<F, R>(
&mut self,
cm: Lrc<SourceMap>,
f: F,
) -> Result<R, TWithDiagnosticArray<Error>>
where
F: FnOnce(&Handler, &mut Program, &Context) -> Result<R, Error>,
{
Expand Down
49 changes: 34 additions & 15 deletions crates/rspack_loader_swc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ fn read_config(opts: &Options, name: &FileName) -> Result<Option<Config>, Error>
res.with_context(|| format!("failed to read .swcrc file for input file at `{}`", name))
}

const SWC_MIETTE_DIAGNOSTIC_CODE: &str = "Builtin swc-loader error";

pub(crate) struct SwcCompiler {
cm: Arc<SourceMap>,
fm: Arc<SourceFile>,
Expand Down Expand Up @@ -336,6 +338,7 @@ impl SwcCompiler {

Ok(Some(built))
})
.map_err(|e| e.to_pretty_error())
})?;

match built {
Expand All @@ -359,21 +362,37 @@ impl SwcCompiler {
match result {
Ok(v) => Ok(v),
Err(err) => {
let error_msg = match err.downcast_ref::<String>(){
Some(msg) => {
msg
},
None => "unknown error"
};
let swc_core_version = env!("RSPACK_SWC_CORE_VERSION");
// FIXME: with_help has bugs, use with_help when diagnostic print is fixed
let help_msg = formatdoc!{"
The version of the SWC Wasm plugin you're using might not be compatible with `builtin:swc-loader`.
The `swc_core` version of the current `rspack_core` is {swc_core_version}.
Please check the `swc_core` version of SWC Wasm plugin to make sure these versions are within the compatible range.
See this guide as a reference for selecting SWC Wasm plugin versions: https://rspack.dev/errors/swc-plugin-version"};
let report: Report = MietteDiagnostic::new(format!("{}{}",error_msg,help_msg)).with_code("Builtin swc-loader error").into();
Err(report)

let swc_diagnostics = err.diagnostics();
if swc_diagnostics.iter().any(|d| match &d.code {
Some(code) => {
// reference to:
// https://github.com/swc-project/swc/blob/v1.11.21/crates/swc/src/plugin.rs#L187
// https://github.com/swc-project/swc/blob/v1.11.21/crates/swc/src/plugin.rs#L200
match code {
swc_core::common::errors::DiagnosticId::Error(e) => e.contains("plugin"),
swc_core::common::errors::DiagnosticId::Lint(_) => false,
}
},
None => false,
}
) {
// swc errors includes plugin error;
let error_msg = err.to_pretty_string();
let swc_core_version = env!("RSPACK_SWC_CORE_VERSION");
// FIXME: with_help has bugs, use with_help when diagnostic print is fixed
let help_msg = formatdoc!{"
The version of the SWC Wasm plugin you're using might not be compatible with `builtin:swc-loader`.
The `swc_core` version of the current `rspack_core` is {swc_core_version}.
Please check the `swc_core` version of SWC Wasm plugin to make sure these versions are within the compatible range.
See this guide as a reference for selecting SWC Wasm plugin versions: https://rspack.dev/errors/swc-plugin-version"};
let report: Report = MietteDiagnostic::new(format!("{}{}",error_msg,help_msg)).with_code(SWC_MIETTE_DIAGNOSTIC_CODE).into();
Err(report)
} else {
let error_msg = err.to_pretty_string();
let report: Report = MietteDiagnostic::new(error_msg.to_owned()).with_code(SWC_MIETTE_DIAGNOSTIC_CODE).into();
Err(report)
}
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_swc_js_minimizer/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ struct RspackErrorEmitter {
}

impl Emitter for RspackErrorEmitter {
fn emit(&mut self, db: &swc_core::common::errors::DiagnosticBuilder<'_>) {
fn emit(&mut self, db: &mut swc_core::common::errors::DiagnosticBuilder<'_>) {
let source_file_and_byte_pos = db
.span
.primary_span()
Expand Down
Loading