Skip to content

Commit 40c0daf

Browse files
authored
feat: pass WebpackError to ignoreWarnings (#10890)
1 parent e06f7a8 commit 40c0daf

File tree

19 files changed

+175
-32
lines changed

19 files changed

+175
-32
lines changed

β€Žcrates/node_binding/binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ export declare class JsCompilation {
317317
addEntry(args: [string, EntryDependency, JsEntryOptions | undefined][], callback: (errMsg: Error | null, results: [string | null, Module][]) => void): void
318318
addInclude(args: [string, EntryDependency, JsEntryOptions | undefined][], callback: (errMsg: Error | null, results: [string | null, Module][]) => void): void
319319
get codeGenerationResults(): CodeGenerationResults
320+
createStatsWarnings(warnings: Array<RspackError>, colored?: boolean | undefined | null): JsStatsError[]
320321
}
321322

322323
export declare class JsCompiler {

β€Žcrates/rspack_binding_api/src/compilation/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ use rustc_hash::FxHashMap;
2525

2626
use super::PathWithInfo;
2727
use crate::{
28-
entry::JsEntryOptions, utils::callbackify, AssetInfo, Chunk, ChunkGraph, ChunkGroupWrapper,
29-
ChunkWrapper, EntryDependency, ErrorCode, JsAddingRuntimeModule, JsAsset, JsCompatSource,
30-
JsFilename, JsModuleGraph, JsPathData, JsRspackDiagnostic, JsStats, JsStatsOptimizationBailout,
31-
ModuleObject, RspackError, RspackResultToNapiResultExt, ToJsCompatSource, COMPILER_REFERENCES,
28+
create_stats_warnings, entry::JsEntryOptions, utils::callbackify, AssetInfo, Chunk, ChunkGraph,
29+
ChunkGroupWrapper, ChunkWrapper, EntryDependency, ErrorCode, JsAddingRuntimeModule, JsAsset,
30+
JsCompatSource, JsFilename, JsModuleGraph, JsPathData, JsRspackDiagnostic, JsStats,
31+
JsStatsOptimizationBailout, ModuleObject, RspackError, RspackResultToNapiResultExt,
32+
ToJsCompatSource, COMPILER_REFERENCES,
3233
};
3334

3435
#[napi]
@@ -913,6 +914,17 @@ impl JsCompilation {
913914

914915
Ok(compilation.code_generation_results.reflector())
915916
}
917+
918+
#[napi(ts_return_type = "JsStatsError[]")]
919+
pub fn create_stats_warnings<'a>(
920+
&self,
921+
env: &'a Env,
922+
warnings: Vec<RspackError>,
923+
colored: Option<bool>,
924+
) -> Result<Array<'a>> {
925+
let compilation = self.as_ref()?;
926+
create_stats_warnings(env, compilation, warnings, colored)
927+
}
916928
}
917929

918930
pub struct JsAddEntryItemCallbackArgs(Vec<Either<String, ModuleObject>>);

β€Žcrates/rspack_binding_api/src/stats.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
use std::{borrow::Cow, cell::RefCell};
22

