Skip to content

[Feature]: Support diagnostics reporting API like esbuild.formatMessages or swc HANDLER #5913

@colinaaa

Description

@colinaaa

What problem does this feature solve?

We are using esbuild as our bundler and we are trying to migrate to rspack.

esbuild has a great API esbuild.formatMessages that we use to print the compilation warnings and errors. It makes all the printed diagnostics(directly by esbuild or by our plugins) look the same.

We also made some esbuild transform plugins with swc. And we use the HANDLER API of swc a lot to print diagnostics(with a custom Emitter to print
esbuild compatible messages).

It would be nice if rspack could provide such an API so that all the ecosystems(oxc, rsbuild and third-party plugins) can make diagnostics in the same format.

What does the proposed API of configuration look like?

Rust

The swc has a great API:

use swc_core::common::errors::HANDLER;

HANDLER.with(|handler| {
  handler
    .struct_span_warn(
      n.span,
      format!("DEPRECATED: old package \"{}\" is removed", n.src.value).as_str(),
    )
    .emit()
});

Maybe rspack could provide rust API like this(in a standalone crate so than it can be used by our rust transformers).

JS

It would be nice if it has the same API as esbuild.

import * as rspack from '@rspack/core'

let formatted = await rspack.formatMessages([
  {
    text: 'This is an error',
    location: {
      file: 'app.js',
      line: 10,
      column: 4,
      length: 3,
      lineText: 'let foo = bar',
    },
  },
], {
  kind: 'error',
  color: false,
  terminalWidth: 100,
})

console.log(formatted.join('\n'))

It also LGTM if rspack uses a different data structure that is more rust-style :)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions