Unverified Commit 2f3e77f5 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: analyze mode tracing imports only (#98825)

We had 3 modes, now we just have 2 boolean for all possible 2*2 = 4
modes
parent 812c88f3
......@@ -404,7 +404,7 @@ pub async fn get_client_module_options_context(
.await?,
),
keep_last_successful_parse: next_mode.is_development(),
analyze_mode: AnalyzeMode::CodeGeneration,
analyze_mode: AnalyzeMode::code_generation(),
..Default::default()
};
......
......@@ -573,9 +573,9 @@ pub async fn get_server_module_options_context(
.await?,
),
analyze_mode: if enable_tracing {
AnalyzeMode::CodeGenerationAndTracing
AnalyzeMode::code_generation_and_tracing()
} else {
AnalyzeMode::CodeGeneration
AnalyzeMode::code_generation()
},
enable_externals_tracing: if enable_tracing {
Some(
......
......@@ -82,7 +82,7 @@ pub fn benchmark(c: &mut Criterion) {
arena.get_or_default(),
&program,
&eval_context,
AnalyzeMode::CodeGenerationAndTracing,
AnalyzeMode::code_generation_and_tracing(),
true,
SpecifiedModuleType::Automatic,
true,
......@@ -121,7 +121,7 @@ fn bench_create_graph(b: &mut Bencher, input: &BenchInput) {
arena.get_or_default(),
&input.program,
&input.eval_context,
AnalyzeMode::CodeGenerationAndTracing,
AnalyzeMode::code_generation_and_tracing(),
true,
SpecifiedModuleType::Automatic,
true,
......
......@@ -52,9 +52,9 @@ pub fn benchmark(c: &mut Criterion) {
root_dir,
file,
analyze_mode: if trace_only {
AnalyzeMode::Tracing
AnalyzeMode::tracing()
} else {
AnalyzeMode::CodeGenerationAndTracing
AnalyzeMode::code_generation_and_tracing()
},
},
bench_full,
......@@ -89,7 +89,7 @@ async fn setup(
ResolvedVc::upcast(module_asset_context),
EcmascriptInputTransforms::empty().to_resolved().await?,
EcmascriptOptions {
follow_reexports: analyze_mode != AnalyzeMode::Tracing,
follow_reexports: analyze_mode.is_codegen,
module_fragments_enabled: false,
analyze_mode,
..Default::default()
......
......@@ -95,13 +95,13 @@ pub fn create_graph<'a>(
// CommonJS export recognition runs for a CommonJS module that emits code when either CJS
// tree-shaking or CJS scope hoisting is enabled (both consume the static export analysis).
if (cjs_tree_shaking || cjs_scope_hoisting)
&& analyze_mode.is_code_gen()
&& analyze_mode.is_codegen
&& eval_context.is_cjs(specified_module_type)
{
analyzer.enable_cjs_exports();
}
if cjs_tree_shaking && analyze_mode.is_code_gen() {
if cjs_tree_shaking && analyze_mode.is_codegen {
analyzer.enable_require_usage(&eval_context.imports);
}
......
......@@ -486,7 +486,7 @@ mod analyzer_state {
start_ast_path,
} => {
self.effects = prev_effects;
if self.analyze_mode.is_code_gen() {
if self.analyze_mode.is_codegen {
self.effects
.push(self.arena, Effect::Unreachable { start_ast_path });
}
......@@ -1444,7 +1444,7 @@ impl<'a> Analyzer<'a, '_> {
}
fn add_esm_module_item(&mut self, ast_path: &AstNodePath<AstParentNodeRef<'_>>) {
if self.analyze_mode.is_code_gen() {
if self.analyze_mode.is_codegen {
let path = self.ast_paths.intern(ast_path.kinds().iter().copied());
self.code_gens
.push(EsmModuleItem::new(path, self.supports_block_scoping).into());
......@@ -2484,7 +2484,7 @@ impl VisitAstPath for Analyzer<'_, '_> {
}
// If this identifier is free, produce an effect so we can potentially replace it later.
if self.analyze_mode.is_code_gen()
if self.analyze_mode.is_codegen
&& let JsValue::FreeVar(var) = self.eval_context.eval_id(self.arena, ident.to_id())
{
// TODO(lukesandberg): we should consider filtering effects here, e.g. there is no
......@@ -2502,7 +2502,7 @@ impl VisitAstPath for Analyzer<'_, '_> {
node: &'ast ThisExpr,
ast_path: &mut swc_core::ecma::visit::AstNodePath<'r>,
) {
if !self.analyze_mode.is_code_gen() {
if !self.analyze_mode.is_codegen {
return;
}
......@@ -2550,7 +2550,7 @@ impl VisitAstPath for Analyzer<'_, '_> {
expr: &'ast MetaPropExpr,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) {
if self.analyze_mode.is_code_gen() && expr.kind == MetaPropKind::ImportMeta {
if self.analyze_mode.is_codegen && expr.kind == MetaPropKind::ImportMeta {
// MetaPropExpr also covers `new.target`. Only consider `import.meta`
// an effect.
self.add_effect(Effect::ImportMeta {
......@@ -2801,7 +2801,7 @@ impl VisitAstPath for Analyzer<'_, '_> {
n: &'ast UnaryExpr,
ast_path: &mut swc_core::ecma::visit::AstNodePath<'r>,
) {
if n.op == UnaryOp::TypeOf && self.analyze_mode.is_code_gen() {
if n.op == UnaryOp::TypeOf && self.analyze_mode.is_codegen {
let arg_value = BumpBox::new_in(self.eval_context.eval(self.arena, &n.arg), self.arena);
self.add_effect(Effect::TypeOf {
......
......@@ -364,7 +364,7 @@ mod tests {
arena.get_or_default(),
&m,
&eval_context,
AnalyzeMode::CodeGenerationAndTracing,
AnalyzeMode::code_generation_and_tracing(),
true,
SpecifiedModuleType::EcmaScript,
true,
......
......@@ -164,35 +164,50 @@ pub enum SpecifiedModuleType {
Debug,
Clone,
Copy,
Default,
Deserialize,
TraceRawVcs,
Encode,
Decode,
)]
pub enum AnalyzeMode {
/// For bundling only, no tracing of referenced files.
#[default]
CodeGeneration,
/// For bundling and finding references to external referenced files
CodeGenerationAndTracing,
/// For tracing transitive external references (i.e. no codegen).
Tracing,
pub struct AnalyzeMode {
/// Whether code generation will be performed after analyzing.
pub is_codegen: bool,
/// Whether references to external files should be traced.
pub trace_file_references: bool,
}
impl Default for AnalyzeMode {
fn default() -> Self {
Self::code_generation()
}
}
impl AnalyzeMode {
/// Are we currently collecting references to external assets. e.g. filesystem dependencies
pub fn is_tracing_assets(self) -> bool {
match self {
AnalyzeMode::Tracing | AnalyzeMode::CodeGenerationAndTracing => true,
AnalyzeMode::CodeGeneration => false,
pub const fn code_generation() -> Self {
Self {
is_codegen: true,
trace_file_references: false,
}
}
pub fn is_code_gen(self) -> bool {
match self {
AnalyzeMode::CodeGeneration | AnalyzeMode::CodeGenerationAndTracing => true,
AnalyzeMode::Tracing => false,
pub const fn code_generation_and_tracing() -> Self {
Self {
is_codegen: true,
trace_file_references: true,
}
}
pub const fn tracing_import_only() -> Self {
Self {
is_codegen: false,
trace_file_references: false,
}
}
pub const fn tracing() -> Self {
Self {
is_codegen: false,
trace_file_references: true,
}
}
}
......@@ -856,7 +871,9 @@ impl EcmascriptModuleAsset {
self.ty,
*self.transforms,
node_env,
options.analyze_mode == AnalyzeMode::Tracing,
// When not codegen-ing at all, turn string encoding and AST parsing issues into
// warnings instead.
!options.analyze_mode.is_codegen,
options.inline_helpers,
))
}
......
......@@ -280,7 +280,7 @@ pub async fn parse(
ty: EcmascriptModuleAssetType,
transforms: ResolvedVc<EcmascriptInputTransforms>,
node_env: RcStr,
is_external_tracing: bool,
loose_errors: bool,
inline_helpers: bool,
) -> Result<Vc<ParseResult>> {
let span = tracing::info_span!(
......@@ -294,7 +294,7 @@ pub async fn parse(
ty,
transforms,
node_env,
is_external_tracing,
loose_errors,
inline_helpers,
)
.instrument(span)
......
......@@ -228,7 +228,7 @@ pub async fn get_constants(
arena.get_or_default(),
program,
eval_context,
AnalyzeMode::Tracing,
AnalyzeMode::tracing(),
supports_block_scoping,
// This is currently ignored with cjs_tree_shaking:false
SpecifiedModuleType::Automatic,
......
......@@ -39,13 +39,13 @@ fn fixture(input: PathBuf) {
noop_backing_storage(),
));
tt.run_once(async move {
fixture_op(input.clone(), AnalyzeMode::CodeGeneration)
fixture_op(input.clone(), AnalyzeMode::code_generation())
.read_strongly_consistent()
.await?;
fixture_op(input.clone(), AnalyzeMode::Tracing)
fixture_op(input.clone(), AnalyzeMode::tracing())
.read_strongly_consistent()
.await?;
fixture_op(input, AnalyzeMode::CodeGenerationAndTracing)
fixture_op(input, AnalyzeMode::code_generation_and_tracing())
.read_strongly_consistent()
.await?;
anyhow::Ok(())
......@@ -144,10 +144,11 @@ async fn fixture_op(input: RcStr, analyze_mode: AnalyzeMode) -> anyhow::Result<(
NormalizedOutput::from(value)
.compare_to_file(input.with_file_name(format!(
"env-vars{}.snapshot",
match analyze_mode {
AnalyzeMode::CodeGenerationAndTracing => "",
AnalyzeMode::CodeGeneration => ".codegen",
AnalyzeMode::Tracing => ".tracing",
match (analyze_mode.is_codegen, analyze_mode.trace_file_references,) {
(true, true) => "",
(true, false) => ".codegen",
(false, true) => ".tracing",
(false, false) => ".tracing-import-only",
}
)))
.unwrap();
......
......@@ -99,7 +99,7 @@ async fn node_file_trace_operation(
// Environment is not passed in order to avoid downleveling JS / CSS for
// node-file-trace.
environment: None,
analyze_mode: AnalyzeMode::Tracing,
analyze_mode: AnalyzeMode::tracing(),
// Disable tree shaking. Even side-effect-free imports need to be traced, as they will
// execute at runtime.
..Default::default()
......
......@@ -441,7 +441,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
environment: Some(env),
follow_reexports: options.follow_reexports,
module_fragments_enabled: options.module_fragments_enabled,
analyze_mode: AnalyzeMode::CodeGenerationAndTracing,
analyze_mode: AnalyzeMode::code_generation_and_tracing(),
..Default::default()
}
.resolved_cell(),
......@@ -449,7 +449,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
module_rules: vec![module_rules],
follow_reexports: options.follow_reexports,
module_fragments_enabled: options.module_fragments_enabled,
analyze_mode: AnalyzeMode::CodeGenerationAndTracing,
analyze_mode: AnalyzeMode::code_generation_and_tracing(),
..Default::default()
}
.cell(),
......
......@@ -415,7 +415,7 @@ async fn node_file_trace_operation(
// Environment is not passed in order to avoid downleveling JS / CSS for
// node-file-trace.
environment: None,
analyze_mode: AnalyzeMode::Tracing,
analyze_mode: AnalyzeMode::tracing(),
// Disable tree shaking. Even side-effect-free imports need to be traced, as they will
// execute at runtime.
follow_reexports: false,
......
......@@ -305,7 +305,7 @@ async fn node_file_trace_operation(
// Environment is not passed in order to avoid downleveling JS / CSS for
// node-file-trace.
environment: None,
analyze_mode: AnalyzeMode::Tracing,
analyze_mode: AnalyzeMode::tracing(),
// Disable tree shaking. Even side-effect-free imports need to be traced, as they will
// execute at runtime.
follow_reexports: false,
......
......@@ -924,7 +924,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,
analyze_mode: AnalyzeMode::Tracing,
analyze_mode: AnalyzeMode::tracing(),
module_rules: prune_rules,
// Disable tree shaking. Even side-effect-free imports need to be traced, as they will
// execute at runtime.
......
......@@ -27,10 +27,9 @@ use turbopack_core::{
};
use turbopack_css::CssModuleType;
use turbopack_ecmascript::{
AnalyzeMode, EcmascriptInputTransform, EcmascriptInputTransforms, EcmascriptOptions,
SpecifiedModuleType, bytes_source_transform::BytesSourceTransform,
json_source_transform::JsonSourceTransform, text_source_transform::TextSourceTransform,
transform::PresetEnvConfig,
EcmascriptInputTransform, EcmascriptInputTransforms, EcmascriptOptions, SpecifiedModuleType,
bytes_source_transform::BytesSourceTransform, json_source_transform::JsonSourceTransform,
text_source_transform::TextSourceTransform, transform::PresetEnvConfig,
};
use turbopack_mdx::MdxTransform;
use turbopack_node::{
......@@ -405,7 +404,7 @@ impl ModuleOptions {
// which produces virtual paths that don't exist on disk. This breaks NFT file tracing
// and standalone build file copying. Use Raw module type instead so the original
// filesystem path is preserved in the trace.
let is_tracing = analyze_mode == AnalyzeMode::Tracing;
let is_only_tracing = !analyze_mode.is_codegen;
// Import attribute rules (bytes/text) must come BEFORE config rules.
// Import attributes have a stronger API contract - they're explicit in the source code
......@@ -415,7 +414,7 @@ impl ModuleOptions {
RuleCondition::ReferenceType(ReferenceTypeCondition::EcmaScriptModules(Some(
EcmaScriptModulesReferenceSubType::ImportWithType("bytes".into()),
))),
if is_tracing {
if is_only_tracing {
vec![ModuleRuleEffect::ModuleType(ModuleType::Raw)]
} else {
vec![ModuleRuleEffect::SourceTransforms(ResolvedVc::cell(vec![
......@@ -429,7 +428,7 @@ impl ModuleOptions {
RuleCondition::ReferenceType(ReferenceTypeCondition::EcmaScriptModules(Some(
EcmaScriptModulesReferenceSubType::ImportWithType("text".into()),
))),
if is_tracing {
if is_only_tracing {
vec![ModuleRuleEffect::ModuleType(ModuleType::Raw)]
} else {
vec![ModuleRuleEffect::SourceTransforms(ResolvedVc::cell(vec![
......@@ -611,7 +610,7 @@ impl ModuleOptions {
RuleCondition::ReferenceType(ReferenceTypeCondition::EcmaScriptModules(Some(
EcmaScriptModulesReferenceSubType::ImportWithType("json".into()),
))),
if is_tracing {
if is_only_tracing {
vec![ModuleRuleEffect::ModuleType(ModuleType::Raw)]
} else {
vec![ModuleRuleEffect::SourceTransforms(ResolvedVc::cell(vec![
......@@ -629,7 +628,7 @@ impl ModuleOptions {
RuleCondition::ResourcePathEndsWith(".json".to_string()),
RuleCondition::ContentTypeStartsWith("application/json".to_string()),
]),
if is_tracing {
if is_only_tracing {
vec![ModuleRuleEffect::ModuleType(ModuleType::Raw)]
} else {
vec![ModuleRuleEffect::SourceTransforms(ResolvedVc::cell(vec![
......@@ -705,7 +704,7 @@ impl ModuleOptions {
RuleCondition::ResourcePathEndsWith(".wasm".to_string()),
RuleCondition::ContentTypeStartsWith("application/wasm".to_string()),
]),
if is_tracing {
if is_only_tracing {
vec![ModuleRuleEffect::ModuleType(ModuleType::Raw)]
} else {
vec![ModuleRuleEffect::ModuleType(ModuleType::WebAssembly {
......@@ -717,7 +716,7 @@ impl ModuleOptions {
RuleCondition::any(vec![RuleCondition::ResourcePathEndsWith(
".wat".to_string(),
)]),
if is_tracing {
if is_only_tracing {
vec![ModuleRuleEffect::ModuleType(ModuleType::Raw)]
} else {
vec![ModuleRuleEffect::ModuleType(ModuleType::WebAssembly {
......
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