Skip to content

Commit

Permalink
refactor: port inner graph
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Jul 4, 2024
1 parent 06b47ef commit 255e00e
Show file tree
Hide file tree
Showing 20 changed files with 1,965 additions and 1,022 deletions.
404 changes: 404 additions & 0 deletions crates/rspack_core/src/tree_shaking/symbol/mod.rs

Large diffs are not rendered by default.

62 changes: 32 additions & 30 deletions crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use rspack_core::{
};
use rspack_error::miette::Diagnostic;
use rspack_error::{DiagnosticExt, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use swc_core::common::comments::Comments;
use swc_core::common::input::SourceFileInput;
use swc_core::common::{FileName, Span, SyntaxContext};
use swc_core::ecma::ast;
use swc_core::ecma::parser::{lexer::Lexer, EsSyntax, Syntax};
use swc_node_comments::SwcComments;

use crate::dependency::HarmonyCompatibilityDependency;
use crate::inner_graph_plugin::InnerGraphPlugin;
use crate::visitors::{scan_dependencies, swc_visitor::resolver};
use crate::visitors::{semicolon, PathIgnoredSpans, ScanDependenciesResult};
use crate::{SideEffectsFlagPluginVisitor, SyntaxContextInfo};
Expand Down Expand Up @@ -164,13 +164,14 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {

let mut path_ignored_spans = PathIgnoredSpans::default();

let unresolved_mark = ast.get_context().unresolved_mark;

let ScanDependenciesResult {
mut dependencies,
dependencies,
blocks,
presentational_dependencies,
mut usage_span_record,
import_map,
mut warning_diagnostics,
..
} = match ast.visit(|program, _| {
scan_dependencies(
cm.clone(),
Expand All @@ -185,6 +186,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
module_parser_options,
&mut semicolons,
&mut path_ignored_spans,
unresolved_mark,
)
}) {
Ok(result) => result,
Expand All @@ -201,7 +203,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
let unresolved_ctxt = SyntaxContext::empty().apply_mark(context.unresolved_mark);
let mut visitor = SideEffectsFlagPluginVisitor::new(
SyntaxContextInfo::new(unresolved_ctxt),
program.comments.as_ref(),
program.comments.as_ref().map(|c| c as &dyn Comments),
);
program.visit_with(&mut visitor);
build_meta.side_effect_free = Some(visitor.side_effects_item.is_none());
Expand All @@ -216,32 +218,32 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
});
}

let inner_graph = if compiler_options.optimization.inner_graph {
ast.transform(|program, context| {
let unresolved_ctxt = SyntaxContext::empty().apply_mark(context.unresolved_mark);
let top_level_ctxt = SyntaxContext::empty().apply_mark(context.top_level_mark);
let mut plugin = InnerGraphPlugin::new(
&mut dependencies,
unresolved_ctxt,
top_level_ctxt,
&mut usage_span_record,
&import_map,
module_identifier,
program.comments.take(),
&path_ignored_spans,
);
plugin.enable();
program.visit_with(&mut plugin);
program.comments = plugin.comments.take();
Some(plugin)
})
} else {
None
};
// let inner_graph = if compiler_options.optimization.inner_graph {
// ast.transform(|program, context| {
// let unresolved_ctxt = SyntaxContext::empty().apply_mark(context.unresolved_mark);
// let top_level_ctxt = SyntaxContext::empty().apply_mark(context.top_level_mark);
// let mut plugin = InnerGraphPlugin::new(
// &mut dependencies,
// unresolved_ctxt,
// top_level_ctxt,
// &mut usage_span_record,
// &import_map,
// module_identifier,
// program.comments.take(),
// &path_ignored_spans,
// );
// plugin.enable();
// // program.visit_with(&mut plugin);
// program.comments = plugin.comments.take();
// Some(plugin)
// })
// } else {
// None
// };

if let Some(mut inner_graph) = inner_graph {
inner_graph.infer_dependency_usage();
}
// if let Some(mut inner_graph) = inner_graph {
// inner_graph.infer_dependency_usage();
// }

