Skip to content

Commit

Permalink
feat(module_optional): support require.resolve and require.resolveWeak (
Browse files Browse the repository at this point in the history
#3237)

* done

* done

* fix

* snapshot

* snapshot

* snapshot

* mv

* remove

* fix

* update snapshot

* rewrite test case
  • Loading branch information
suxin2017 authored May 22, 2023
1 parent dc5bde9 commit ac70f66
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/rspack_core/src/dependency/require_resolve_dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn ModuleDependency>>,
pub presentational_dependencies: &'a mut Vec<Box<dyn Dependency>>,
in_try: bool,
}

impl<'a> CommonJsScanner<'a> {
Expand All @@ -23,6 +24,7 @@ impl<'a> CommonJsScanner<'a> {
Self {
dependencies,
presentational_dependencies,
in_try: false,
}
}

Expand Down Expand Up @@ -50,13 +52,24 @@ impl<'a> CommonJsScanner<'a> {
weak,
node.span.into(),
ast_path,
self.in_try,
)));
}
}
}
}

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,
Expand Down
93 changes: 93 additions & 0 deletions packages/rspack/tests/__snapshots__/StatsTestCases.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
2require('./missing-module')
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed to resolve ./missing-module in <PROJECT_ROOT>/tests/statsCases/try-require--module/index.js
",
"message": "Failed to resolve ./missing-module in <PROJECT_ROOT>/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 <PROJECT_ROOT>/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
2require.resolve('./missing-module')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed to resolve ./missing-module in <PROJECT_ROOT>/tests/statsCases/try-require-resolve-module/index.js
",
"message": "Failed to resolve ./missing-module in <PROJECT_ROOT>/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 <PROJECT_ROOT>/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
2require.resolveWeak('./missing-module')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Failed to resolve ./missing-module in <PROJECT_ROOT>/tests/statsCases/try-require-resolve-weak-module/index.js
",
"message": "Failed to resolve ./missing-module in <PROJECT_ROOT>/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 <PROJECT_ROOT>/tests/statsCases/try-require-resolve-weak-module/index.js
"
`;
5 changes: 5 additions & 0 deletions packages/rspack/tests/statsCases/try-require--module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
try {
require('./missing-module')
} catch (e) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
stats: "errors-warnings"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
try {
require.resolve('./missing-module')
} catch (e) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
stats: "errors-warnings"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
try {
require.resolveWeak('./missing-module')
} catch (e) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
stats: "errors-warnings"
};

0 comments on commit ac70f66

Please sign in to comment.