diff --git a/crates/rspack_core/src/dependency/require_resolve_dependency.rs b/crates/rspack_core/src/dependency/require_resolve_dependency.rs index 35ce904e3c0..e737c19e88d 100644 --- a/crates/rspack_core/src/dependency/require_resolve_dependency.rs +++ b/crates/rspack_core/src/dependency/require_resolve_dependency.rs @@ -17,16 +17,24 @@ pub struct RequireResolveDependency { span: ErrorSpan, #[allow(unused)] pub ast_path: JsAstPath, + optional: bool, } impl RequireResolveDependency { - pub fn new(request: String, weak: bool, span: ErrorSpan, ast_path: JsAstPath) -> Self { + pub fn new( + request: String, + weak: bool, + span: ErrorSpan, + ast_path: JsAstPath, + optional: bool, + ) -> Self { Self { request, weak, span, ast_path, id: None, + optional, } } } @@ -67,6 +75,10 @@ impl ModuleDependency for RequireResolveDependency { fn options(&self) -> Option<&ContextOptions> { None } + + fn get_optional(&self) -> bool { + self.optional + } } impl CodeGeneratable for RequireResolveDependency { diff --git a/crates/rspack_plugin_javascript/src/visitors/dependency/common_js_scanner.rs b/crates/rspack_plugin_javascript/src/visitors/dependency/common_js_scanner.rs index 0048a087985..b9ef672a566 100644 --- a/crates/rspack_plugin_javascript/src/visitors/dependency/common_js_scanner.rs +++ b/crates/rspack_plugin_javascript/src/visitors/dependency/common_js_scanner.rs @@ -13,6 +13,7 @@ use super::{as_parent_path, expr_matcher, is_require_resolve_call, is_require_re pub struct CommonJsScanner<'a> { pub dependencies: &'a mut Vec>, pub presentational_dependencies: &'a mut Vec>, + in_try: bool, } impl<'a> CommonJsScanner<'a> { @@ -23,6 +24,7 @@ impl<'a> CommonJsScanner<'a> { Self { dependencies, presentational_dependencies, + in_try: false, } } @@ -50,6 +52,7 @@ impl<'a> CommonJsScanner<'a> { weak, node.span.into(), ast_path, + self.in_try, ))); } } @@ -57,6 +60,16 @@ impl<'a> CommonJsScanner<'a> { } impl VisitAstPath for CommonJsScanner<'_> { + fn visit_try_stmt<'ast: 'r, 'r>( + &mut self, + node: &'ast swc_core::ecma::ast::TryStmt, + ast_path: &mut swc_core::ecma::visit::AstNodePath<'r>, + ) { + self.in_try = true; + node.visit_children_with_path(self, ast_path); + self.in_try = false; + } + fn visit_expr<'ast: 'r, 'r>( &mut self, expr: &'ast Expr, diff --git a/packages/rspack/tests/__snapshots__/StatsTestCases.test.ts.snap b/packages/rspack/tests/__snapshots__/StatsTestCases.test.ts.snap index 6c7453a65ce..7e485727810 100644 --- a/packages/rspack/tests/__snapshots__/StatsTestCases.test.ts.snap +++ b/packages/rspack/tests/__snapshots__/StatsTestCases.test.ts.snap @@ -1788,3 +1788,96 @@ chunk {main} bundle.js (main) 26 bytes [entry] [10] ./index.js 26 bytes {main} entry ./index " `; + +exports[`StatsTestCases should print correct stats for try-require--module 1`] = ` +{ + "errors": [], + "errorsCount": 0, + "warnings": [ + { + "formatted": "warning[internal]: Resolve error + ┌─ tests/statsCases/try-require--module/index.js:2:2 + │ +2 │ require('./missing-module') + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed to resolve ./missing-module in /tests/statsCases/try-require--module/index.js + +", + "message": "Failed to resolve ./missing-module in /tests/statsCases/try-require--module/index.js", + }, + ], + "warningsCount": 1, +} +`; + +exports[`StatsTestCases should print correct stats for try-require--module 2`] = ` +" +warning[internal]: Resolve error + ┌─ tests/statsCases/try-require--module/index.js:2:2 + │ +2 │ require('./missing-module') + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed to resolve ./missing-module in /tests/statsCases/try-require--module/index.js + +" +`; + +exports[`StatsTestCases should print correct stats for try-require-resolve-module 1`] = ` +{ + "errors": [], + "errorsCount": 0, + "warnings": [ + { + "formatted": "warning[internal]: Resolve error + ┌─ tests/statsCases/try-require-resolve-module/index.js:2:2 + │ +2 │ require.resolve('./missing-module') + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed to resolve ./missing-module in /tests/statsCases/try-require-resolve-module/index.js + +", + "message": "Failed to resolve ./missing-module in /tests/statsCases/try-require-resolve-module/index.js", + }, + ], + "warningsCount": 1, +} +`; + +exports[`StatsTestCases should print correct stats for try-require-resolve-module 2`] = ` +" +warning[internal]: Resolve error + ┌─ tests/statsCases/try-require-resolve-module/index.js:2:2 + │ +2 │ require.resolve('./missing-module') + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed to resolve ./missing-module in /tests/statsCases/try-require-resolve-module/index.js + +" +`; + +exports[`StatsTestCases should print correct stats for try-require-resolve-weak-module 1`] = ` +{ + "errors": [], + "errorsCount": 0, + "warnings": [ + { + "formatted": "warning[internal]: Resolve error + ┌─ tests/statsCases/try-require-resolve-weak-module/index.js:2:2 + │ +2 │ require.resolveWeak('./missing-module') + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed to resolve ./missing-module in /tests/statsCases/try-require-resolve-weak-module/index.js + +", + "message": "Failed to resolve ./missing-module in /tests/statsCases/try-require-resolve-weak-module/index.js", + }, + ], + "warningsCount": 1, +} +`; + +exports[`StatsTestCases should print correct stats for try-require-resolve-weak-module 2`] = ` +" +warning[internal]: Resolve error + ┌─ tests/statsCases/try-require-resolve-weak-module/index.js:2:2 + │ +2 │ require.resolveWeak('./missing-module') + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed to resolve ./missing-module in /tests/statsCases/try-require-resolve-weak-module/index.js + +" +`; diff --git a/packages/rspack/tests/statsCases/try-require--module/index.js b/packages/rspack/tests/statsCases/try-require--module/index.js new file mode 100644 index 00000000000..e5ac230d0a5 --- /dev/null +++ b/packages/rspack/tests/statsCases/try-require--module/index.js @@ -0,0 +1,5 @@ +try { + require('./missing-module') +} catch (e) { + +} diff --git a/packages/rspack/tests/statsCases/try-require--module/webpack.config.js b/packages/rspack/tests/statsCases/try-require--module/webpack.config.js new file mode 100644 index 00000000000..a7a0089aeae --- /dev/null +++ b/packages/rspack/tests/statsCases/try-require--module/webpack.config.js @@ -0,0 +1,3 @@ +module.exports = { + stats: "errors-warnings" +}; diff --git a/packages/rspack/tests/statsCases/try-require-resolve-module/index.js b/packages/rspack/tests/statsCases/try-require-resolve-module/index.js new file mode 100644 index 00000000000..393d7f8e07b --- /dev/null +++ b/packages/rspack/tests/statsCases/try-require-resolve-module/index.js @@ -0,0 +1,5 @@ +try { + require.resolve('./missing-module') +} catch (e) { + +} diff --git a/packages/rspack/tests/statsCases/try-require-resolve-module/webpack.config.js b/packages/rspack/tests/statsCases/try-require-resolve-module/webpack.config.js new file mode 100644 index 00000000000..a7a0089aeae --- /dev/null +++ b/packages/rspack/tests/statsCases/try-require-resolve-module/webpack.config.js @@ -0,0 +1,3 @@ +module.exports = { + stats: "errors-warnings" +}; diff --git a/packages/rspack/tests/statsCases/try-require-resolve-weak-module/index.js b/packages/rspack/tests/statsCases/try-require-resolve-weak-module/index.js new file mode 100644 index 00000000000..18fa7f7c4f2 --- /dev/null +++ b/packages/rspack/tests/statsCases/try-require-resolve-weak-module/index.js @@ -0,0 +1,5 @@ +try { + require.resolveWeak('./missing-module') +} catch (e) { + +} diff --git a/packages/rspack/tests/statsCases/try-require-resolve-weak-module/webpack.config.js b/packages/rspack/tests/statsCases/try-require-resolve-weak-module/webpack.config.js new file mode 100644 index 00000000000..a7a0089aeae --- /dev/null +++ b/packages/rspack/tests/statsCases/try-require-resolve-weak-module/webpack.config.js @@ -0,0 +1,3 @@ +module.exports = { + stats: "errors-warnings" +};