3-
use napi::{sys::napi_value, Env};
3+
use napi::{
4+
bindgen_prelude::{Array, FromNapiValue, JsObjectValue, Object},
5+
sys::napi_value,
6+
Env,
7+
};
48
use napi_derive::napi;
59
use rspack_collections::IdentifierMap;
610
use rspack_core::{
711
rspack_sources::{RawBufferSource, RawSource, Source},
812
EntrypointsStatsOption, ExtendedStatsOptions, Stats, StatsChunk, StatsModule, StatsUsedExports,
913
};
14+
use rspack_error::RspackSeverity;
1015
use rspack_napi::napi::{
1116
bindgen_prelude::{Buffer, Result, SharedReference, ToNapiValue},
1217
Either,
@@ -15,7 +20,8 @@ use rspack_util::{atom::Atom, itoa};
1520
use rustc_hash::FxHashMap as HashMap;
1621

1722
use crate::{
18-
identifier::JsIdentifier, to_js_module_id, JsCompilation, JsModuleId, RspackResultToNapiResultExt,
23+
identifier::JsIdentifier, to_js_module_id, JsCompilation, JsModuleId, RspackError,
24+
RspackResultToNapiResultExt,
1925
};
2026

2127
// These handles are only used during the `to_json` call,
@@ -1229,3 +1235,43 @@ impl JsStats {
12291235
self.inner.get_hash()
12301236
}
12311237
}
1238+
1239+
pub fn create_stats_warnings<'a>(
1240+
env: &'a Env,
1241+
compilation: &rspack_core::Compilation,
1242+
warnings: Vec<RspackError>,
1243+
colored: Option<bool>,
1244+
) -> Result<Array<'a>> {
1245+
let module_graph = compilation.get_module_graph();
1246+
1247+
let mut diagnostics = warnings
1248+
.into_iter()
1249+
.map(|warning| warning.into_diagnostic(RspackSeverity::Warn))
1250+
.collect::<Vec<_>>();
1251+
1252+
let stats_warnings = rspack_core::create_stats_errors(
1253+
compilation,
1254+
&module_graph,
1255+
&mut diagnostics,
1256+
colored.unwrap_or(false),
1257+
);
1258+
1259+
let mut array = env.create_array(stats_warnings.len() as u32)?;
1260+
let raw_env = env.raw();
1261+
for (i, warning) in stats_warnings.into_iter().enumerate() {
1262+
let js_warning = JsStatsError::from(warning);
1263+
let napi_val = unsafe { ToNapiValue::to_napi_value(raw_env, js_warning)? };
1264+
let object = unsafe { Object::from_napi_value(raw_env, napi_val)? };
1265+
array.set_element(i as u32, object)?;
1266+
}
1267+
1268+
MODULE_DESCRIPTOR_REFS.with(|refs| {
1269+
let mut refs = refs.borrow_mut();
1270+
refs.drain();
1271+
});
1272+
MODULE_COMMON_ATTRIBUTES_REFS.with(|refs| {
1273+
let mut refs = refs.borrow_mut();
1274+
refs.drain();
1275+
});
1276+
Ok(array)
1277+
}

β€Žcrates/rspack_core/src/stats/mod.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rspack_error::{
99
emitter::{
1010
DiagnosticDisplay, DiagnosticDisplayer, StdioDiagnosticDisplay, StringDiagnosticDisplay,
1111
},
12-
Result,
12+
Diagnostic, Result,
1313
};
1414
use rustc_hash::FxHashMap as HashMap;
1515

@@ -1351,3 +1351,62 @@ impl Stats<'_> {
13511351
Ok(stats)
13521352
}
13531353
}
1354+
1355+
pub fn create_stats_errors<'a>(
1356+
compilation: &'a Compilation,
1357+
module_graph: &'a ModuleGraph<'a>,
1358+
diagnostics: &'a mut Vec<Diagnostic>,
1359+
colored: bool,
1360+
) -> Vec<StatsError<'a>> {
1361+
diagnostics
1362+
.par_iter()
1363+
.map(|d| {
1364+
let module_identifier = d.module_identifier();
1365+
let (module_name, module_id) = module_identifier
1366+
.as_ref()
1367+
.and_then(|identifier| {
1368+
Some(get_stats_module_name_and_id(
1369+
compilation.module_by_identifier(identifier)?,
1370+
compilation,
1371+
))
1372+
})
1373+
.unzip();
1374+
1375+
let chunk = d
1376+
.chunk()
1377+
.map(ChunkUkey::from)
1378+
.map(|key| compilation.chunk_by_ukey.expect_get(&key));
1379+
1380+
let module_trace = get_module_trace(
1381+
module_identifier,
1382+
module_graph,
1383+
compilation,
1384+
&compilation.options,
1385+
);
1386+
1387+
let code = d.code().map(|code| code.to_string());
1388+
1389+
let mut diagnostic_displayer = DiagnosticDisplayer::new(colored);
1390+
StatsError {
1391+
name: d.code().map(|c| c.to_string()),
1392+
message: diagnostic_displayer
1393+
.emit_diagnostic(d)
1394+
.expect("should print diagnostics"),
1395+
code,
1396+
module_identifier,
1397+
module_name,
1398+
module_id: module_id.flatten(),
1399+
loc: d.loc().map(|loc| loc.to_string()),
1400+
file: d.file(),
1401+
1402+
chunk_name: chunk.and_then(|c| c.name()),
1403+
chunk_entry: chunk.map(|c| c.has_runtime(&compilation.chunk_group_by_ukey)),
1404+
chunk_initial: chunk.map(|c| c.can_be_initial(&compilation.chunk_group_by_ukey)),
1405+
chunk_id: chunk.and_then(|c| c.id(&compilation.chunk_ids_artifact).map(|id| id.as_str())),
1406+
details: d.details(),
1407+
stack: d.stack(),
1408+
module_trace,
1409+
}
1410+
})
1411+
.collect::<Vec<_>>()
1412+
}