Ok(
ParseResult {
Expand Down
91 changes: 89 additions & 2 deletions crates/rspack_plugin_javascript/src/parser_plugin/drive.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use swc_core::atoms::Atom;
use swc_core::common::Span;
use swc_core::ecma::ast::{
BinExpr, CallExpr, Callee, CondExpr, ExportDecl, ExportDefaultDecl, Expr, OptChainExpr, UnaryExpr,
BinExpr, CallExpr, Callee, ClassMember, CondExpr, ExportDecl, ExportDefaultDecl, Expr,
OptChainExpr, UnaryExpr,
};
use swc_core::ecma::ast::{IfStmt, MemberExpr, Stmt, UnaryOp, VarDecl, VarDeclarator};

use super::{BoxJavascriptParserPlugin, JavascriptParserPlugin};
use crate::parser_plugin::r#const::is_logic_op;
use crate::utils::eval::BasicEvaluatedExpression;
use crate::visitors::{ExportedVariableInfo, JavascriptParser};
use crate::visitors::{ClassDeclOrExpr, ExportedVariableInfo, JavascriptParser};

pub struct JavaScriptParserPluginDrive {
plugins: Vec<BoxJavascriptParserPlugin>,
Expand Down Expand Up @@ -84,6 +85,21 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn module_declaration(
&self,
parser: &mut JavascriptParser,
decl: &swc_core::ecma::ast::ModuleDecl,
) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.module_declaration(parser, decl);
// `SyncBailHook`
if res.is_some() {
return res;
}
}
None
}

fn call(&self, parser: &mut JavascriptParser, expr: &CallExpr, name: &str) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.call(parser, expr, name);
Expand Down Expand Up @@ -256,6 +272,17 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn statement(&self, parser: &mut JavascriptParser, stmt: &Stmt) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.statement(parser, stmt);
// `SyncBailHook`
if res.is_some() {
return res;
}
}
None
}

fn statement_if(&self, parser: &mut JavascriptParser, expr: &IfStmt) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.statement_if(parser, expr);
Expand Down Expand Up @@ -315,6 +342,55 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn class_extends_expression(
&self,
parser: &mut JavascriptParser,
super_class: &Expr,
class_decl_or_expr: ClassDeclOrExpr,
) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.class_extends_expression(parser, super_class, class_decl_or_expr);
// `SyncBailHook`
if res.is_some() {
return res;
}
}
None
}

fn class_body_element(
&self,
parser: &mut JavascriptParser,
member: &ClassMember,
class_decl_or_expr: ClassDeclOrExpr,
) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.class_body_element(parser, member, class_decl_or_expr);
// `SyncBailHook`
if res.is_some() {
return res;
}
}
None
}

fn class_body_value(
&self,
parser: &mut JavascriptParser,
element: &swc_core::ecma::ast::ClassMember,
expr_span: Span,
class_decl_or_expr: ClassDeclOrExpr,
) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.class_body_value(parser, element, expr_span, class_decl_or_expr);
// `SyncBailHook`
if res.is_some() {
return res;
}
}
None
}

fn this(
&self,
parser: &mut JavascriptParser,
Expand Down Expand Up @@ -445,6 +521,17 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn pre_block_statement(&self, parser: &mut JavascriptParser, stmt: &Stmt) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.pre_block_statement(parser, stmt);
// `SyncBailHook`
if res.is_some() {
return res;
}
}
None
}

