Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: align parser.walk_expression to webpack, which put into context_dependency_helper #6963

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

CPunisher
Copy link
Contributor

@CPunisher CPunisher commented Jun 27, 2024

Summary

fix #6874

The cause of the problem is the direct walking of import call expression, which led to a conflict between the replacement generated by this walk and the replacement handled by ConstPlugin.

Webpack handles the import call expression in three cases:
TemplateLiteral: walk the parts with odd indices, see https://github.com/webpack/webpack/blob/8241da7f1e75c5581ba535d127fa66aeb9eb2ac8/lib/dependencies/ContextDependencyHelpers.js#L151
Wrapped expr: process the inner expressions, see https://github.com/webpack/webpack/blob/8241da7f1e75c5581ba535d127fa66aeb9eb2ac8/lib/dependencies/ContextDependencyHelpers.js#L218
Other cases: directly walk the import call expression, see https://github.com/webpack/webpack/blob/8241da7f1e75c5581ba535d127fa66aeb9eb2ac8/lib/dependencies/ContextDependencyHelpers.js#L241

My solution is to use a visitor to traverse the expression and find the corresponding Expr for BasicEvaluatedExpressionVisitor based on span. A better solution might be to add a reference to the corresponding Expr for BasicEvaluatedExpressionVisitor like webpack does. I tried this but couldn't resolve the lifetime issues.

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@github-actions github-actions bot added the release: bug fix release: bug related release(mr only) label Jun 27, 2024
Copy link

netlify bot commented Jun 27, 2024

Deploy Preview for rspack canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit 85456ac
🔍 Latest deploy log https://app.netlify.com/sites/rspack/deploys/667e36761d7b960008fe9f89

@LingyuCoder LingyuCoder requested a review from ahabhgk June 28, 2024 05:50
Copy link
Collaborator

@h-a-n-a h-a-n-a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@h-a-n-a
Copy link
Collaborator

h-a-n-a commented Jun 28, 2024

Do you need a double check? @ahabhgk

}

impl<'a, F: FnMut(&Expr)> Visit for BasicEvaluatedExpressionVisitor<'a, F> {
fn visit_expr(&mut self, n: &Expr) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Walk is different with swc visitor, parser plugins' goal is replace all the swc visitors, so this needs to rewrite to walk_expression

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I use this visitor to find the Expr ast node corresponding to the BasicEvaluatedExpression and then call parser.walk_expression. how would this visitor be rewritten to walk_expression?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there could have some walk around to implement this and avoid the life time issue, I will take a look next week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: bug fix release: bug related release(mr only)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Evaluating context request with conditional expression
3 participants