β€Žpackages/rspack-test-tools/tests/diagnosticsCases/module-build-failed/loader-emit-hide-stack/my-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = function (context) {
22
let e;
33
e = new Error("Failed to load");
44
e.hideStack = true;
5-
this.emitError(e);
5+
this.emitError(e);
66

77
e = new Error("Failed to load");
88
e.hideStack = true;

β€Žpackages/rspack-test-tools/tests/errorCases/warning-test-push.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
"moduleIdentifier": "<TEST_TOOLS_ROOT>/tests/fixtures/errors/require.main.require.js",
3232
"moduleName": "./require.main.require.js",
3333
"moduleTrace": Array [],
34-
"stack": "ModuleParseWarning: ⚠ Module parse warning:\\n ╰─▢ ⚠ Unsupported feature: require.main.require() is not supported by Rspack.\\n ╭────\\n 1 β”‚ require.main.require('./file');\\n Β· ──────────────────────────────\\n ╰────\\n \\n\\n at warningFromStatsWarning (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at Array.map (<anonymous>)\\n at context.cachedGetWarnings (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at warnings (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at Object.fn (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at SyncBailHook.callAsyncStageRange (<ROOT>/node_modules/<PNPM_INNER>/@rspack/lite-tapable/dist/index.js<LINE_COL>)\\n at SyncBailHook.callStageRange (<ROOT>/node_modules/<PNPM_INNER>/@rspack/lite-tapable/dist/index.js<LINE_COL>)\\n at SyncBailHook.call (<ROOT>/node_modules/<PNPM_INNER>/@rspack/lite-tapable/dist/index.js<LINE_COL>)\\n at <RSPACK_ROOT>/dist/index.js<LINE_COL>\\n at StatsFactory._forEachLevel (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at StatsFactory._create (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at StatsFactory.create (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at Stats.toJson (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at ErrorProcessor.check (<TEST_TOOLS_ROOT>/dist/processor/error.js<LINE_COL>)\\n at run (<TEST_TOOLS_ROOT>/dist/test/simple.js<LINE_COL>)\\n at Object.<anonymous> (<TEST_TOOLS_ROOT>/dist/case/error.js<LINE_COL>)",
34+
"stack": undefined,
3535
},
3636
],
3737
}

β€Žpackages/rspack-test-tools/tests/errorCases/warning-test-splice-2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
"moduleIdentifier": "<TEST_TOOLS_ROOT>/tests/fixtures/errors/require.main.require.js",
3232
"moduleName": "./require.main.require.js",
3333
"moduleTrace": Array [],
34-
"stack": "ModuleParseWarning: ⚠ Module parse warning:\\n ╰─▢ ⚠ Unsupported feature: require.main.require() is not supported by Rspack.\\n ╭────\\n 1 β”‚ require.main.require('./file');\\n Β· ──────────────────────────────\\n ╰────\\n \\n\\n at warningFromStatsWarning (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at Array.map (<anonymous>)\\n at context.cachedGetWarnings (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at warnings (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at Object.fn (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at SyncBailHook.callAsyncStageRange (<ROOT>/node_modules/<PNPM_INNER>/@rspack/lite-tapable/dist/index.js<LINE_COL>)\\n at SyncBailHook.callStageRange (<ROOT>/node_modules/<PNPM_INNER>/@rspack/lite-tapable/dist/index.js<LINE_COL>)\\n at SyncBailHook.call (<ROOT>/node_modules/<PNPM_INNER>/@rspack/lite-tapable/dist/index.js<LINE_COL>)\\n at <RSPACK_ROOT>/dist/index.js<LINE_COL>\\n at StatsFactory._forEachLevel (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at StatsFactory._create (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at StatsFactory.create (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at Stats.toJson (<RSPACK_ROOT>/dist/index.js<LINE_COL>)\\n at ErrorProcessor.check (<TEST_TOOLS_ROOT>/dist/processor/error.js<LINE_COL>)\\n at run (<TEST_TOOLS_ROOT>/dist/test/simple.js<LINE_COL>)\\n at Object.<anonymous> (<TEST_TOOLS_ROOT>/dist/case/error.js<LINE_COL>)",
34+
"stack": undefined,
3535
},
3636
],
3737
}

β€Žpackages/rspack-test-tools/tests/statsOutputCases/ignore-warning/a.js

Whitespace-only changes.

β€Žpackages/rspack-test-tools/tests/statsOutputCases/ignore-warning/b.js

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
require("./index.scss");
22
import "./foo";
3+
import "./loader!./a.js";
4+
import "./loader!./b.js";

0 commit comments

Comments
Β (0)