fn import_call(&self, parser: &mut JavascriptParser, expr: &CallExpr) -> Option<bool> {
assert!(expr.callee.is_import());
for plugin in &self.plugins {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rspack_core::{ConstDependency, DependencyLocation, DependencyType, SpanExt, DEFAULT_EXPORT};
use rspack_core::{ConstDependency, DependencyLocation, DependencyType, SpanExt};
use swc_core::atoms::Atom;
use swc_core::common::{Span, Spanned};
use swc_core::ecma::ast::{
Expand All @@ -8,12 +8,15 @@ use swc_core::ecma::ast::{
use swc_core::ecma::utils::{find_pat_ids, ExprFactory};

use super::harmony_import_dependency_parser_plugin::handle_harmony_import_side_effects_dep;
use super::{JavascriptParserPlugin, JS_DEFAULT_KEYWORD};
use super::{
InnerGraphMapUsage, InnerGraphPlugin, JavascriptParserPlugin, DEFAULT_STAR_JS_WORD,
JS_DEFAULT_KEYWORD,
};
use crate::dependency::{
DeclarationId, DeclarationInfo, HarmonyExportExpressionDependency, HarmonyExportHeaderDependency,
HarmonyExportImportedSpecifierDependency, HarmonyExportSpecifierDependency, Specifier,
};
use crate::visitors::{ExtraSpanInfo, JavascriptParser};
use crate::visitors::JavascriptParser;

fn handle_esm_export_harmony_import_side_effects_dep(
parser: &mut JavascriptParser,
Expand Down Expand Up @@ -204,9 +207,10 @@ impl JavascriptParserPlugin for HarmonyExportDependencyParserPlugin {
JS_DEFAULT_KEYWORD.clone(),
named_id.clone(),
)));
parser.rewrite_usage_span.insert(
export_default_decl.span,
ExtraSpanInfo::AddVariableUsage(vec![(named_id, JS_DEFAULT_KEYWORD.clone())]),
InnerGraphPlugin::add_variable_usage(
parser,
&named_id,
InnerGraphMapUsage::Value(JS_DEFAULT_KEYWORD.clone()),
);
parser
.presentational_dependencies
Expand All @@ -232,12 +236,14 @@ impl JavascriptParserPlugin for HarmonyExportDependencyParserPlugin {
};
let local = match &ident {
Some(ident) => ident.sym.clone(),
None => DEFAULT_EXPORT.into(),
None => DEFAULT_STAR_JS_WORD.clone(),
};
parser.rewrite_usage_span.insert(
export_default_decl.span,
ExtraSpanInfo::AddVariableUsage(vec![(local, JS_DEFAULT_KEYWORD.clone())]),
InnerGraphPlugin::add_variable_usage(
parser,
&local,
InnerGraphMapUsage::Value(JS_DEFAULT_KEYWORD.clone()),
);

parser
.presentational_dependencies
.push(Box::new(HarmonyExportExpressionDependency::new(
Expand Down Expand Up @@ -292,9 +298,10 @@ impl JavascriptParserPlugin for HarmonyExportDependencyParserPlugin {
parser: &mut JavascriptParser,
export_default_expr: &swc_core::ecma::ast::ExportDefaultExpr,
) -> Option<bool> {
parser.rewrite_usage_span.insert(
export_default_expr.span,
ExtraSpanInfo::AddVariableUsage(vec![(DEFAULT_EXPORT.into(), JS_DEFAULT_KEYWORD.clone())]),
InnerGraphPlugin::add_variable_usage(
parser,
&DEFAULT_STAR_JS_WORD,
InnerGraphMapUsage::Value(JS_DEFAULT_KEYWORD.clone()),
);
parser
.presentational_dependencies
Expand Down Expand Up @@ -328,17 +335,17 @@ impl JavascriptParserPlugin for HarmonyExportDependencyParserPlugin {
ident.sym.clone(),
)));

parser.rewrite_usage_span.insert(
export_decl.span(),
ExtraSpanInfo::AddVariableUsage(vec![(ident.sym.clone(), ident.sym.clone())]),
InnerGraphPlugin::add_variable_usage(
parser,
&ident.sym,
InnerGraphMapUsage::Value(ident.sym.clone()),
);
parser
.build_info
.harmony_named_exports
.insert(ident.sym.clone());
}
Decl::Var(v) => {
let mut usages = vec![];
find_pat_ids::<_, Ident>(&v.decls)
.into_iter()
.for_each(|ident| {
Expand All @@ -349,13 +356,13 @@ impl JavascriptParserPlugin for HarmonyExportDependencyParserPlugin {
ident.sym.clone(),
)));

usages.push((ident.sym.clone(), ident.sym.clone()));
InnerGraphPlugin::add_variable_usage(
parser,
&ident.sym,
InnerGraphMapUsage::Value(ident.sym.clone()),
);
parser.build_info.harmony_named_exports.insert(ident.sym);
});

parser
.rewrite_usage_span
.insert(export_decl.span(), ExtraSpanInfo::AddVariableUsage(usages));
}
_ => {}
}
Expand All @@ -382,7 +389,6 @@ impl JavascriptParserPlugin for HarmonyExportDependencyParserPlugin {
named_export: &NamedExport,
) -> Option<bool> {
if named_export.src.is_none() {
let mut usages = vec![];
named_export
.specifiers
.iter()
Expand Down Expand Up @@ -435,15 +441,16 @@ impl JavascriptParserPlugin for HarmonyExportDependencyParserPlugin {
.harmony_named_exports
.insert(export.clone());
}
usages.push((orig.sym.clone(), export));
InnerGraphPlugin::add_variable_usage(
parser,
&orig.sym,
InnerGraphMapUsage::Value(export),
);
}
}
_ => unreachable!(),
});

parser
.rewrite_usage_span
.insert(named_export.span(), ExtraSpanInfo::AddVariableUsage(usages));
parser
.presentational_dependencies
.push(Box::new(ConstDependency::new(
Expand Down
Loading

0 comments on commit 255e00e

Please sign in to comment.