Skip to content

Commit

Permalink
feat: stats module reason type and userRequest (#2671)
Browse files Browse the repository at this point in the history
* chore: init type field

* feat: type field init

* chore: display update

* feat: init user_request field

* chore: changeset

* chore: example name modify

* chore: delete stats example

* test: add stats reason test case

* chore: use matchsnapshot instead of toequal

* chore: update snapshot

---------

Co-authored-by: Gengkun <ahabhgk@gmail.com>
  • Loading branch information
faga295 and ahabhgk authored Apr 9, 2023
1 parent e649469 commit 32f822b
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/warm-snakes-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@rspack/binding": patch
"examples-stats": patch
"@rspack/core": patch
---

add type and user_request field for module.reason
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ export interface JsStatsModuleReason {
moduleIdentifier?: string
moduleName?: string
moduleId?: string
type?: string
userRequest?: string
}
export interface JsStatsChunk {
type: string
Expand Down
4 changes: 4 additions & 0 deletions crates/node_binding/src/js_values/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub struct JsStatsModuleReason {
pub module_identifier: Option<String>,
pub module_name: Option<String>,
pub module_id: Option<String>,
pub r#type: Option<String>,
pub user_request: Option<String>,
}

impl From<rspack_core::StatsModuleReason> for JsStatsModuleReason {
Expand All @@ -141,6 +143,8 @@ impl From<rspack_core::StatsModuleReason> for JsStatsModuleReason {
module_identifier: stats.module_identifier,
module_name: stats.module_name,
module_id: stats.module_id,
r#type: stats.r#type,
user_request: stats.user_request,
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions crates/rspack_core/src/dependency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ pub enum DependencyType {
StaticExports,
}

impl Display for DependencyType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DependencyType::Unknown => write!(f, "unknown"),
DependencyType::Entry => write!(f, "entry"),
DependencyType::EsmImport => write!(f, "esm import"),
DependencyType::EsmExport => write!(f, "esm export"),
DependencyType::DynamicImport => write!(f, "dynamic import"),
DependencyType::CjsRequire => write!(f, "cjs require"),
DependencyType::NewUrl => write!(f, "new URL()"),
DependencyType::ImportMetaHotAccept => write!(f, "import.meta.webpackHot.accept"),
DependencyType::ImportMetaHotDecline => write!(f, "import.meta.webpackHot.decline"),
DependencyType::ModuleHotAccept => write!(f, "module.hot.accept"),
DependencyType::ModuleHotDecline => write!(f, "module.hot.decline"),
DependencyType::CssUrl => write!(f, "css url"),
DependencyType::CssImport => write!(f, "css import"),
DependencyType::CssCompose => write!(f, "css compose"),
DependencyType::ContextElement => write!(f, "context element"),
DependencyType::ImportContext => write!(f, "import context"),
DependencyType::CommonJSRequireContext => write!(f, "commonjs require context"),
DependencyType::RequireContext => write!(f, "require.context"),
DependencyType::WasmImport => write!(f, "wasm import"),
DependencyType::WasmExportImported => write!(f, "wasm export imported"),
DependencyType::StaticExports => write!(f, "static exports"),
}
}
}

#[derive(Default, Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum DependencyCategory {
#[default]
Expand Down
12 changes: 12 additions & 0 deletions crates/rspack_core/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,20 @@ impl Stats<'_> {
.and_then(|i| self.compilation.module_graph.module_by_identifier(&i))
.map(|m| get_stats_module_name_and_id(m, self.compilation))
.unzip();
let dependency = self
.compilation
.module_graph
.dependency_by_id(&connection.dependency_id);

let r#type = dependency.map(|d| d.dependency_type().to_string());

let user_request = dependency.map(|d| d.user_request().to_string());
StatsModuleReason {
module_identifier: connection.original_module_identifier.map(|i| i.to_string()),
module_name,
module_id,
r#type,
user_request,
}
})
.collect();
Expand Down Expand Up @@ -514,4 +524,6 @@ pub struct StatsModuleReason {
pub module_identifier: Option<String>,
pub module_name: Option<String>,
pub module_id: Option<String>,
pub r#type: Option<String>,
pub user_request: Option<String>,
}
61 changes: 61 additions & 0 deletions packages/rspack/tests/Stats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,65 @@ describe("Stats", () => {
"
`);
});

it("should have reasons", async () => {
const stats = await compile({
context: __dirname,
entry: "./fixtures/main5.js",
stats: { reasons: true }
});
expect(stats?.toJson({ all: false, modules: true }).modules)
.toMatchInlineSnapshot(`
[
{
"chunks": [
"main",
],
"id": "777",
"identifier": "javascript/auto|<PROJECT_ROOT>/tests/fixtures/a.js",
"issuer": "javascript/auto|<PROJECT_ROOT>/tests/fixtures/main5.js",
"issuerId": "371",
"issuerName": "./fixtures/main5.js",
"issuerPath": [
{
"id": "371",
"identifier": "javascript/auto|<PROJECT_ROOT>/tests/fixtures/main5.js",
"name": "./fixtures/main5.js",
},
],
"moduleType": "javascript/auto",
"name": "./fixtures/a.js",
"reasons": [
{
"moduleId": "371",
"moduleIdentifier": "javascript/auto|<PROJECT_ROOT>/tests/fixtures/main5.js",
"moduleName": "./fixtures/main5.js",
"type": "cjs require",
"userRequest": "./a",
},
],
"size": 55,
"type": "module",
},
{
"chunks": [
"main",
],
"id": "371",
"identifier": "javascript/auto|<PROJECT_ROOT>/tests/fixtures/main5.js",
"issuerPath": [],
"moduleType": "javascript/auto",
"name": "./fixtures/main5.js",
"reasons": [
{
"type": "entry",
"userRequest": "./fixtures/main5.js",
},
],
"size": 44,
"type": "module",
},
]
`);
});
});
3 changes: 3 additions & 0 deletions packages/rspack/tests/fixtures/main5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = require('./a');

console.log(a());

0 comments on commit 32f822b

Please sign in to comment.