Unverified Commit a12283e5 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: mode to disable tracing (#83683)

This means that the default is `TracingMode::Bundling` which has no tracing.

This is what you want for most normal bundler use cases, and what we want for our execution contexts for the Node.js loader runners.

This should help with #83613
parent 743d7a17
......@@ -28,7 +28,7 @@ use turbopack_core::{
module_graph::export_usage::OptionExportUsageInfo,
resolve::{parse::Request, pattern::Pattern},
};
use turbopack_ecmascript::{TypeofWindow, chunk::EcmascriptChunkType};
use turbopack_ecmascript::{AnalyzeMode, TypeofWindow, chunk::EcmascriptChunkType};
use turbopack_node::{
execution_context::ExecutionContext,
transforms::postcss::{PostCssConfigLocation, PostCssTransformOptions},
......@@ -337,6 +337,14 @@ pub async fn get_client_module_options_context(
enable_postcss_transform,
side_effect_free_packages: next_config.optimize_package_imports().owned().await?,
keep_last_successful_parse: next_mode.is_development(),
analyze_mode: if next_mode.is_development() {
AnalyzeMode::CodeGeneration
} else {
// Technically, this doesn't need to tracing for the client context. But this will
// result in more cache hits for the analysis for modules which are loaded for both ssr
// and client
AnalyzeMode::CodeGenerationAndTracing
},
..Default::default()
};
......
......@@ -27,7 +27,7 @@ use turbopack_core::{
target::CompileTarget,
};
use turbopack_ecmascript::{
TypeofWindow, chunk::EcmascriptChunkType, references::esm::UrlRewriteBehavior,
AnalyzeMode, TypeofWindow, chunk::EcmascriptChunkType, references::esm::UrlRewriteBehavior,
};
use turbopack_ecmascript_plugins::transform::directives::{
client::ClientDirectiveTransformer, client_disallowed::ClientDisallowedDirectiveTransformer,
......@@ -592,6 +592,11 @@ pub async fn get_server_module_options_context(
},
tree_shaking_mode: tree_shaking_mode_for_user_code,
side_effect_free_packages: next_config.optimize_package_imports().owned().await?,
analyze_mode: if next_mode.is_development() {
AnalyzeMode::CodeGeneration
} else {
AnalyzeMode::CodeGenerationAndTracing
},
enable_externals_tracing: if next_mode.is_production() {
Some(
ExternalsTracingOptions {
......
......@@ -17,11 +17,14 @@ use turbopack_core::{
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment, NodeJsVersion},
target::CompileTarget,
};
use turbopack_ecmascript::analyzer::{
graph::{EvalContext, VarGraph, VarMeta, create_graph},
imports::ImportAttributes,
linker::link,
test_utils::{early_visitor, visitor},
use turbopack_ecmascript::{
AnalyzeMode,
analyzer::{
graph::{EvalContext, VarGraph, VarMeta, create_graph},
imports::ImportAttributes,
linker::link,
test_utils::{early_visitor, visitor},
},
};
pub fn benchmark(c: &mut Criterion) {
......@@ -62,7 +65,11 @@ pub fn benchmark(c: &mut Criterion) {
None,
None,
);
let var_graph = create_graph(&program, &eval_context, false);
let var_graph = create_graph(
&program,
&eval_context,
AnalyzeMode::CodeGenerationAndTracing,
);
let input = BenchInput {
program,
......@@ -88,7 +95,13 @@ struct BenchInput {
}
fn bench_create_graph(b: &mut Bencher, input: &BenchInput) {
b.iter(|| create_graph(&input.program, &input.eval_context, false));
b.iter(|| {
create_graph(
&input.program,
&input.eval_context,
AnalyzeMode::CodeGenerationAndTracing,
)
});
}
fn bench_link(b: &mut Bencher, input: &BenchInput) {
......
......@@ -50,7 +50,7 @@ pub fn benchmark(c: &mut Criterion) {
.resolved_cell();
let mut cases = vec![];
for (file, is_tracing) in [
for (file, analyze_mode) in [
(r#"packages-bundle.js"#, false),
(r#"packages-bundle.js"#, true),
(r#"app-page-turbo.runtime.prod.js"#, false),
......@@ -68,7 +68,11 @@ pub fn benchmark(c: &mut Criterion) {
EcmascriptInputTransforms::empty().to_resolved().await?,
EcmascriptOptions {
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
is_tracing,
analyze_mode: if analyze_mode {
turbopack_ecmascript::AnalyzeMode::Tracing
} else {
turbopack_ecmascript::AnalyzeMode::CodeGenerationAndTracing
},
..Default::default()
}
.resolved_cell(),
......@@ -80,7 +84,7 @@ pub fn benchmark(c: &mut Criterion) {
cases.push((
file.rsplit("/").next().unwrap(),
if is_tracing { "tracing" } else { "full" },
if analyze_mode { "tracing" } else { "full" },
module,
));
}
......
......@@ -29,7 +29,7 @@ use super::{
is_unresolved_id,
};
use crate::{
SpecifiedModuleType,
AnalyzeMode, SpecifiedModuleType,
analyzer::{WellKnownObjectKind, is_unresolved},
references::constant_value::parse_single_expr_lit,
utils::{AstPathRange, unparen},
......@@ -330,7 +330,11 @@ impl VarGraph {
/// You should use same [Mark] for this function and
/// [swc_ecma_transforms_base::resolver::resolver_with_mark]
pub fn create_graph(m: &Program, eval_context: &EvalContext, is_tracing: bool) -> VarGraph {
pub fn create_graph(
m: &Program,
eval_context: &EvalContext,
analyze_mode: AnalyzeMode,
) -> VarGraph {
let mut graph = VarGraph {
values: Default::default(),
free_var_ids: Default::default(),
......@@ -339,7 +343,7 @@ pub fn create_graph(m: &Program, eval_context: &EvalContext, is_tracing: bool) -
m.visit_with_ast_path(
&mut Analyzer {
is_tracing,
analyze_mode,
data: &mut graph,
state: analyzer_state::AnalyzerState::new(),
eval_context,
......@@ -860,7 +864,7 @@ pub fn as_parent_path_skip(
}
struct Analyzer<'a> {
is_tracing: bool,
analyze_mode: AnalyzeMode,
data: &'a mut VarGraph,
state: analyzer_state::AnalyzerState,
......@@ -1467,7 +1471,7 @@ impl Analyzer<'_> {
member_expr: &'ast MemberExpr,
ast_path: &AstNodePath<AstParentNodeRef<'r>>,
) {
if self.is_tracing {
if !self.analyze_mode.is_code_gen() {
return;
}
......@@ -1501,7 +1505,7 @@ impl Analyzer<'_> {
start_ast_path,
} => {
self.effects = prev_effects;
if !self.is_tracing {
if self.analyze_mode.is_code_gen() {
self.effects.push(Effect::Unreachable { start_ast_path });
}
always_returns = true;
......@@ -2229,7 +2233,7 @@ impl VisitAstPath for Analyzer<'_> {
}
// If this identifier is free, produce an effect so we can potentially replace it later.
if !self.is_tracing
if self.analyze_mode.is_code_gen()
&& let JsValue::FreeVar(var) = self.eval_context.eval_ident(ident)
{
// TODO(lukesandberg): we should consider filtering effects here, e.g. there is no
......@@ -2267,7 +2271,7 @@ impl VisitAstPath for Analyzer<'_> {
return;
}
if !self.is_tracing {
if self.analyze_mode.is_code_gen() {
// Otherwise 'this' is free
self.add_effect(Effect::FreeVar {
var: atom!("this"),
......@@ -2282,7 +2286,7 @@ impl VisitAstPath for Analyzer<'_> {
expr: &'ast MetaPropExpr,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) {
if !self.is_tracing && expr.kind == MetaPropKind::ImportMeta {
if self.analyze_mode.is_code_gen() && expr.kind == MetaPropKind::ImportMeta {
// MetaPropExpr also covers `new.target`. Only consider `import.meta`
// an effect.
self.add_effect(Effect::ImportMeta {
......@@ -2533,7 +2537,7 @@ impl VisitAstPath for Analyzer<'_> {
n: &'ast UnaryExpr,
ast_path: &mut swc_core::ecma::visit::AstNodePath<'r>,
) {
if n.op == UnaryOp::TypeOf && !self.is_tracing {
if n.op == UnaryOp::TypeOf && self.analyze_mode.is_code_gen() {
let arg_value = Box::new(self.eval_context.eval(&n.arg));
self.add_effect(Effect::TypeOf {
......
......@@ -3813,9 +3813,12 @@ mod tests {
graph::{ConditionalKind, Effect, EffectArg, EvalContext, VarGraph, create_graph},
linker::link,
};
use crate::analyzer::{
graph::{AssignmentScopes, VarMeta},
imports::ImportAttributes,
use crate::{
AnalyzeMode,
analyzer::{
graph::{AssignmentScopes, VarMeta},
imports::ImportAttributes,
},
};
#[fixture("tests/analyzer/graph/**/input.js")]
......@@ -3857,7 +3860,8 @@ mod tests {
None,
);
let mut var_graph = create_graph(&m, &eval_context, false);
let mut var_graph =
create_graph(&m, &eval_context, AnalyzeMode::CodeGenerationAndTracing);
let var_cache = Default::default();
let mut named_values = var_graph
......
......@@ -173,6 +173,48 @@ pub enum TreeShakingMode {
ReexportsOnly,
}
#[derive(
PartialOrd,
Ord,
PartialEq,
Eq,
Hash,
Debug,
Clone,
Copy,
Default,
Serialize,
Deserialize,
TaskInput,
TraceRawVcs,
NonLocalValue,
)]
pub enum AnalyzeMode {
/// For bundling only, no tracing of referenced files.
#[default]
CodeGeneration,
/// For bundling and tracing of referenced files.
CodeGenerationAndTracing,
/// For tracing of referenced files only, no bundling (i.e. no codegen).
Tracing,
}
impl AnalyzeMode {
pub fn is_tracing(self) -> bool {
match self {
AnalyzeMode::Tracing | AnalyzeMode::CodeGenerationAndTracing => true,
AnalyzeMode::CodeGeneration => false,
}
}
pub fn is_code_gen(self) -> bool {
match self {
AnalyzeMode::CodeGeneration | AnalyzeMode::CodeGenerationAndTracing => true,
AnalyzeMode::Tracing => false,
}
}
}
#[turbo_tasks::value(transparent)]
pub struct OptionTreeShaking(pub Option<TreeShakingMode>);
......@@ -222,7 +264,7 @@ pub struct EcmascriptOptions {
pub keep_last_successful_parse: bool,
/// Whether the modules in this context are never chunked/codegen-ed, but only used for
/// tracing.
pub is_tracing: bool,
pub analyze_mode: AnalyzeMode,
// TODO this should just be handled via CompileTimeInfo FreeVarReferences, but then it
// (currently) wouldn't be possible to have different replacement values in user code vs
// node_modules.
......
......@@ -84,7 +84,7 @@ impl Display for CachedExternalType {
pub struct CachedExternalModule {
request: RcStr,
external_type: CachedExternalType,
tracing_mode: CachedExternalTracingMode,
analyze_mode: CachedExternalTracingMode,
}
#[turbo_tasks::value_impl]
......@@ -93,12 +93,12 @@ impl CachedExternalModule {
pub fn new(
request: RcStr,
external_type: CachedExternalType,
tracing_mode: CachedExternalTracingMode,
analyze_mode: CachedExternalTracingMode,
) -> Vc<Self> {
Self::cell(CachedExternalModule {
request,
external_type,
tracing_mode,
analyze_mode,
})
}
......@@ -226,7 +226,7 @@ impl Module for CachedExternalModule {
#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<ModuleReferences>> {
Ok(match &self.tracing_mode {
Ok(match &self.analyze_mode {
CachedExternalTracingMode::Untraced => ModuleReferences::empty(),
CachedExternalTracingMode::Traced {
externals_context,
......
......@@ -6,6 +6,7 @@ use turbo_tasks::{ResolvedVc, TransientInstance, TryJoinIterExt, ValueToString,
use turbo_tasks_fs::{DiskFileSystem, FileSystem};
use turbopack::{
ModuleAssetContext,
ecmascript::AnalyzeMode,
module_options::{
CssOptionsContext, EcmascriptOptionsContext, ModuleOptionsContext,
TypescriptTransformOptions,
......@@ -95,7 +96,7 @@ async fn node_file_trace_operation(
// Environment is not passed in order to avoid downleveling JS / CSS for
// node-file-trace.
environment: None,
is_tracing: true,
analyze_mode: AnalyzeMode::Tracing,
..Default::default()
}
.cell(),
......
......@@ -20,7 +20,9 @@ use turbo_tasks_fs::{
use turbo_unix_path::sys_to_unix;
use turbopack::{
ModuleAssetContext,
ecmascript::{EcmascriptInputTransform, TreeShakingMode, chunk::EcmascriptChunkType},
ecmascript::{
AnalyzeMode, EcmascriptInputTransform, TreeShakingMode, chunk::EcmascriptChunkType,
},
module_options::{
EcmascriptOptionsContext, JsxTransformOptions, ModuleOptionsContext, ModuleRule,
ModuleRuleEffect, RuleCondition, TypescriptTransformOptions,
......@@ -356,12 +358,14 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
ModuleOptionsContext {
environment: Some(env),
tree_shaking_mode: options.tree_shaking_mode,
analyze_mode: AnalyzeMode::CodeGenerationAndTracing,
..Default::default()
}
.resolved_cell(),
)],
module_rules: vec![module_rules],
tree_shaking_mode: options.tree_shaking_mode,
analyze_mode: AnalyzeMode::CodeGenerationAndTracing,
..Default::default()
}
.into(),
......
......@@ -33,7 +33,9 @@ use turbo_tasks::{
use turbo_tasks_backend::TurboTasksBackend;
use turbo_tasks_fs::{DiskFileSystem, FileSystem};
use turbopack::{
ModuleAssetContext, emit_with_completion_operation,
ModuleAssetContext,
ecmascript::AnalyzeMode,
emit_with_completion_operation,
module_options::{
CssOptionsContext, EcmascriptOptionsContext, ModuleOptionsContext,
TypescriptTransformOptions,
......@@ -373,7 +375,7 @@ async fn node_file_trace_operation(
// Environment is not passed in order to avoid downleveling JS / CSS for
// node-file-trace.
environment: None,
is_tracing: true,
analyze_mode: AnalyzeMode::Tracing,
..Default::default()
}
.cell(),
......
......@@ -15,6 +15,7 @@ use turbo_tasks_backend::TurboTasksBackend;
use turbo_tasks_fs::{DiskFileSystem, FileSystem};
use turbopack::{
ModuleAssetContext,
ecmascript::AnalyzeMode,
module_options::{
CssOptionsContext, EcmascriptOptionsContext, ModuleOptionsContext,
TypescriptTransformOptions,
......@@ -210,7 +211,7 @@ async fn node_file_trace_operation(package_root: RcStr, input: RcStr) -> Result<
// Environment is not passed in order to avoid downleveling JS / CSS for
// node-file-trace.
environment: None,
is_tracing: true,
analyze_mode: AnalyzeMode::Tracing,
..Default::default()
}
.cell(),
......
......@@ -56,6 +56,7 @@ use turbopack_core::{
pub use turbopack_css as css;
pub use turbopack_ecmascript as ecmascript;
use turbopack_ecmascript::{
AnalyzeMode,
inlined_bytes_module::InlinedBytesJsModule,
references::external_module::{
CachedExternalModule, CachedExternalTracingMode, CachedExternalType,
......@@ -719,7 +720,7 @@ pub async fn externals_tracing_module_context(
// Environment is not passed in order to avoid downleveling JS / CSS for
// node-file-trace.
environment: None,
is_tracing: true,
analyze_mode: AnalyzeMode::Tracing,
..Default::default()
}
.cell(),
......@@ -824,7 +825,7 @@ impl AssetContext for ModuleAssetContext {
}
ResolveResultItem::External { name, ty, traced } => {
let replacement = if replace_externals {
let tracing_mode = if traced == ExternalTraced::Traced
let analyze_mode = if traced == ExternalTraced::Traced
&& let Some(options) = &self
.module_options_context()
.await?
......@@ -850,7 +851,7 @@ impl AssetContext for ModuleAssetContext {
CachedExternalTracingMode::Untraced
};
replace_external(&name, ty, import_externals, tracing_mode).await?
replace_external(&name, ty, import_externals, analyze_mode).await?
} else {
None
};
......@@ -1003,7 +1004,7 @@ pub async fn replace_external(
name: &RcStr,
ty: ExternalType,
import_externals: bool,
tracing_mode: CachedExternalTracingMode,
analyze_mode: CachedExternalTracingMode,
) -> Result<Option<ModuleResolveResultItem>> {
let external_type = match ty {
ExternalType::CommonJs => CachedExternalType::CommonJs,
......@@ -1022,7 +1023,7 @@ pub async fn replace_external(
}
};
let module = CachedExternalModule::new(name.clone(), external_type, tracing_mode)
let module = CachedExternalModule::new(name.clone(), external_type, analyze_mode)
.to_resolved()
.await?;
......
......@@ -227,7 +227,7 @@ impl ModuleOptions {
execution_context,
tree_shaking_mode,
keep_last_successful_parse,
is_tracing,
analyze_mode,
..
} = *module_options_context.await?;
......@@ -280,7 +280,7 @@ impl ModuleOptions {
ignore_dynamic_requests,
extract_source_map: matches!(ecmascript_source_maps, SourceMapsType::Full),
keep_last_successful_parse,
is_tracing,
analyze_mode,
enable_typeof_window_inlining,
..Default::default()
};
......
......@@ -9,7 +9,9 @@ use turbopack_core::{
chunk::SourceMapsType, compile_time_info::CompileTimeInfo, condition::ContextCondition,
environment::Environment, resolve::options::ImportMapping,
};
use turbopack_ecmascript::{TreeShakingMode, TypeofWindow, references::esm::UrlRewriteBehavior};
use turbopack_ecmascript::{
AnalyzeMode, TreeShakingMode, TypeofWindow, references::esm::UrlRewriteBehavior,
};
pub use turbopack_mdx::MdxTransformOptions;
use turbopack_node::{
execution_context::ExecutionContext,
......@@ -207,7 +209,7 @@ pub struct ModuleOptionsContext {
/// Whether the modules in this context are never chunked/codegen-ed, but only used for
/// tracing.
pub is_tracing: bool,
pub analyze_mode: AnalyzeMode,
pub placeholder_for_future_extensions: (),
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment