From b311251cc8fe3b4b60d015b408c7a14ff8fb9e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 17 Jul 2024 15:16:14 +0900 Subject: [PATCH] feat: Enable tree shaking in next.js (#8755) --- .gitattributes | 1 + .../turbopack-ecmascript/benches/analyzer.rs | 2 +- .../src/analyzer/graph.rs | 3 +- .../src/analyzer/imports.rs | 20 +- .../turbopack-ecmascript/src/analyzer/mod.rs | 3 +- crates/turbopack-ecmascript/src/lib.rs | 8 + crates/turbopack-ecmascript/src/minify.rs | 7 +- crates/turbopack-ecmascript/src/parse.rs | 1 - .../src/references/esm/base.rs | 3 + .../src/references/mod.rs | 9 +- .../src/tree_shake/asset.rs | 2 +- .../src/tree_shake/cjs_finder.rs | 11 - .../src/tree_shake/graph.rs | 194 ++- .../src/tree_shake/mod.rs | 130 +- .../src/tree_shake/util.rs | 304 +++- .../tests/tree-shaker/analyzer/1/output.md | 58 + .../tests/tree-shaker/analyzer/2/output.md | 58 + .../tests/tree-shaker/analyzer/3/output.md | 38 + .../analyzer/amphtml-document/input.js | 34 + .../analyzer/amphtml-document/output.md | 596 +++++++ .../tree-shaker/analyzer/app-route/output.md | 61 +- .../analyzer/combined-export/output.md | 22 + .../tree-shaker/analyzer/complex/output.md | 55 +- .../analyzer/export-named/output.md | 20 +- .../tree-shaker/analyzer/failed-1/output.md | 28 + .../tree-shaker/analyzer/failed-2/output.md | 104 +- .../tree-shaker/analyzer/failed-3/output.md | 204 ++- .../tree-shaker/analyzer/grouping/output.md | 22 + .../analyzer/ipc-evaluate/output.md | 100 +- .../tree-shaker/analyzer/ipc-index/output.md | 204 ++- .../tree-shaker/analyzer/let-bug-1/input.js | 44 + .../tree-shaker/analyzer/let-bug-1/output.md | 465 +++++ .../tree-shaker/analyzer/logger/input.js | 44 + .../tree-shaker/analyzer/logger/output.md | 465 +++++ .../analyzer/multi-export/output.md | 26 +- .../tree-shaker/analyzer/nanoid/input.js | 44 + .../tree-shaker/analyzer/nanoid/output.md | 960 +++++++++++ .../analyzer/next-response/input.js | 122 ++ .../analyzer/next-response/output.md | 1161 +++++++++++++ .../analyzer/nextjs-tracer/input.js | 214 +++ .../analyzer/nextjs-tracer/output.md | 1516 +++++++++++++++++ .../tree-shaker/analyzer/node-fetch/input.js | 9 + .../tree-shaker/analyzer/node-fetch/output.md | 278 +++ .../analyzer/node-globals/output.md | 10 + .../tree-shaker/analyzer/otel-core/input.js | 27 + .../tree-shaker/analyzer/otel-core/output.md | 355 ++++ .../analyzer/route-handler/input.js | 9 + .../analyzer/route-handler/output.md | 252 +++ .../tree-shaker/analyzer/route-kind/output.md | 16 + .../tree-shaker/analyzer/shared-2/output.md | 100 +- .../shared-and-side-effects/output.md | 150 +- .../analyzer/shared-regression/output.md | 100 +- .../analyzer/simple-vars-1/output.md | 22 + .../tree-shaker/analyzer/simple/output.md | 22 + .../analyzer/template-pages/output.md | 571 +++++-- .../analyzer/test-config-1/output.md | 66 + .../tree-shaker/analyzer/tla-1/output.md | 34 +- .../tree-shaker/analyzer/typeof-1/input.js | 10 + .../tree-shaker/analyzer/typeof-1/output.md | 298 ++++ .../analyzer/write-order/output.md | 37 + ...e-shake_dynamic-import_input_lib_05eac0.js | 4 +- ...ake_dynamic-import_input_lib_05eac0.js.map | 16 +- ...-shake_export-namespace_input_e0a7d8._.js} | 30 +- ...ke_export-namespace_input_e0a7d8._.js.map} | 26 +- ...-shake_import-namespace_input_87afd8._.js} | 30 +- ...ke_import-namespace_input_87afd8._.js.map} | 22 +- ...hake_require-side-effect_input_fa7732._.js | 4 +- ..._require-side-effect_input_fa7732._.js.map | 20 +- ...ke_tree-shake-test-1_input_index_2951af.js | 4 +- ...ree-shake-test-1_input_index_2951af.js.map | 16 +- crates/turbopack-wasm/src/module_asset.rs | 36 +- crates/turbopack/src/module_options/mod.rs | 2 + .../module_options/module_options_context.rs | 1 + 73 files changed, 9349 insertions(+), 591 deletions(-) delete mode 100644 crates/turbopack-ecmascript/src/tree_shake/cjs_finder.rs create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/amphtml-document/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/amphtml-document/output.md create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/let-bug-1/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/let-bug-1/output.md create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/logger/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/logger/output.md create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nanoid/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nanoid/output.md create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/next-response/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/next-response/output.md create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nextjs-tracer/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nextjs-tracer/output.md create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-fetch/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-fetch/output.md create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/otel-core/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/otel-core/output.md create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-handler/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-handler/output.md create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/typeof-1/input.js create mode 100644 crates/turbopack-ecmascript/tests/tree-shaker/analyzer/typeof-1/output.md rename crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/{79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js => 79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js} (91%) rename crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/{79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js.map => 79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js.map} (89%) rename crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/{79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js => 79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js} (90%) rename crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/{79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js.map => 79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js.map} (89%) diff --git a/.gitattributes b/.gitattributes index 650dc002a59d5..860411fb2b3c6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,5 @@ .config/ast-grep/rule-tests/__snapshots__/** linguist-generated=true crates/turbo-tasks-macros-tests/tests/**/*.stderr linguist-generated=true +crates/turbopack-ecmascript/tests/tree-shaker/analyzer/**/output.md linguist-generated=true crates/turbopack-tests/tests/snapshot/**/output/** linguist-generated=true crates/turborepo-lockfiles/fixtures/*.lock text eol=lf diff --git a/crates/turbopack-ecmascript/benches/analyzer.rs b/crates/turbopack-ecmascript/benches/analyzer.rs index 4b70575de1e98..0a94684f5ed9b 100644 --- a/crates/turbopack-ecmascript/benches/analyzer.rs +++ b/crates/turbopack-ecmascript/benches/analyzer.rs @@ -56,7 +56,7 @@ pub fn benchmark(c: &mut Criterion) { program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false)); let eval_context = - EvalContext::new(&program, unresolved_mark, top_level_mark, false, None); + EvalContext::new(&program, unresolved_mark, top_level_mark, None); let var_graph = create_graph(&program, &eval_context); let input = BenchInput { diff --git a/crates/turbopack-ecmascript/src/analyzer/graph.rs b/crates/turbopack-ecmascript/src/analyzer/graph.rs index 05dd2f59e6646..8105ae870289f 100644 --- a/crates/turbopack-ecmascript/src/analyzer/graph.rs +++ b/crates/turbopack-ecmascript/src/analyzer/graph.rs @@ -270,13 +270,12 @@ impl EvalContext { module: &Program, unresolved_mark: Mark, top_level_mark: Mark, - skip_namespace: bool, source: Option>>, ) -> Self { Self { unresolved_mark, top_level_mark, - imports: ImportMap::analyze(module, skip_namespace, source), + imports: ImportMap::analyze(module, source), } } diff --git a/crates/turbopack-ecmascript/src/analyzer/imports.rs b/crates/turbopack-ecmascript/src/analyzer/imports.rs index 7221c0a710091..ba874d0c3ca44 100644 --- a/crates/turbopack-ecmascript/src/analyzer/imports.rs +++ b/crates/turbopack-ecmascript/src/analyzer/imports.rs @@ -139,8 +139,6 @@ pub(crate) struct ImportMap { pub(crate) enum ImportedSymbol { ModuleEvaluation, Symbol(JsWord), - /// User requested the whole module - Namespace, Exports, Part(u32), } @@ -202,16 +200,11 @@ impl ImportMap { } /// Analyze ES import - pub(super) fn analyze( - m: &Program, - skip_namespace: bool, - source: Option>>, - ) -> Self { + pub(super) fn analyze(m: &Program, source: Option>>) -> Self { let mut data = ImportMap::default(); m.visit_with(&mut Analyzer { data: &mut data, - skip_namespace, source, }); @@ -221,7 +214,6 @@ impl ImportMap { struct Analyzer<'a> { data: &'a mut ImportMap, - skip_namespace: bool, source: Option>>, } @@ -233,10 +225,6 @@ impl<'a> Analyzer<'a> { imported_symbol: ImportedSymbol, annotations: ImportAnnotations, ) -> Option { - if self.skip_namespace && matches!(imported_symbol, ImportedSymbol::Namespace) { - return None; - } - let issue_source = self .source .map(|s| IssueSource::from_swc_offsets(s, span.lo.to_usize(), span.hi.to_usize())); @@ -339,7 +327,7 @@ impl Visit for Analyzer<'_> { let i = self.ensure_reference( export.span, export.src.value.clone(), - symbol.unwrap_or(ImportedSymbol::Namespace), + symbol.unwrap_or(ImportedSymbol::Exports), annotations, ); if let Some(i) = i { @@ -455,7 +443,7 @@ fn get_import_symbol_from_import(specifier: &ImportSpecifier) -> ImportedSymbol _ => local.sym.clone(), }), ImportSpecifier::Default(..) => ImportedSymbol::Symbol(js_word!("default")), - ImportSpecifier::Namespace(..) => ImportedSymbol::Namespace, + ImportSpecifier::Namespace(..) => ImportedSymbol::Exports, } } @@ -465,6 +453,6 @@ fn get_import_symbol_from_export(specifier: &ExportSpecifier) -> ImportedSymbol ImportedSymbol::Symbol(orig_name(orig)) } ExportSpecifier::Default(..) => ImportedSymbol::Symbol(js_word!("default")), - ExportSpecifier::Namespace(..) => ImportedSymbol::Namespace, + ExportSpecifier::Namespace(..) => ImportedSymbol::Exports, } } diff --git a/crates/turbopack-ecmascript/src/analyzer/mod.rs b/crates/turbopack-ecmascript/src/analyzer/mod.rs index 54f229549a26b..d397d1873a2c3 100644 --- a/crates/turbopack-ecmascript/src/analyzer/mod.rs +++ b/crates/turbopack-ecmascript/src/analyzer/mod.rs @@ -3588,8 +3588,7 @@ mod tests { let top_level_mark = Mark::new(); m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false)); - let eval_context = - EvalContext::new(&m, unresolved_mark, top_level_mark, false, None); + let eval_context = EvalContext::new(&m, unresolved_mark, top_level_mark, None); let mut var_graph = create_graph(&m, &eval_context); diff --git a/crates/turbopack-ecmascript/src/lib.rs b/crates/turbopack-ecmascript/src/lib.rs index 114a6d2a218ec..51fc0bc516f36 100644 --- a/crates/turbopack-ecmascript/src/lib.rs +++ b/crates/turbopack-ecmascript/src/lib.rs @@ -120,6 +120,9 @@ pub enum TreeShakingMode { ReexportsOnly, } +#[turbo_tasks::value(transparent)] +pub struct OptionTreeShaking(pub Option); + #[turbo_tasks::value(shared, serialization = "auto_for_input")] #[derive(PartialOrd, Ord, Hash, Debug, Default, Copy, Clone)] pub struct EcmascriptOptions { @@ -139,6 +142,11 @@ pub struct EcmascriptOptions { /// If false, they will reference the whole directory. If true, they won't /// reference anything and lead to an runtime error instead. pub ignore_dynamic_requests: bool, + + /// The list of export names that should make tree shaking bail off. This is + /// required because tree shaking can split imports like `export const + /// runtime = 'edge'` as a separate module. + pub special_exports: Vc>, } #[turbo_tasks::value(serialization = "auto_for_input")] diff --git a/crates/turbopack-ecmascript/src/minify.rs b/crates/turbopack-ecmascript/src/minify.rs index f02a3517a30b0..f94859d23d8c3 100644 --- a/crates/turbopack-ecmascript/src/minify.rs +++ b/crates/turbopack-ecmascript/src/minify.rs @@ -54,9 +54,12 @@ pub async fn minify(path: Vc, code: Vc) -> Result GLOBALS.set(&Default::default(), || { let program = match parser.parse_program() { Ok(program) => program, - Err(_error) => { + Err(err) => { // TODO should emit an issue - bail!("failed to parse source code") + bail!( + "failed to parse source code\n{err:?}\n{}", + code.source_code().to_str()? + ) } }; let comments = SingleThreadedComments::default(); diff --git a/crates/turbopack-ecmascript/src/parse.rs b/crates/turbopack-ecmascript/src/parse.rs index 7fae1353e5cbd..f9a61d0d19643 100644 --- a/crates/turbopack-ecmascript/src/parse.rs +++ b/crates/turbopack-ecmascript/src/parse.rs @@ -432,7 +432,6 @@ async fn parse_content( &parsed_program, unresolved_mark, top_level_mark, - false, Some(source), ); diff --git a/crates/turbopack-ecmascript/src/references/esm/base.rs b/crates/turbopack-ecmascript/src/references/esm/base.rs index 5286850437a42..8852d61a52442 100644 --- a/crates/turbopack-ecmascript/src/references/esm/base.rs +++ b/crates/turbopack-ecmascript/src/references/esm/base.rs @@ -96,6 +96,7 @@ pub struct EsmAssetReference { pub issue_source: Option>, pub export_name: Option>, pub import_externals: bool, + pub special_exports: Vc>, } /// A list of [EsmAssetReference]s @@ -121,6 +122,7 @@ impl EsmAssetReference { issue_source: Option>, annotations: Value, export_name: Option>, + special_exports: Vc>, import_externals: bool, ) -> Vc { Self::cell(EsmAssetReference { @@ -130,6 +132,7 @@ impl EsmAssetReference { annotations: annotations.into_value(), export_name, import_externals, + special_exports, }) } diff --git a/crates/turbopack-ecmascript/src/references/mod.rs b/crates/turbopack-ecmascript/src/references/mod.rs index e284992e9ad58..379b4e5262368 100644 --- a/crates/turbopack-ecmascript/src/references/mod.rs +++ b/crates/turbopack-ecmascript/src/references/mod.rs @@ -353,6 +353,7 @@ struct AnalysisState<'a> { // the object allocation. first_import_meta: bool, tree_shaking_mode: Option, + special_exports: Vc>, import_externals: bool, ignore_dynamic_requests: bool, } @@ -412,6 +413,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( let options = raw_module.options; let compile_time_info = raw_module.compile_time_info; let options = options.await?; + let special_exports = options.special_exports; let import_externals = options.import_externals; let origin = Vc::upcast::>(module); @@ -428,7 +430,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( let parsed = if let Some(part) = part { let parsed = parse(source, ty, transforms); - let split_data = split(source.ident(), source, parsed); + let split_data = split(source.ident(), source, parsed, special_exports); part_of_module(split_data, part) } else { parse(source, ty, transforms) @@ -576,7 +578,6 @@ pub(crate) async fn analyse_ecmascript_module_internal( ImportedSymbol::Symbol(name) => Some(ModulePart::export((&**name).into())), ImportedSymbol::Part(part_id) => Some(ModulePart::internal(*part_id)), ImportedSymbol::Exports => Some(ModulePart::exports()), - ImportedSymbol::Namespace => None, }, Some(TreeShakingMode::ReexportsOnly) => match &r.imported_symbol { ImportedSymbol::ModuleEvaluation => { @@ -586,10 +587,10 @@ pub(crate) async fn analyse_ecmascript_module_internal( ImportedSymbol::Symbol(name) => Some(ModulePart::export((&**name).into())), ImportedSymbol::Part(part_id) => Some(ModulePart::internal(*part_id)), ImportedSymbol::Exports => None, - ImportedSymbol::Namespace => None, }, None => None, }, + special_exports, import_externals, ); @@ -814,6 +815,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( first_import_meta: true, tree_shaking_mode: options.tree_shaking_mode, import_externals: options.import_externals, + special_exports: options.special_exports, ignore_dynamic_requests: options.ignore_dynamic_requests, }; @@ -1972,6 +1974,7 @@ async fn handle_free_var_reference( .map(|export| ModulePart::export(export.clone())), None => None, }, + state.special_exports, state.import_externals, ) .resolve() diff --git a/crates/turbopack-ecmascript/src/tree_shake/asset.rs b/crates/turbopack-ecmascript/src/tree_shake/asset.rs index c7b4b7c2d7168..f27d4320f048d 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/asset.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/asset.rs @@ -23,7 +23,7 @@ use crate::{ /// This type is used for an advanced tree shkaing. #[turbo_tasks::value] pub struct EcmascriptModulePartAsset { - pub(crate) full_module: Vc, + pub full_module: Vc, pub(crate) part: Vc, pub(crate) import_externals: bool, } diff --git a/crates/turbopack-ecmascript/src/tree_shake/cjs_finder.rs b/crates/turbopack-ecmascript/src/tree_shake/cjs_finder.rs deleted file mode 100644 index 9cf44e8983818..0000000000000 --- a/crates/turbopack-ecmascript/src/tree_shake/cjs_finder.rs +++ /dev/null @@ -1,11 +0,0 @@ -use swc_core::ecma::ast::*; - -pub fn contains_cjs(m: &Program) -> bool { - if let Program::Module(m) = m { - if m.body.iter().any(|s| s.is_module_decl()) { - return false; - } - } - - true -} diff --git a/crates/turbopack-ecmascript/src/tree_shake/graph.rs b/crates/turbopack-ecmascript/src/tree_shake/graph.rs index 9531033047db0..20ef975394ed6 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/graph.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/graph.rs @@ -13,11 +13,11 @@ use swc_core::{ common::{util::take::Take, SyntaxContext, DUMMY_SP}, ecma::{ ast::{ - op, ClassDecl, Decl, DefaultDecl, ExportDecl, ExportNamedSpecifier, ExportSpecifier, - Expr, ExprStmt, FnDecl, Id, Ident, ImportDecl, ImportNamedSpecifier, ImportSpecifier, - ImportStarAsSpecifier, KeyValueProp, Lit, Module, ModuleDecl, ModuleExportName, - ModuleItem, NamedExport, ObjectLit, Prop, PropName, PropOrSpread, Stmt, VarDecl, - VarDeclKind, VarDeclarator, + op, ClassDecl, Decl, DefaultDecl, ExportAll, ExportDecl, ExportNamedSpecifier, + ExportSpecifier, Expr, ExprStmt, FnDecl, Id, Ident, ImportDecl, ImportNamedSpecifier, + ImportSpecifier, ImportStarAsSpecifier, KeyValueProp, Lit, Module, ModuleDecl, + ModuleExportName, ModuleItem, NamedExport, ObjectLit, Prop, PropName, PropOrSpread, + Stmt, VarDecl, VarDeclKind, VarDeclarator, }, atoms::JsWord, utils::{find_pat_ids, private_ident, quote_ident}, @@ -26,7 +26,9 @@ use swc_core::{ use turbo_tasks::RcStr; use super::{ - util::{ids_captured_by, ids_used_by, ids_used_by_ignoring_nested}, + util::{ + collect_top_level_decls, ids_captured_by, ids_used_by, ids_used_by_ignoring_nested, Vars, + }, Key, TURBOPACK_PART_IMPORT_SOURCE, }; use crate::magic_identifier; @@ -191,6 +193,8 @@ pub(super) struct SplitModuleResult { /// Dependency between parts. pub part_deps: FxHashMap>, pub modules: Vec, + + pub star_reexports: Vec, } impl DepGraph { @@ -226,7 +230,13 @@ impl DepGraph { let mut exports = FxHashMap::default(); let mut part_deps = FxHashMap::<_, Vec<_>>::default(); + let star_reexports: Vec<_> = data + .values() + .filter_map(|v| v.content.as_module_decl()?.as_export_all()) + .cloned() + .collect(); let mut modules = vec![]; + let mut exports_module = Module::dummy(); if groups.graph_ix.is_empty() { // If there's no dependency, all nodes are in the module evaluaiotn group. @@ -273,6 +283,24 @@ impl DepGraph { if let Some(export) = &data[item].export { exports.insert(Key::Export(export.as_str().into()), ix as u32); + + let s = ExportSpecifier::Named(ExportNamedSpecifier { + span: DUMMY_SP, + orig: ModuleExportName::Ident(Ident::new(export.clone(), DUMMY_SP)), + exported: None, + is_type_only: false, + }); + exports_module.body.push(ModuleItem::ModuleDecl( + ModuleDecl::ExportNamed(NamedExport { + span: DUMMY_SP, + specifiers: vec![s], + src: Some(Box::new(TURBOPACK_PART_IMPORT_SOURCE.into())), + type_only: false, + with: Some(Box::new(create_turbopack_part_id_assert( + PartId::Export(export.to_string().into()), + ))), + }), + )); } } ItemId::Group(ItemIdGroupKind::ModuleEvaluation) => { @@ -295,7 +323,7 @@ impl DepGraph { let data = data.get(dep_item).unwrap(); for var in data.var_decls.iter() { - if required_vars.remove(var) { + if required_vars.contains(var) { specifiers.push(ImportSpecifier::Named(ImportNamedSpecifier { span: DUMMY_SP, local: var.clone().into(), @@ -366,10 +394,21 @@ impl DepGraph { modules.push(chunk); } + exports.insert(Key::Exports, modules.len() as u32); + + for star in &star_reexports { + exports_module + .body + .push(ModuleItem::ModuleDecl(ModuleDecl::ExportAll(star.clone()))); + } + + modules.push(exports_module); + SplitModuleResult { entrypoints: exports, part_deps, modules, + star_reexports, } } @@ -429,7 +468,7 @@ impl DepGraph { let ix = self.g.get_node(id); if let ItemId::Group(_) = id { - groups.push((vec![id.clone()], FxHashSet::default())); + groups.push((vec![id.clone()], FxHashSet::default(), 1)); global_done.insert(ix); } } @@ -447,7 +486,8 @@ impl DepGraph { global_done.extend(cycle.iter().copied()); - groups.push((ids, Default::default())); + let len = ids.len(); + groups.push((ids, FxHashSet::default(), len)); } } } @@ -482,7 +522,7 @@ impl DepGraph { .count(); if dependant_count >= 2 && count_of_startings >= 2 { - groups.push((vec![id.clone()], FxHashSet::default())); + groups.push((vec![id.clone()], FxHashSet::default(), 1)); global_done.insert(ix as u32); } } @@ -490,11 +530,15 @@ impl DepGraph { loop { let mut changed = false; - for (group, group_done) in &mut groups { - let start = &group[0]; - let start_ix = self.g.get_node(start); - changed |= - add_to_group(&self.g, data, group, start_ix, &mut global_done, group_done); + for (group, group_done, init_len) in &mut groups { + // Cycle group + + for i in 0..*init_len { + let start = &group[i]; + let start_ix = self.g.get_node(start); + changed |= + add_to_group(&self.g, data, group, start_ix, &mut global_done, group_done); + } } if !changed { @@ -557,6 +601,7 @@ impl DepGraph { unresolved_ctxt: SyntaxContext, top_level_ctxt: SyntaxContext, ) -> (Vec, FxHashMap) { + let top_level_vars = collect_top_level_decls(module); let mut exports = vec![]; let mut items = FxHashMap::default(); let mut ids = vec![]; @@ -697,21 +742,41 @@ impl DepGraph { }); { - let mut used_ids = ids_used_by_ignoring_nested( - &export.decl, - unresolved_ctxt, - top_level_ctxt, - ); + let mut used_ids = if export.decl.is_fn_expr() { + ids_used_by_ignoring_nested( + &export.decl, + unresolved_ctxt, + top_level_ctxt, + &top_level_vars, + ) + } else { + ids_used_by( + &export.decl, + unresolved_ctxt, + top_level_ctxt, + &top_level_vars, + ) + }; + used_ids.read.remove(&default_var.to_id()); used_ids.write.insert(default_var.to_id()); - let captured_ids = - ids_captured_by(&export.decl, unresolved_ctxt, top_level_ctxt); + let mut captured_ids = if export.decl.is_fn_expr() { + ids_captured_by( + &export.decl, + unresolved_ctxt, + top_level_ctxt, + &top_level_vars, + ) + } else { + Vars::default() + }; + captured_ids.read.remove(&default_var.to_id()); + let data = ItemData { read_vars: used_ids.read, eventual_read_vars: captured_ids.read, write_vars: used_ids.write, eventual_write_vars: captured_ids.write, var_decls: [default_var.to_id()].into_iter().collect(), - side_effects: true, content: ModuleItem::ModuleDecl(item.clone()), ..Default::default() }; @@ -741,9 +806,14 @@ impl DepGraph { &export.expr, unresolved_ctxt, top_level_ctxt, + &top_level_vars, + ); + let captured_ids = ids_captured_by( + &export.expr, + unresolved_ctxt, + top_level_ctxt, + &top_level_vars, ); - let captured_ids = - ids_captured_by(&export.expr, unresolved_ctxt, top_level_ctxt); used_ids.write.insert(default_var.to_id()); @@ -843,11 +913,7 @@ impl DepGraph { kind: ItemIdItemKind::ImportBinding(si as _), }; ids.push(id.clone()); - let local = match s { - ImportSpecifier::Named(s) => s.local.to_id(), - ImportSpecifier::Default(s) => s.local.to_id(), - ImportSpecifier::Namespace(s) => s.local.to_id(), - }; + let local = s.local().to_id(); items.insert( id, ItemData { @@ -875,7 +941,12 @@ impl DepGraph { }; ids.push(id.clone()); - let vars = ids_used_by(&f.function, unresolved_ctxt, top_level_ctxt); + let vars = ids_used_by( + &f.function, + unresolved_ctxt, + top_level_ctxt, + &top_level_vars, + ); let var_decls = { let mut v = IndexSet::with_capacity_and_hasher(1, Default::default()); v.insert(f.ident.to_id()); @@ -905,19 +976,19 @@ impl DepGraph { }; ids.push(id.clone()); - let vars = ids_used_by(&c.class, unresolved_ctxt, top_level_ctxt); + let mut vars = + ids_used_by(&c.class, unresolved_ctxt, top_level_ctxt, &top_level_vars); let var_decls = { let mut v = IndexSet::with_capacity_and_hasher(1, Default::default()); v.insert(c.ident.to_id()); v }; + vars.write.insert(c.ident.to_id()); items.insert( id, ItemData { - is_hoisted: true, - eventual_read_vars: vars.read, - eventual_write_vars: vars.write, - write_vars: var_decls.clone(), + read_vars: vars.read, + write_vars: vars.write, var_decls, content: ModuleItem::Stmt(Stmt::Decl(Decl::Class(c.clone()))), ..Default::default() @@ -937,13 +1008,24 @@ impl DepGraph { ids.push(id.clone()); let decl_ids: Vec = find_pat_ids(&decl.name); - let vars = ids_used_by_ignoring_nested( + let vars = ids_used_by( &decl.init, unresolved_ctxt, top_level_ctxt, + &top_level_vars, ); let eventual_vars = - ids_captured_by(&decl.init, unresolved_ctxt, top_level_ctxt); + if matches!(decl.init.as_deref(), Some(Expr::Fn(..) | Expr::Arrow(..))) + { + ids_captured_by( + &decl.init, + unresolved_ctxt, + top_level_ctxt, + &top_level_vars, + ) + } else { + Vars::default() + }; let side_effects = vars.found_unresolved; @@ -972,9 +1054,14 @@ impl DepGraph { expr: box Expr::Assign(assign), .. })) => { - let mut used_ids = - ids_used_by_ignoring_nested(item, unresolved_ctxt, top_level_ctxt); - let captured_ids = ids_captured_by(item, unresolved_ctxt, top_level_ctxt); + let mut used_ids = ids_used_by_ignoring_nested( + item, + unresolved_ctxt, + top_level_ctxt, + &top_level_vars, + ); + let captured_ids = + ids_captured_by(item, unresolved_ctxt, top_level_ctxt, &top_level_vars); if assign.op != op!("=") { used_ids.read.extend(used_ids.write.iter().cloned()); @@ -983,6 +1070,7 @@ impl DepGraph { &assign.left, unresolved_ctxt, top_level_ctxt, + &top_level_vars, ); used_ids.read.extend(extra_ids.read); used_ids.write.extend(extra_ids.write); @@ -1011,15 +1099,21 @@ impl DepGraph { ModuleItem::ModuleDecl( ModuleDecl::ExportDefaultDecl(..) | ModuleDecl::ExportDefaultExpr(..) - | ModuleDecl::ExportNamed(NamedExport { .. }), + | ModuleDecl::ExportNamed(NamedExport { .. }) + | ModuleDecl::ExportAll(..), ) => {} _ => { // Default to normal - let used_ids = - ids_used_by_ignoring_nested(item, unresolved_ctxt, top_level_ctxt); - let captured_ids = ids_captured_by(item, unresolved_ctxt, top_level_ctxt); + let used_ids = ids_used_by_ignoring_nested( + item, + unresolved_ctxt, + top_level_ctxt, + &top_level_vars, + ); + let captured_ids = + ids_captured_by(item, unresolved_ctxt, top_level_ctxt, &top_level_vars); let data = ItemData { read_vars: used_ids.read, eventual_read_vars: captured_ids.read, @@ -1058,10 +1152,10 @@ impl DepGraph { for (local, export_name) in exports { let name = match &export_name { - Some(ModuleExportName::Ident(v)) => v.to_id(), - _ => local.clone(), + Some(ModuleExportName::Ident(v)) => v.sym.clone(), + _ => local.0.clone(), }; - let id = ItemId::Group(ItemIdGroupKind::Export(local.clone(), name.0.clone())); + let id = ItemId::Group(ItemIdGroupKind::Export(local.clone(), name.clone())); ids.push(id.clone()); items.insert( id.clone(), @@ -1078,8 +1172,8 @@ impl DepGraph { type_only: false, with: None, })), - read_vars: [name.clone()].into_iter().collect(), - export: Some(name.0), + read_vars: [local.clone()].into_iter().collect(), + export: Some(name), ..Default::default() }, ); diff --git a/crates/turbopack-ecmascript/src/tree_shake/mod.rs b/crates/turbopack-ecmascript/src/tree_shake/mod.rs index c72b11ecfc1ab..d5b8a8a31b557 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/mod.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/mod.rs @@ -1,13 +1,16 @@ -use std::borrow::Cow; +use std::{borrow::Cow, fmt::Write}; use anyhow::{bail, Result}; use indexmap::IndexSet; use rustc_hash::FxHashMap; use swc_core::{ common::{util::take::Take, SyntaxContext, DUMMY_SP, GLOBALS}, - ecma::ast::{ - ExportNamedSpecifier, Id, Ident, ImportDecl, Module, ModuleDecl, ModuleExportName, - ModuleItem, NamedExport, Program, + ecma::{ + ast::{ + ExportAll, ExportNamedSpecifier, Id, Ident, ImportDecl, Module, ModuleDecl, + ModuleExportName, ModuleItem, NamedExport, Program, + }, + codegen::{text_writer::JsWriter, Emitter}, }, }; use turbo_tasks::{RcStr, ValueToString, Vc}; @@ -21,7 +24,6 @@ use crate::{analyzer::graph::EvalContext, parse::ParseResult, EcmascriptModuleAs pub mod asset; pub mod chunk_item; -mod cjs_finder; mod graph; pub mod merge; #[cfg(test)] @@ -110,6 +112,10 @@ impl Analyzer<'_> { for id in item.var_decls.iter() { let state = self.vars.entry(id.clone()).or_default(); + if state.declarator.is_none() { + state.declarator = Some(item_id.clone()); + } + if item.is_hoisted { state.last_writes.push(item_id.clone()); } else { @@ -171,7 +177,11 @@ impl Analyzer<'_> { if let Some(declarator) = &state.declarator { if declarator != item_id { // A write also depends on the declaration. - self.g.add_weak_deps(item_id, [declarator].iter().copied()); + if item.side_effects { + self.g.add_strong_deps(item_id, [declarator]); + } else { + self.g.add_weak_deps(item_id, [declarator]); + } } } } @@ -256,6 +266,13 @@ impl Analyzer<'_> { let state = get_var(&self.vars, id); self.g.add_strong_deps(item_id, state.last_writes.iter()); + + if let Some(declarator) = &state.declarator { + if declarator != item_id { + // A read also depends on the declaration. + self.g.add_strong_deps(item_id, [declarator]); + } + } } // For each var in EVENTUAL_WRITE_VARS: @@ -266,6 +283,13 @@ impl Analyzer<'_> { let state = get_var(&self.vars, id); self.g.add_weak_deps(item_id, state.last_reads.iter()); + + if let Some(declarator) = &state.declarator { + if declarator != item_id { + // A write also depends on the declaration. + self.g.add_strong_deps(item_id, [declarator]); + } + } } // (no state update happens, since this is only triggered by @@ -302,6 +326,7 @@ impl Analyzer<'_> { pub(crate) enum Key { ModuleEvaluation, Export(RcStr), + Exports, } /// Converts [Vc] to the index. @@ -312,9 +337,9 @@ async fn get_part_id(result: &SplitResult, part: Vc) -> Result let key = match &*part { ModulePart::Evaluation => Key::ModuleEvaluation, ModulePart::Export(export) => Key::Export(export.await?.as_str().into()), + ModulePart::Exports => Key::Exports, ModulePart::Internal(part_id) => return Ok(*part_id), ModulePart::Locals - | ModulePart::Exports | ModulePart::Facade | ModulePart::RenamedExport { .. } | ModulePart::RenamedNamespace { .. } => { @@ -322,23 +347,64 @@ async fn get_part_id(result: &SplitResult, part: Vc) -> Result } }; - let entrypoints = match &result { - SplitResult::Ok { entrypoints, .. } => entrypoints, - _ => bail!("split failed"), + let SplitResult::Ok { + entrypoints, + modules, + .. + } = &result + else { + bail!("split failed") }; - let part_id = match entrypoints.get(&key) { - Some(id) => *id, - None => { - bail!( - "could not find part id for module part {:?} in {:?}", - key, - entrypoints - ) + if let Some(id) = entrypoints.get(&key) { + return Ok(*id); + } + + // This is required to handle `export * from 'foo'` + if let ModulePart::Export(..) = &*part { + if let Some(&v) = entrypoints.get(&Key::Exports) { + return Ok(v); } - }; + } + + let mut dump = String::new(); + + for (idx, m) in modules.iter().enumerate() { + let ParseResult::Ok { + program, + source_map, + .. + } = &*m.await? + else { + bail!("failed to get module") + }; + + { + let mut buf = vec![]; + + { + let wr = JsWriter::new(Default::default(), "\n", &mut buf, None); - Ok(part_id) + let mut emitter = Emitter { + cfg: Default::default(), + comments: None, + cm: source_map.clone(), + wr, + }; + + emitter.emit_program(program).unwrap(); + } + let code = String::from_utf8(buf).unwrap(); + + writeln!(dump, "# Module #{idx}:\n{code}\n\n\n")?; + } + } + + bail!( + "could not find part id for module part {:?} in {:?}\n\nModule dump:\n{dump}", + key, + entrypoints + ) } #[turbo_tasks::value(shared, serialization = "none", eq = "manual")] @@ -355,6 +421,9 @@ pub(crate) enum SplitResult { #[turbo_tasks(trace_ignore)] deps: FxHashMap>, + + #[turbo_tasks(debug_ignore, trace_ignore)] + star_reexports: Vec, }, Failed { parse_result: Vc, @@ -372,7 +441,12 @@ impl PartialEq for SplitResult { #[turbo_tasks::function] pub(super) async fn split_module(asset: Vc) -> Result> { - Ok(split(asset.source().ident(), asset.source(), asset.parse())) + Ok(split( + asset.source().ident(), + asset.source(), + asset.parse(), + asset.options().await?.special_exports, + )) } #[turbo_tasks::function] @@ -380,6 +454,7 @@ pub(super) async fn split( ident: Vc, source: Vc>, parsed: Vc, + special_exports: Vc>, ) -> Result> { let parse_result = parsed.await?; @@ -393,7 +468,7 @@ pub(super) async fn split( .. } => { // If the script file is a common js file, we cannot split the module - if cjs_finder::contains_cjs(program) { + if util::should_skip_tree_shaking(program, &special_exports.await?) { return Ok(SplitResult::Failed { parse_result: parsed, } @@ -419,6 +494,7 @@ pub(super) async fn split( entrypoints, part_deps, modules, + star_reexports, } = dep_graph.split_module(&items); assert_ne!(modules.len(), 0, "modules.len() == 0;\nModule: {module:?}",); @@ -431,7 +507,6 @@ pub(super) async fn split( modules.len() ); } - let modules = modules .into_iter() .map(|module| { @@ -440,7 +515,6 @@ pub(super) async fn split( &program, eval_context.unresolved_mark, eval_context.top_level_mark, - false, Some(source), ); @@ -459,6 +533,7 @@ pub(super) async fn split( entrypoints, deps: part_deps, modules, + star_reexports, } .cell()) } @@ -483,6 +558,7 @@ pub(super) async fn part_of_module( modules, entrypoints, deps, + star_reexports, .. } => { debug_assert_ne!(modules.len(), 0, "modules.len() == 0"); @@ -557,7 +633,6 @@ pub(super) async fn part_of_module( &program, eval_context.unresolved_mark, eval_context.top_level_mark, - true, None, ); return Ok(ParseResult::Ok { @@ -624,12 +699,15 @@ pub(super) async fn part_of_module( ))); } + module.body.extend(star_reexports.iter().map(|export_all| { + ModuleItem::ModuleDecl(ModuleDecl::ExportAll(export_all.clone())) + })); + let program = Program::Module(module); let eval_context = EvalContext::new( &program, eval_context.unresolved_mark, eval_context.top_level_mark, - true, None, ); return Ok(ParseResult::Ok { diff --git a/crates/turbopack-ecmascript/src/tree_shake/util.rs b/crates/turbopack-ecmascript/src/tree_shake/util.rs index 79224046c7dd7..050f021e0c65e 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/util.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/util.rs @@ -1,17 +1,22 @@ use std::hash::BuildHasherDefault; use indexmap::IndexSet; -use rustc_hash::FxHasher; +use rustc_hash::{FxHashSet, FxHasher}; use swc_core::{ common::SyntaxContext, ecma::{ ast::{ - AssignTarget, BlockStmtOrExpr, Constructor, ExportNamedSpecifier, ExportSpecifier, - Expr, Function, Id, Ident, MemberExpr, MemberProp, NamedExport, Pat, PropName, + ArrowExpr, AssignPatProp, AssignTarget, ClassDecl, ClassExpr, Constructor, DefaultDecl, + ExportDefaultDecl, ExportNamedSpecifier, ExportSpecifier, Expr, FnDecl, FnExpr, + Function, Id, Ident, ImportSpecifier, MemberExpr, MemberProp, NamedExport, Param, Pat, + Prop, PropName, VarDeclarator, *, }, visit::{noop_visit_type, Visit, VisitWith}, }, }; +use turbo_tasks::RcStr; + +use crate::TURBOPACK_HELPER; #[derive(Debug, Default, Clone, Copy)] enum Mode { @@ -27,10 +32,11 @@ struct Target { } /// A visitor which collects variables which are read or written. -#[derive(Default)] -pub(crate) struct IdentUsageCollector { +pub(crate) struct IdentUsageCollector<'a> { unresolved: SyntaxContext, top_level: SyntaxContext, + top_level_vars: &'a FxHashSet, + vars: Vars, is_nested: bool, @@ -39,7 +45,7 @@ pub(crate) struct IdentUsageCollector { mode: Option, } -impl IdentUsageCollector { +impl IdentUsageCollector<'_> { fn with_nested(&mut self, f: impl FnOnce(&mut Self)) { if !self.target.eventual { return; @@ -58,19 +64,13 @@ impl IdentUsageCollector { } } -impl Visit for IdentUsageCollector { +impl Visit for IdentUsageCollector<'_> { fn visit_assign_target(&mut self, n: &AssignTarget) { self.with_mode(Some(Mode::Write), |this| { n.visit_children_with(this); }) } - fn visit_block_stmt_or_expr(&mut self, n: &BlockStmtOrExpr) { - self.with_nested(|this| { - n.visit_children_with(this); - }) - } - fn visit_constructor(&mut self, n: &Constructor) { self.with_nested(|this| { n.visit_children_with(this); @@ -114,6 +114,7 @@ impl Visit for IdentUsageCollector { if n.span.ctxt != self.unresolved && n.span.ctxt != self.top_level && n.span.ctxt != SyntaxContext::empty() + && !self.top_level_vars.contains(&n.to_id()) { return; } @@ -168,6 +169,16 @@ impl Visit for IdentUsageCollector { } } + fn visit_prop(&mut self, n: &Prop) { + match n { + Prop::Shorthand(v) => { + self.with_mode(None, |c| c.visit_ident(v)); + } + + _ => n.visit_children_with(self), + } + } + noop_visit_type!(); } @@ -186,18 +197,26 @@ pub(crate) struct Vars { /// /// Note: This functions accept `SyntaxContext` to filter out variables which /// are not interesting. We only need to analyze top-level variables. -pub(crate) fn ids_captured_by(n: &N, unresolved: SyntaxContext, top_level: SyntaxContext) -> Vars +pub(crate) fn ids_captured_by<'a, N>( + n: &N, + unresolved: SyntaxContext, + top_level: SyntaxContext, + top_level_vars: &'a FxHashSet, +) -> Vars where - N: VisitWith, + N: VisitWith>, { let mut v = IdentUsageCollector { unresolved, top_level, + top_level_vars, target: Target { direct: false, eventual: true, }, - ..Default::default() + vars: Vars::default(), + is_nested: false, + mode: None, }; n.visit_with(&mut v); v.vars @@ -207,18 +226,26 @@ where /// /// Note: This functions accept `SyntaxContext` to filter out variables which /// are not interesting. We only need to analyze top-level variables. -pub(crate) fn ids_used_by(n: &N, unresolved: SyntaxContext, top_level: SyntaxContext) -> Vars +pub(crate) fn ids_used_by<'a, N>( + n: &N, + unresolved: SyntaxContext, + top_level: SyntaxContext, + top_level_vars: &'a FxHashSet, +) -> Vars where - N: VisitWith, + N: VisitWith>, { let mut v = IdentUsageCollector { unresolved, top_level, + top_level_vars, target: Target { direct: true, eventual: true, }, - ..Default::default() + vars: Vars::default(), + is_nested: false, + mode: None, }; n.visit_with(&mut v); v.vars @@ -228,23 +255,256 @@ where /// /// Note: This functions accept `SyntaxContext` to filter out variables which /// are not interesting. We only need to analyze top-level variables. -pub(crate) fn ids_used_by_ignoring_nested( +pub(crate) fn ids_used_by_ignoring_nested<'a, N>( n: &N, unresolved: SyntaxContext, top_level: SyntaxContext, + top_level_vars: &'a FxHashSet, ) -> Vars where - N: VisitWith, + N: VisitWith>, { let mut v = IdentUsageCollector { unresolved, top_level, + top_level_vars, target: Target { direct: true, eventual: false, }, - ..Default::default() + vars: Vars::default(), + is_nested: false, + mode: None, }; n.visit_with(&mut v); v.vars } + +pub struct TopLevelBindingCollector { + bindings: FxHashSet, + is_pat_decl: bool, +} + +impl TopLevelBindingCollector { + fn add(&mut self, i: &Ident) { + self.bindings.insert(i.to_id()); + } +} + +impl Visit for TopLevelBindingCollector { + noop_visit_type!(); + + fn visit_arrow_expr(&mut self, _: &ArrowExpr) {} + + fn visit_assign_pat_prop(&mut self, node: &AssignPatProp) { + node.value.visit_with(self); + + if self.is_pat_decl { + self.add(&node.key); + } + } + + fn visit_class_decl(&mut self, node: &ClassDecl) { + self.add(&node.ident); + } + + fn visit_expr(&mut self, _: &Expr) {} + + fn visit_export_default_decl(&mut self, e: &ExportDefaultDecl) { + match &e.decl { + DefaultDecl::Class(ClassExpr { + ident: Some(ident), .. + }) => { + self.add(ident); + } + DefaultDecl::Fn(FnExpr { + ident: Some(ident), + function: f, + }) if f.body.is_some() => { + self.add(ident); + } + _ => {} + } + } + + fn visit_fn_decl(&mut self, node: &FnDecl) { + self.add(&node.ident); + } + + fn visit_import_specifier(&mut self, node: &ImportSpecifier) { + match node { + ImportSpecifier::Named(s) => self.add(&s.local), + ImportSpecifier::Default(s) => { + self.add(&s.local); + } + ImportSpecifier::Namespace(s) => { + self.add(&s.local); + } + } + } + + fn visit_param(&mut self, node: &Param) { + let old = self.is_pat_decl; + self.is_pat_decl = true; + node.visit_children_with(self); + self.is_pat_decl = old; + } + + fn visit_pat(&mut self, node: &Pat) { + node.visit_children_with(self); + + if self.is_pat_decl { + if let Pat::Ident(i) = node { + self.add(&i.id) + } + } + } + + fn visit_var_declarator(&mut self, node: &VarDeclarator) { + let old = self.is_pat_decl; + self.is_pat_decl = true; + node.name.visit_with(self); + + self.is_pat_decl = false; + node.init.visit_with(self); + self.is_pat_decl = old; + } +} + +/// Collects binding identifiers. +pub fn collect_top_level_decls(n: &N) -> FxHashSet +where + N: VisitWith, +{ + let mut v = TopLevelBindingCollector { + bindings: Default::default(), + is_pat_decl: false, + }; + n.visit_with(&mut v); + v.bindings +} + +pub fn should_skip_tree_shaking(m: &Program, special_exports: &[RcStr]) -> bool { + if let Program::Module(m) = m { + for item in m.body.iter() { + match item { + // Skip turbopack helpers. + ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl { + with, specifiers, .. + })) => { + if let Some(with) = with.as_deref().and_then(|v| v.as_import_with()) { + for item in with.values.iter() { + if item.key.sym == *TURBOPACK_HELPER { + // Skip tree shaking if the import is from turbopack-helper + return true; + } + } + } + + // TODO(PACK-3150): Tree shaking has a bug related to ModuleExportName::Str + for s in specifiers.iter() { + if let ImportSpecifier::Named(is) = s { + if matches!(is.imported, Some(ModuleExportName::Str(..))) { + return true; + } + } + } + } + + // Tree shaking has a bug related to ModuleExportName::Str + ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport { + src: Some(..), + specifiers, + .. + })) => { + for s in specifiers { + if let ExportSpecifier::Named(es) = s { + if matches!(es.orig, ModuleExportName::Str(..)) + || matches!(es.exported, Some(ModuleExportName::Str(..))) + { + return true; + } + } + } + } + + // Skip sever actions + ModuleItem::Stmt(Stmt::Expr(ExprStmt { + expr: box Expr::Lit(Lit::Str(Str { value, .. })), + .. + })) => { + if value == "use server" { + return true; + } + } + + // Skip special reexports that are recognized by next.js + ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { + decl: Decl::Var(box VarDecl { decls, .. }), + .. + })) => { + for decl in decls { + if let Pat::Ident(name) = &decl.name { + if special_exports.iter().any(|s| **s == *name.sym) { + return true; + } + } + } + } + + // Skip special reexports that are recognized by next.js + ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { + decl: Decl::Fn(f), + .. + })) => { + if special_exports.iter().any(|s| **s == *f.ident.sym) { + return true; + } + } + + _ => {} + } + } + + let mut visitor = UseServerFinder::default(); + m.visit_with(&mut visitor); + if visitor.abort { + return true; + } + + for item in m.body.iter() { + if item.is_module_decl() { + return false; + } + } + } + + true +} + +#[derive(Default)] +struct UseServerFinder { + abort: bool, +} + +impl Visit for UseServerFinder { + fn visit_expr_stmt(&mut self, e: &ExprStmt) { + e.visit_children_with(self); + + if let Expr::Lit(Lit::Str(Str { value, .. })) = &*e.expr { + if value == "use server" { + self.abort = true; + } + } + } + + fn visit_stmt(&mut self, n: &Stmt) { + if self.abort { + return; + } + + n.visit_children_with(self); + } + + noop_visit_type!(); +} diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/1/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/1/output.md index 66a2680795ce9..79c4fc3cbc295 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/1/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/1/output.md @@ -274,11 +274,14 @@ graph TD Item18 --> Item13; Item11 --> Item2; Item11 --> Item8; + Item11 --> Item3; Item12 --> Item11; Item12 --> Item8; + Item12 --> Item3; Item13 -.-> Item4; Item13 -.-> Item7; Item13 -.-> Item15; + Item13 --> Item3; ``` # Phase 4 ```mermaid @@ -331,11 +334,14 @@ graph TD Item18 --> Item13; Item11 --> Item2; Item11 --> Item8; + Item11 --> Item3; Item12 --> Item11; Item12 --> Item8; + Item12 --> Item3; Item13 -.-> Item4; Item13 -.-> Item7; Item13 -.-> Item15; + Item13 --> Item3; Item14 --> Item1; Item14 --> Item9; ``` @@ -362,9 +368,11 @@ graph TD N2 --> N6; N3 --> N10; N3 --> N9; + N3 --> N5; N4 --> N6; N4 --> N8; N4 --> N1; + N4 --> N5; N6 --> N5; N7 --> N5; N7 --> N6; @@ -375,6 +383,7 @@ graph TD N9 --> N6; N9 --> N8; N10 --> N9; + N10 --> N5; ``` # Entrypoints @@ -384,6 +393,7 @@ graph TD Export( "external1", ): 3, + Exports: 11, Export( "foo", ): 2, @@ -448,6 +458,9 @@ import { internal } from "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external1 }; function external1() { return internal() + foobar; @@ -468,6 +481,9 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 1 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external2 }; function external2() { foobar += "."; @@ -547,6 +563,9 @@ foobar += "foo"; import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; import { upper } from "module"; function internal() { return upper(foobar); @@ -558,6 +577,22 @@ export { internal } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 11 +```js +export { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foobar" +}; +export { foo } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foo" +}; +export { external1 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external1" +}; +export { external2 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external2" +}; + ``` ## Merged (module eval) ```js @@ -590,6 +625,7 @@ export { upper } from "__TURBOPACK_VAR__" assert { Export( "external1", ): 3, + Exports: 8, Export( "foo", ): 2, @@ -649,6 +685,9 @@ export { foo } from "__TURBOPACK_VAR__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external1 }; import { upper } from "module"; function internal() { @@ -670,6 +709,9 @@ export { external1 } from "__TURBOPACK_VAR__" assert { ``` ## Part 4 ```js +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external2 }; function external2() { foobar += "."; @@ -709,6 +751,22 @@ import { foobar } from "__TURBOPACK_PART__" assert { }; foobar += "foo"; +``` +## Part 8 +```js +export { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foobar" +}; +export { foo } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foo" +}; +export { external1 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external1" +}; +export { external2 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external2" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/2/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/2/output.md index d121155292e1d..073987fab8235 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/2/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/2/output.md @@ -292,11 +292,14 @@ graph TD Item19 --> Item14; Item3 --> Item13; Item3 --> Item10; + Item3 --> Item5; Item13 --> Item4; Item13 --> Item10; + Item13 --> Item5; Item14 -.-> Item6; Item14 -.-> Item9; Item14 -.-> Item17; + Item14 --> Item5; ``` # Phase 4 ```mermaid @@ -352,11 +355,14 @@ graph TD Item19 --> Item14; Item3 --> Item13; Item3 --> Item10; + Item3 --> Item5; Item13 --> Item4; Item13 --> Item10; + Item13 --> Item5; Item14 -.-> Item6; Item14 -.-> Item9; Item14 -.-> Item17; + Item14 --> Item5; Item15 --> Item1; Item15 --> Item2; Item15 --> Item11; @@ -381,12 +387,14 @@ graph TD N0 --> N6; N1 --> N10; N1 --> N9; + N1 --> N5; N2 --> N9; N2 --> N5; N3 --> N6; N4 --> N6; N4 --> N8; N4 --> N2; + N4 --> N5; N6 --> N5; N7 --> N5; N7 --> N6; @@ -397,6 +405,7 @@ graph TD N9 --> N6; N9 --> N8; N10 --> N9; + N10 --> N5; ``` # Entrypoints @@ -406,6 +415,7 @@ graph TD Export( "external1", ): 1, + Exports: 11, Export( "foo", ): 3, @@ -452,6 +462,9 @@ import { internal } from "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external1 }; function external1() { return internal() + foobar; @@ -491,6 +504,9 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 2 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external2 }; function external2() { foobar += "."; @@ -570,6 +586,9 @@ foobar += "foo"; import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; import { upper } from "module"; function internal() { return upper(foobar); @@ -581,6 +600,22 @@ export { internal } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 11 +```js +export { external1 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external1" +}; +export { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foobar" +}; +export { foo } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foo" +}; +export { external2 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external2" +}; + ``` ## Merged (module eval) ```js @@ -614,6 +649,7 @@ export { upper } from "__TURBOPACK_VAR__" assert { Export( "external1", ): 1, + Exports: 8, Export( "foo", ): 3, @@ -651,6 +687,9 @@ export { foobarCopy } from "__TURBOPACK_VAR__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external1 }; function external1() { return internal() + foobar; @@ -695,6 +734,9 @@ export { foo } from "__TURBOPACK_VAR__" assert { ``` ## Part 4 ```js +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external2 }; function external2() { foobar += "."; @@ -734,6 +776,22 @@ import { foobar } from "__TURBOPACK_PART__" assert { }; foobar += "foo"; +``` +## Part 8 +```js +export { external1 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external1" +}; +export { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foobar" +}; +export { foo } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foo" +}; +export { external2 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external2" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/3/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/3/output.md index eefa42f0d0b7a..3602e2dd5b590 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/3/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/3/output.md @@ -355,6 +355,7 @@ graph TD Export( "c2_2", ): 3, + Exports: 9, } ``` @@ -482,6 +483,19 @@ export { d3 } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 9 +```js +export { c1_1 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export c1_1" +}; +export { c1_3 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export c1_3" +}; +export { c2_2 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export c2_2" +}; + ``` ## Merged (module eval) ```js @@ -519,6 +533,7 @@ c2_2(); Export( "c2_2", ): 3, + Exports: 6, } ``` @@ -563,6 +578,7 @@ export { c2_2 }; ``` ## Part 4 ```js +function d1() {} function d2() {} function c1_1() { return c1_2(); @@ -573,6 +589,9 @@ function c1_2() { function c1_3() { return c1_1(d2); } +export { d1 } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; export { d2 } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; @@ -589,6 +608,7 @@ export { c1_3 } from "__TURBOPACK_VAR__" assert { ``` ## Part 5 ```js +function d3() {} function c2_1() { return c2_2(d3); } @@ -598,6 +618,9 @@ function c2_2() { function c2_3() { return c2_1(); } +export { d3 } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; export { c2_1 } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; @@ -608,6 +631,19 @@ export { c2_3 } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 6 +```js +export { c1_1 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export c1_1" +}; +export { c1_3 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export c1_3" +}; +export { c2_2 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export c2_2" +}; + ``` ## Merged (module eval) ```js @@ -636,6 +672,7 @@ c2_2(); Export( "c2_2", ): 3, + Exports: 6, } ``` @@ -662,6 +699,7 @@ export { c1_3 }; Export( "c2_2", ): 3, + Exports: 6, } ``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/amphtml-document/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/amphtml-document/input.js new file mode 100644 index 0000000000000..fd849eeea7373 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/amphtml-document/input.js @@ -0,0 +1,34 @@ +import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; +import Document, { Html, Head, Main, NextScript } from 'next/document'; +class MyDocument extends Document { + static async getInitialProps(ctx) { + const initialProps = await Document.getInitialProps(ctx); + return { + ...initialProps, + styles: _jsxs(_Fragment, { + children: [ + initialProps.styles, + _jsx("style", { + dangerouslySetInnerHTML: { + __html: `html { background: hotpink; }` + } + }) + ] + }) + }; + } + render() { + return _jsxs(Html, { + children: [ + _jsx(Head, {}), + _jsxs("body", { + children: [ + _jsx(Main, {}), + _jsx(NextScript, {}) + ] + }) + ] + }); + } +} +export default MyDocument; diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/amphtml-document/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/amphtml-document/output.md new file mode 100644 index 0000000000000..0db4dfc0f65db --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/amphtml-document/output.md @@ -0,0 +1,596 @@ +# Items + +Count: 14 + +## Item 1: Stmt 0, `ImportOfModule` + +```js +import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; + +``` + +- Hoisted +- Side effects + +## Item 2: Stmt 0, `ImportBinding(0)` + +```js +import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; + +``` + +- Hoisted +- Declares: `_jsx` + +## Item 3: Stmt 0, `ImportBinding(1)` + +```js +import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; + +``` + +- Hoisted +- Declares: `_jsxs` + +## Item 4: Stmt 0, `ImportBinding(2)` + +```js +import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; + +``` + +- Hoisted +- Declares: `_Fragment` + +## Item 5: Stmt 1, `ImportOfModule` + +```js +import Document, { Html, Head, Main, NextScript } from 'next/document'; + +``` + +- Hoisted +- Side effects + +## Item 6: Stmt 1, `ImportBinding(0)` + +```js +import Document, { Html, Head, Main, NextScript } from 'next/document'; + +``` + +- Hoisted +- Declares: `Document` + +## Item 7: Stmt 1, `ImportBinding(1)` + +```js +import Document, { Html, Head, Main, NextScript } from 'next/document'; + +``` + +- Hoisted +- Declares: `Html` + +## Item 8: Stmt 1, `ImportBinding(2)` + +```js +import Document, { Html, Head, Main, NextScript } from 'next/document'; + +``` + +- Hoisted +- Declares: `Head` + +## Item 9: Stmt 1, `ImportBinding(3)` + +```js +import Document, { Html, Head, Main, NextScript } from 'next/document'; + +``` + +- Hoisted +- Declares: `Main` + +## Item 10: Stmt 1, `ImportBinding(4)` + +```js +import Document, { Html, Head, Main, NextScript } from 'next/document'; + +``` + +- Hoisted +- Declares: `NextScript` + +## Item 11: Stmt 2, `Normal` + +```js +class MyDocument extends Document { + static async getInitialProps(ctx) { + const initialProps = await Document.getInitialProps(ctx); + return { + ...initialProps, + styles: _jsxs(_Fragment, { + children: [ + initialProps.styles, + _jsx("style", { + dangerouslySetInnerHTML: { + __html: `html { background: hotpink; }` + } + }) + ] + }) + }; + } + render() { + return _jsxs(Html, { + children: [ + _jsx(Head, {}), + _jsxs("body", { + children: [ + _jsx(Main, {}), + _jsx(NextScript, {}) + ] + }) + ] + }); + } +} + +``` + +- Declares: `MyDocument` +- Reads: `Document`, `_jsxs`, `_Fragment`, `_jsx`, `Html`, `Head`, `Main`, `NextScript` +- Write: `Document`, `MyDocument` + +## Item 12: Stmt 3, `Normal` + +```js +export default MyDocument; + +``` + +- Side effects +- Declares: `__TURBOPACK__default__export__` +- Reads: `MyDocument` +- Write: `__TURBOPACK__default__export__` + +# Phase 1 +```mermaid +graph TD + Item1; + Item3; + Item4; + Item5; + Item2; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item13["ModuleEvaluation"]; + Item14; + Item14["export default"]; + Item2 --> Item1; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item3; + Item4; + Item5; + Item2; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item13["ModuleEvaluation"]; + Item14; + Item14["export default"]; + Item2 --> Item1; + Item11 --> Item6; + Item11 --> Item4; + Item11 --> Item5; + Item11 --> Item3; + Item11 --> Item7; + Item11 --> Item8; + Item11 --> Item9; + Item11 --> Item10; + Item12 --> Item11; + Item12 --> Item1; + Item12 --> Item2; + Item14 --> Item12; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item3; + Item4; + Item5; + Item2; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item13["ModuleEvaluation"]; + Item14; + Item14["export default"]; + Item2 --> Item1; + Item11 --> Item6; + Item11 --> Item4; + Item11 --> Item5; + Item11 --> Item3; + Item11 --> Item7; + Item11 --> Item8; + Item11 --> Item9; + Item11 --> Item10; + Item12 --> Item11; + Item12 --> Item1; + Item12 --> Item2; + Item14 --> Item12; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item3; + Item4; + Item5; + Item2; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item13["ModuleEvaluation"]; + Item14; + Item14["export default"]; + Item2 --> Item1; + Item11 --> Item6; + Item11 --> Item4; + Item11 --> Item5; + Item11 --> Item3; + Item11 --> Item7; + Item11 --> Item8; + Item11 --> Item9; + Item11 --> Item10; + Item12 --> Item11; + Item12 --> Item1; + Item12 --> Item2; + Item14 --> Item12; + Item13 --> Item1; + Item13 --> Item2; + Item13 --> Item12; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation)]"]; + N1["Items: [ItemId(Export(("__TURBOPACK__default__export__", #5), "default"))]"]; + N2["Items: [ItemId(0, ImportOfModule)]"]; + N3["Items: [ItemId(1, ImportOfModule)]"]; + N4["Items: [ItemId(0, ImportBinding(0)), ItemId(0, ImportBinding(1)), ItemId(0, ImportBinding(2)), ItemId(1, ImportBinding(0)), ItemId(1, ImportBinding(1)), ItemId(1, ImportBinding(2)), ItemId(1, ImportBinding(3)), ItemId(1, ImportBinding(4)), ItemId(2, Normal), ItemId(3, Normal)]"]; + N0 --> N2; + N0 --> N3; + N0 --> N4; + N1 --> N4; + N3 --> N2; + N4 --> N2; + N4 --> N3; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "default", + ): 1, + Exports: 5, +} +``` + + +# Modules (dev) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { __TURBOPACK__default__export__ } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +export { __TURBOPACK__default__export__ as default }; + +``` +## Part 2 +```js +import "react/jsx-runtime"; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import 'next/document'; + +``` +## Part 4 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { jsx as _jsx } from "react/jsx-runtime"; +import { jsxs as _jsxs } from "react/jsx-runtime"; +import { Fragment as _Fragment } from "react/jsx-runtime"; +import Document from 'next/document'; +import { Html } from 'next/document'; +import { Head } from 'next/document'; +import { Main } from 'next/document'; +import { NextScript } from 'next/document'; +class MyDocument extends Document { + static async getInitialProps(ctx) { + const initialProps = await Document.getInitialProps(ctx); + return { + ...initialProps, + styles: _jsxs(_Fragment, { + children: [ + initialProps.styles, + _jsx("style", { + dangerouslySetInnerHTML: { + __html: `html { background: hotpink; }` + } + }) + ] + }) + }; + } + render() { + return _jsxs(Html, { + children: [ + _jsx(Head, {}), + _jsxs("body", { + children: [ + _jsx(Main, {}), + _jsx(NextScript, {}) + ] + }) + ] + }); + } +} +const __TURBOPACK__default__export__ = MyDocument; +export { _jsx } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { _jsxs } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { _Fragment } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { Document } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { Html } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { Head } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { Main } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextScript } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { MyDocument } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { __TURBOPACK__default__export__ } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 5 +```js +export { default } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export default" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "default", + ): 1, + Exports: 5, +} +``` + + +# Modules (prod) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { __TURBOPACK__default__export__ } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +export { __TURBOPACK__default__export__ as default }; + +``` +## Part 2 +```js +import "react/jsx-runtime"; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import 'next/document'; + +``` +## Part 4 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { jsx as _jsx } from "react/jsx-runtime"; +import { jsxs as _jsxs } from "react/jsx-runtime"; +import { Fragment as _Fragment } from "react/jsx-runtime"; +import Document from 'next/document'; +import { Html } from 'next/document'; +import { Head } from 'next/document'; +import { Main } from 'next/document'; +import { NextScript } from 'next/document'; +class MyDocument extends Document { + static async getInitialProps(ctx) { + const initialProps = await Document.getInitialProps(ctx); + return { + ...initialProps, + styles: _jsxs(_Fragment, { + children: [ + initialProps.styles, + _jsx("style", { + dangerouslySetInnerHTML: { + __html: `html { background: hotpink; }` + } + }) + ] + }) + }; + } + render() { + return _jsxs(Html, { + children: [ + _jsx(Head, {}), + _jsxs("body", { + children: [ + _jsx(Main, {}), + _jsx(NextScript, {}) + ] + }) + ] + }); + } +} +const __TURBOPACK__default__export__ = MyDocument; +export { _jsx } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { _jsxs } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { _Fragment } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { Document } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { Html } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { Head } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { Main } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextScript } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { MyDocument } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { __TURBOPACK__default__export__ } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 5 +```js +export { default } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export default" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/app-route/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/app-route/output.md index b797be2842de7..b7655b3de241f 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/app-route/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/app-route/output.md @@ -103,7 +103,7 @@ const routeModule = new AppRouteRouteModule({ - Side effects - Declares: `routeModule` - Reads: `AppRouteRouteModule`, `RouteKind`, `userland` -- Write: `routeModule`, `RouteKind` +- Write: `routeModule`, `RouteKind`, `userland` ## Item 10: Stmt 5, `VarDeclarator(0)` @@ -142,6 +142,7 @@ function patchFetch() { - Declares: `patchFetch` - Reads (eventual): `_patchFetch`, `serverHooks`, `staticGenerationAsyncStorage` - Write: `patchFetch` +- Write (eventual): `serverHooks`, `staticGenerationAsyncStorage` # Phase 1 ```mermaid @@ -282,6 +283,8 @@ graph TD Item19 --> Item12; Item12 --> Item7; Item12 --> Item10; + Item12 -.-> Item17; + Item12 -.-> Item16; ``` # Phase 4 ```mermaid @@ -335,6 +338,8 @@ graph TD Item19 --> Item12; Item12 --> Item7; Item12 --> Item10; + Item12 -.-> Item17; + Item12 -.-> Item16; Item13 --> Item1; Item13 --> Item2; Item13 --> Item3; @@ -368,6 +373,8 @@ graph TD N4 --> N12; N6 --> N11; N6 --> N12; + N6 --> N4; + N6 --> N3; N8 --> N7; N9 --> N7; N9 --> N8; @@ -400,6 +407,7 @@ graph TD Export( "routeModule", ): 1, + Exports: 13, Export( "originalPathname", ): 5, @@ -477,6 +485,12 @@ import "__TURBOPACK_PART__" assert { import { staticGenerationAsyncStorage, serverHooks } from "__TURBOPACK_PART__" assert { __turbopack_part__: 12 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; export { patchFetch as patchFetch }; import { patchFetch as _patchFetch } from '../../server/lib/patch-fetch'; function patchFetch() { @@ -594,6 +608,28 @@ export { serverHooks } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 13 +```js +export { routeModule } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export routeModule" +}; +export { requestAsyncStorage } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export requestAsyncStorage" +}; +export { staticGenerationAsyncStorage } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export staticGenerationAsyncStorage" +}; +export { serverHooks } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export serverHooks" +}; +export { originalPathname } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export originalPathname" +}; +export { patchFetch } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export patchFetch" +}; + ``` ## Merged (module eval) ```js @@ -635,6 +671,7 @@ import "__TURBOPACK_PART__" assert { Export( "routeModule", ): 1, + Exports: 13, Export( "originalPathname", ): 5, @@ -822,6 +859,28 @@ export { serverHooks } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 13 +```js +export { routeModule } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export routeModule" +}; +export { requestAsyncStorage } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export requestAsyncStorage" +}; +export { staticGenerationAsyncStorage } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export staticGenerationAsyncStorage" +}; +export { serverHooks } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export serverHooks" +}; +export { originalPathname } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export originalPathname" +}; +export { patchFetch } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export patchFetch" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/combined-export/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/combined-export/output.md index c734bad84fb88..458f65fd2d252 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/combined-export/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/combined-export/output.md @@ -88,6 +88,7 @@ graph TD ``` { ModuleEvaluation: 0, + Exports: 3, Export( "b", ): 2, @@ -121,6 +122,16 @@ export { b } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 3 +```js +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + ``` ## Merged (module eval) ```js @@ -132,6 +143,7 @@ export { b } from "__TURBOPACK_VAR__" assert { ``` { ModuleEvaluation: 0, + Exports: 3, Export( "b", ): 2, @@ -165,6 +177,16 @@ export { b } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 3 +```js +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/complex/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/complex/output.md index 7929d55a7d44b..e21a6ea4860dc 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/complex/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/complex/output.md @@ -281,10 +281,13 @@ graph TD Item17 --> Item12; Item18 --> Item13; Item4 --> Item8; + Item4 --> Item1; Item7 -.-> Item9; Item7 -.-> Item10; + Item7 --> Item1; Item13 --> Item11; Item13 --> Item8; + Item13 --> Item1; ``` # Phase 4 ```mermaid @@ -338,10 +341,13 @@ graph TD Item17 --> Item12; Item18 --> Item13; Item4 --> Item8; + Item4 --> Item1; Item7 -.-> Item9; Item7 -.-> Item10; + Item7 --> Item1; Item13 --> Item11; Item13 --> Item8; + Item13 --> Item1; Item14 --> Item3; Item14 --> Item6; Item14 --> Item9; @@ -371,9 +377,10 @@ graph TD N3 --> N13; N4 --> N13; N4 --> N11; + N4 --> N6; N5 --> N11; - N5 --> N12; N5 --> N6; + N5 --> N12; N7 --> N6; N8 --> N7; N8 --> N6; @@ -402,6 +409,7 @@ graph TD Export( "initialCat", ): 3, + Exports: 14, Export( "cat", ): 2, @@ -463,6 +471,9 @@ import { cat } from "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 11 }; +import { dog } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; export { getChimera }; function getChimera() { return cat + dog; @@ -477,12 +488,12 @@ export { getChimera } from "__TURBOPACK_VAR__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 11 }; -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 12 -}; import { dog } from "__TURBOPACK_PART__" assert { __turbopack_part__: 6 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; function getDog() { return dog; } @@ -598,6 +609,22 @@ export { cat } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 14 +```js +export { dogRef } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export dogRef" +}; +export { cat } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export cat" +}; +export { initialCat } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export initialCat" +}; +export { getChimera } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getChimera" +}; + ``` ## Merged (module eval) ```js @@ -624,6 +651,7 @@ import "__TURBOPACK_PART__" assert { Export( "initialCat", ): 3, + Exports: 10, Export( "cat", ): 2, @@ -714,6 +742,9 @@ import { cat } from "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; +import { dog } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { getChimera }; function getChimera() { return cat + dog; @@ -768,6 +799,22 @@ export { cat } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 10 +```js +export { dogRef } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export dogRef" +}; +export { cat } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export cat" +}; +export { initialCat } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export initialCat" +}; +export { getChimera } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getChimera" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/export-named/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/export-named/output.md index a19d842625785..0fbaba881b5bd 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/export-named/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/export-named/output.md @@ -41,6 +41,7 @@ graph TD Item3["ModuleEvaluation"]; Item4; Item4["export fakeCat"]; + Item4 --> Item2; ``` # Phase 3 ```mermaid @@ -51,6 +52,7 @@ graph TD Item3["ModuleEvaluation"]; Item4; Item4["export fakeCat"]; + Item4 --> Item2; ``` # Phase 4 ```mermaid @@ -61,8 +63,8 @@ graph TD Item3["ModuleEvaluation"]; Item4; Item4["export fakeCat"]; - Item3 --> Item1; Item4 --> Item2; + Item3 --> Item1; ``` # Final ```mermaid @@ -78,6 +80,7 @@ graph TD Export( "fakeCat", ): 1, + Exports: 2, } ``` @@ -97,6 +100,13 @@ export { __TURBOPACK__reexport__cat__ } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 2 +```js +export { fakeCat } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export fakeCat" +}; + ``` ## Merged (module eval) ```js @@ -112,6 +122,7 @@ import "./lib"; Export( "fakeCat", ): 1, + Exports: 2, } ``` @@ -131,6 +142,13 @@ export { __TURBOPACK__reexport__cat__ } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 2 +```js +export { fakeCat } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export fakeCat" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-1/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-1/output.md index 41b9426b0686b..b38d9dbf90f83 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-1/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-1/output.md @@ -239,6 +239,7 @@ graph TD Export( "addMessageListener", ): 1, + Exports: 6, Export( "sendMessage", ): 2, @@ -361,6 +362,19 @@ export { eventCallbacks } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 6 +```js +export { addMessageListener } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export addMessageListener" +}; +export { sendMessage } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export sendMessage" +}; +export { connectHMR } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export connectHMR" +}; + ``` ## Merged (module eval) ```js @@ -378,6 +392,7 @@ export { eventCallbacks } from "__TURBOPACK_VAR__" assert { Export( "addMessageListener", ): 1, + Exports: 6, Export( "sendMessage", ): 2, @@ -500,6 +515,19 @@ export { eventCallbacks } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 6 +```js +export { addMessageListener } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export addMessageListener" +}; +export { sendMessage } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export sendMessage" +}; +export { connectHMR } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export connectHMR" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-2/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-2/output.md index 361e8588ef2c0..dcaa3b1ed962c 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-2/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-2/output.md @@ -465,9 +465,11 @@ graph TD Item14 --> Item15; Item15 --> Item18; Item15 --> Item9; + Item15 --> Item5; Item18 --> Item9; Item19 --> Item18; Item19 --> Item9; + Item19 --> Item5; ``` # Phase 4 ```mermaid @@ -536,9 +538,11 @@ graph TD Item14 --> Item15; Item15 --> Item18; Item15 --> Item9; + Item15 --> Item5; Item18 --> Item9; Item19 --> Item18; Item19 --> Item9; + Item19 --> Item5; Item20 --> Item1; Item20 --> Item2; Item20 --> Item3; @@ -555,9 +559,9 @@ graph TD N5["Items: [ItemId(Export(("trackDynamicFetch", #2), "trackDynamicFetch")), ItemId(9, Normal)]"]; N6["Items: [ItemId(Export(("usedDynamicAPIs", #2), "usedDynamicAPIs")), ItemId(11, Normal)]"]; N7["Items: [ItemId(Export(("formatDynamicAPIAccesses", #2), "formatDynamicAPIAccesses")), ItemId(12, Normal)]"]; - N8["Items: [ItemId(Export(("createPostponedAbortSignal", #2), "createPostponedAbortSignal")), ItemId(14, Normal)]"]; + N8["Items: [ItemId(Export(("createPostponedAbortSignal", #2), "createPostponedAbortSignal")), ItemId(0, ImportBinding(0)), ItemId(14, Normal)]"]; N9["Items: [ItemId(0, ImportBinding(0)), ItemId(4, VarDeclarator(0))]"]; - N10["Items: [ItemId(10, Normal)]"]; + N10["Items: [ItemId(0, ImportBinding(0)), ItemId(10, Normal)]"]; N11["Items: [ItemId(13, Normal)]"]; N2 --> N3; N2 --> N10; @@ -566,6 +570,8 @@ graph TD N5 --> N10; N8 --> N11; N8 --> N9; + N8 --> N10; + N9 --> N10; N10 --> N11; N10 --> N9; N11 --> N9; @@ -599,6 +605,7 @@ graph TD Export( "formatDynamicAPIAccesses", ): 7, + Exports: 12, } ``` @@ -782,10 +789,14 @@ export { formatDynamicAPIAccesses } from "__TURBOPACK_VAR__" assert { import { assertPostpone } from "__TURBOPACK_PART__" assert { __turbopack_part__: 11 }; -import { React } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; export { createPostponedAbortSignal }; +import React from 'react'; function createPostponedAbortSignal(reason) { assertPostpone(); const controller = new AbortController(); @@ -796,6 +807,9 @@ function createPostponedAbortSignal(reason) { } return controller.signal; } +export { React } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; export { createPostponedAbortSignal } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; @@ -803,6 +817,9 @@ export { createPostponedAbortSignal } from "__TURBOPACK_VAR__" assert { ``` ## Part 9 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; import React from 'react'; const hasPostpone = typeof React.unstable_postpone === 'function'; export { React } from "__TURBOPACK_VAR__" assert { @@ -818,9 +835,10 @@ export { hasPostpone } from "__TURBOPACK_VAR__" assert { import { assertPostpone } from "__TURBOPACK_PART__" assert { __turbopack_part__: 11 }; -import { React } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import React from 'react'; function postponeWithTracking(prerenderState, expression, pathname) { assertPostpone(); const reason = `Route ${pathname} needs to bail out of prerendering at this point because it used ${expression}. ` + `React throws this special object to indicate where. It should not be caught by ` + `your own try/catch. Learn more: https://nextjs.org/docs/messages/ppr-caught-error`; @@ -830,6 +848,9 @@ function postponeWithTracking(prerenderState, expression, pathname) { }); React.unstable_postpone(reason); } +export { React } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; export { postponeWithTracking } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; @@ -849,6 +870,34 @@ export { assertPostpone } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 12 +```js +export { createPrerenderState } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export createPrerenderState" +}; +export { markCurrentScopeAsDynamic } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export markCurrentScopeAsDynamic" +}; +export { trackDynamicDataAccessed } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export trackDynamicDataAccessed" +}; +export { Postpone } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export Postpone" +}; +export { trackDynamicFetch } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export trackDynamicFetch" +}; +export { usedDynamicAPIs } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export usedDynamicAPIs" +}; +export { formatDynamicAPIAccesses } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export formatDynamicAPIAccesses" +}; +export { createPostponedAbortSignal } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export createPostponedAbortSignal" +}; + ``` ## Merged (module eval) ```js @@ -888,6 +937,7 @@ import '../../lib/url'; Export( "formatDynamicAPIAccesses", ): 7, + Exports: 12, } ``` @@ -1071,10 +1121,14 @@ export { formatDynamicAPIAccesses } from "__TURBOPACK_VAR__" assert { import { assertPostpone } from "__TURBOPACK_PART__" assert { __turbopack_part__: 11 }; -import { React } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; export { createPostponedAbortSignal }; +import React from 'react'; function createPostponedAbortSignal(reason) { assertPostpone(); const controller = new AbortController(); @@ -1085,6 +1139,9 @@ function createPostponedAbortSignal(reason) { } return controller.signal; } +export { React } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; export { createPostponedAbortSignal } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; @@ -1092,6 +1149,9 @@ export { createPostponedAbortSignal } from "__TURBOPACK_VAR__" assert { ``` ## Part 9 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; import React from 'react'; const hasPostpone = typeof React.unstable_postpone === 'function'; export { React } from "__TURBOPACK_VAR__" assert { @@ -1107,9 +1167,10 @@ export { hasPostpone } from "__TURBOPACK_VAR__" assert { import { assertPostpone } from "__TURBOPACK_PART__" assert { __turbopack_part__: 11 }; -import { React } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import React from 'react'; function postponeWithTracking(prerenderState, expression, pathname) { assertPostpone(); const reason = `Route ${pathname} needs to bail out of prerendering at this point because it used ${expression}. ` + `React throws this special object to indicate where. It should not be caught by ` + `your own try/catch. Learn more: https://nextjs.org/docs/messages/ppr-caught-error`; @@ -1119,6 +1180,9 @@ function postponeWithTracking(prerenderState, expression, pathname) { }); React.unstable_postpone(reason); } +export { React } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; export { postponeWithTracking } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; @@ -1138,6 +1202,34 @@ export { assertPostpone } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 12 +```js +export { createPrerenderState } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export createPrerenderState" +}; +export { markCurrentScopeAsDynamic } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export markCurrentScopeAsDynamic" +}; +export { trackDynamicDataAccessed } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export trackDynamicDataAccessed" +}; +export { Postpone } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export Postpone" +}; +export { trackDynamicFetch } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export trackDynamicFetch" +}; +export { usedDynamicAPIs } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export usedDynamicAPIs" +}; +export { formatDynamicAPIAccesses } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export formatDynamicAPIAccesses" +}; +export { createPostponedAbortSignal } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export createPostponedAbortSignal" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-3/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-3/output.md index 5220a3ffac6a3..d903445cdfb18 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-3/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/failed-3/output.md @@ -251,8 +251,8 @@ process.on("uncaughtException", (err)=>{ ``` - Side effects -- Reads (eventual): `IPC` -- Write (eventual): `IPC` +- Reads: `IPC` +- Write: `IPC` ## Item 12: Stmt 8, `VarDeclarator(0)` @@ -274,6 +274,7 @@ const improveConsole = (name, stream, addStack)=>{ ``` +- Side effects - Declares: `improveConsole` - Write: `improveConsole` @@ -534,15 +535,25 @@ graph TD Item10 -.-> Item5; Item10 -.-> Item4; Item10 -.-> Item7; + Item11 --> Item10; Item11 --> Item1; Item11 --> Item2; Item11 --> Item3; Item11 --> Item9; - Item11 --> Item10; Item11 -.-> Item6; Item11 -.-> Item5; Item11 -.-> Item4; Item11 -.-> Item7; + Item12 --> Item1; + Item12 --> Item2; + Item12 --> Item3; + Item12 --> Item9; + Item12 --> Item10; + Item12 --> Item11; + Item12 -.-> Item6; + Item12 -.-> Item5; + Item12 -.-> Item4; + Item12 -.-> Item7; Item13 --> Item12; Item13 --> Item1; Item13 --> Item2; @@ -840,6 +851,7 @@ graph TD Item28 -.-> Item4; Item28 -.-> Item7; Item30 --> Item7; + Item31 --> Item11; Item31 --> Item10; ``` # Phase 3 @@ -898,15 +910,25 @@ graph TD Item10 -.-> Item5; Item10 -.-> Item4; Item10 -.-> Item7; + Item11 --> Item10; Item11 --> Item1; Item11 --> Item2; Item11 --> Item3; Item11 --> Item9; - Item11 --> Item10; Item11 -.-> Item6; Item11 -.-> Item5; Item11 -.-> Item4; Item11 -.-> Item7; + Item12 --> Item1; + Item12 --> Item2; + Item12 --> Item3; + Item12 --> Item9; + Item12 --> Item10; + Item12 --> Item11; + Item12 -.-> Item6; + Item12 -.-> Item5; + Item12 -.-> Item4; + Item12 -.-> Item7; Item13 --> Item12; Item13 --> Item1; Item13 --> Item2; @@ -1204,12 +1226,12 @@ graph TD Item28 -.-> Item4; Item28 -.-> Item7; Item30 --> Item7; + Item31 --> Item11; Item31 --> Item10; Item7 --> Item6; Item7 --> Item5; Item8 --> Item4; Item8 --> Item7; - Item11 -.-> Item31; ``` # Phase 4 ```mermaid @@ -1267,15 +1289,25 @@ graph TD Item10 -.-> Item5; Item10 -.-> Item4; Item10 -.-> Item7; + Item11 --> Item10; Item11 --> Item1; Item11 --> Item2; Item11 --> Item3; Item11 --> Item9; - Item11 --> Item10; Item11 -.-> Item6; Item11 -.-> Item5; Item11 -.-> Item4; Item11 -.-> Item7; + Item12 --> Item1; + Item12 --> Item2; + Item12 --> Item3; + Item12 --> Item9; + Item12 --> Item10; + Item12 --> Item11; + Item12 -.-> Item6; + Item12 -.-> Item5; + Item12 -.-> Item4; + Item12 -.-> Item7; Item13 --> Item12; Item13 --> Item1; Item13 --> Item2; @@ -1573,18 +1605,19 @@ graph TD Item28 -.-> Item4; Item28 -.-> Item7; Item30 --> Item7; + Item31 --> Item11; Item31 --> Item10; Item7 --> Item6; Item7 --> Item5; Item8 --> Item4; Item8 --> Item7; - Item11 -.-> Item31; Item29 --> Item1; Item29 --> Item2; Item29 --> Item3; Item29 --> Item9; Item29 --> Item10; Item29 --> Item11; + Item29 --> Item12; Item29 --> Item13; Item29 --> Item14; Item29 --> Item15; @@ -1605,7 +1638,7 @@ graph TD # Final ```mermaid graph TD - N0["Items: [ItemId(ModuleEvaluation), ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(7, Normal), ItemId(8, VarDeclarator(0)), ItemId(9, Normal), ItemId(10, Normal), ItemId(11, Normal), ItemId(12, Normal), ItemId(13, Normal), ItemId(14, Normal), ItemId(15, Normal), ItemId(16, Normal), ItemId(17, Normal), ItemId(18, Normal), ItemId(19, Normal), ItemId(20, Normal), ItemId(21, Normal), ItemId(22, Normal), ItemId(23, Normal), ItemId(24, Normal)]"]; + N0["Items: [ItemId(ModuleEvaluation), ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(8, VarDeclarator(0)), ItemId(9, Normal), ItemId(10, Normal), ItemId(11, Normal), ItemId(12, Normal), ItemId(13, Normal), ItemId(14, Normal), ItemId(15, Normal), ItemId(16, Normal), ItemId(17, Normal), ItemId(18, Normal), ItemId(19, Normal), ItemId(20, Normal), ItemId(21, Normal), ItemId(22, Normal), ItemId(23, Normal), ItemId(24, Normal)]"]; N1["Items: [ItemId(Export(("structuredError", #2), "structuredError"))]"]; N2["Items: [ItemId(Export(("IPC", #2), "IPC"))]"]; N3["Items: [ItemId(0, ImportOfModule)]"]; @@ -1614,41 +1647,51 @@ graph TD N6["Items: [ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(3, Normal)]"]; N7["Items: [ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(5, VarDeclarator(0))]"]; N8["Items: [ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(4, Normal), ItemId(6, VarDeclarator(0))]"]; + N9["Items: [ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(7, Normal)]"]; N0 --> N3; N0 --> N4; N0 --> N5; N0 --> N7; N0 --> N8; + N0 --> N9; N0 --> N6; - N0 --> N2; N1 --> N6; + N2 --> N9; N2 --> N8; N4 --> N3; N5 --> N3; N5 --> N4; - N6 --> N8; + N6 --> N9; N7 --> N3; N7 --> N4; N7 --> N5; - N7 --> N8; + N7 --> N9; N7 --> N6; + N8 --> N9; N8 --> N6; N8 --> N7; N8 --> N3; N8 --> N4; N8 --> N5; + N9 --> N8; + N9 --> N3; + N9 --> N4; + N9 --> N5; + N9 --> N7; + N9 --> N6; ``` # Entrypoints ``` { ModuleEvaluation: 0, - Export( - "structuredError", - ): 1, Export( "IPC", ): 2, + Exports: 10, + Export( + "structuredError", + ): 1, } ``` @@ -1668,22 +1711,19 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; -import { IPC } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 6 + __turbopack_part__: 9 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 2 + __turbopack_part__: 6 }; "module evaluation"; import { createConnection } from "node:net"; import { parse as parseStackTrace } from "../compiled/stacktrace-parser"; import { getProperError } from "./error"; -process.on("uncaughtException", (err)=>{ - IPC.sendError(err); -}); const improveConsole = (name, stream, addStack)=>{ const original = console[name]; const stdio = process[stream]; @@ -1738,6 +1778,9 @@ export { structuredError }; ``` ## Part 2 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { IPC } from "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; @@ -1771,7 +1814,7 @@ import "./error"; ## Part 6 ```js import "__TURBOPACK_PART__" assert { - __turbopack_part__: 8 + __turbopack_part__: 9 }; import { parse as parseStackTrace } from "../compiled/stacktrace-parser"; import { getProperError } from "./error"; @@ -1806,7 +1849,7 @@ import "__TURBOPACK_PART__" assert { __turbopack_part__: 5 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 8 + __turbopack_part__: 9 }; import "__TURBOPACK_PART__" assert { __turbopack_part__: 6 @@ -1831,6 +1874,9 @@ export { PORT } from "__TURBOPACK_VAR__" assert { ``` ## Part 8 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { structuredError } from "__TURBOPACK_PART__" assert { __turbopack_part__: 6 }; @@ -1992,6 +2038,53 @@ export { IPC } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 9 +```js +import { IPC } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import { createConnection } from "node:net"; +import { parse as parseStackTrace } from "../compiled/stacktrace-parser"; +import { getProperError } from "./error"; +process.on("uncaughtException", (err)=>{ + IPC.sendError(err); +}); +export { createConnection } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { parseStackTrace } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getProperError } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +export { structuredError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export structuredError" +}; +export { IPC } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export IPC" +}; + ``` ## Merged (module eval) ```js @@ -2007,22 +2100,19 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; -import { IPC } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 6 + __turbopack_part__: 9 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 2 + __turbopack_part__: 6 }; import { createConnection } from "node:net"; import { parse as parseStackTrace } from "../compiled/stacktrace-parser"; import { getProperError } from "./error"; "module evaluation"; -process.on("uncaughtException", (err)=>{ - IPC.sendError(err); -}); const improveConsole = (name, stream, addStack)=>{ const original = console[name]; const stdio = process[stream]; @@ -2072,12 +2162,13 @@ export { improveConsole } from "__TURBOPACK_VAR__" assert { ``` { ModuleEvaluation: 0, - Export( - "structuredError", - ): 1, Export( "IPC", ): 2, + Exports: 10, + Export( + "structuredError", + ): 1, } ``` @@ -2097,13 +2188,13 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; -import { IPC } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; "module evaluation"; -process.on("uncaughtException", (err)=>{ - IPC.sendError(err); -}); const improveConsole = (name, stream, addStack)=>{ const original = console[name]; const stdio = process[stream]; @@ -2149,6 +2240,9 @@ export { structuredError }; ``` ## Part 2 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { IPC } from "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; @@ -2374,6 +2468,38 @@ export { IPC } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 9 +```js +import { IPC } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +process.on("uncaughtException", (err)=>{ + IPC.sendError(err); +}); + +``` +## Part 10 +```js +export { structuredError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export structuredError" +}; +export { IPC } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export IPC" +}; + ``` ## Merged (module eval) ```js @@ -2389,13 +2515,13 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; -import { IPC } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; "module evaluation"; -process.on("uncaughtException", (err)=>{ - IPC.sendError(err); -}); const improveConsole = (name, stream, addStack)=>{ const original = console[name]; const stdio = process[stream]; diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/grouping/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/grouping/output.md index 6426c3f4376c8..32edc7597008f 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/grouping/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/grouping/output.md @@ -299,6 +299,7 @@ graph TD ``` { ModuleEvaluation: 0, + Exports: 7, Export( "y", ): 2, @@ -388,6 +389,16 @@ x += 7; x += 8; x += 9; +``` +## Part 7 +```js +export { x } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export x" +}; +export { y } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export y" +}; + ``` ## Merged (module eval) ```js @@ -402,6 +413,7 @@ import "__TURBOPACK_PART__" assert { ``` { ModuleEvaluation: 0, + Exports: 6, Export( "y", ): 2, @@ -478,6 +490,16 @@ x += 7; x += 8; x += 9; +``` +## Part 6 +```js +export { x } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export x" +}; +export { y } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export y" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/ipc-evaluate/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/ipc-evaluate/output.md index adbe2ec572da3..c00cbd3e502da 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/ipc-evaluate/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/ipc-evaluate/output.md @@ -140,10 +140,10 @@ export const run = async (moduleFactory)=>{ ``` +- Side effects - Declares: `run` -- Reads (eventual): `ipc`, `queue` -- Write: `run` -- Write (eventual): `ipc`, `queue` +- Reads: `ipc`, `queue` +- Write: `run`, `ipc`, `queue` # Phase 1 ```mermaid @@ -171,6 +171,9 @@ graph TD Item7; Item7["export run"]; Item3 --> Item2; + Item5 --> Item3; + Item5 --> Item4; + Item5 --> Item1; Item7 --> Item5; ``` # Phase 3 @@ -186,9 +189,10 @@ graph TD Item7; Item7["export run"]; Item3 --> Item2; - Item7 --> Item5; Item5 --> Item3; Item5 --> Item4; + Item5 --> Item1; + Item7 --> Item5; ``` # Phase 4 ```mermaid @@ -203,22 +207,31 @@ graph TD Item7; Item7["export run"]; Item3 --> Item2; - Item7 --> Item5; Item5 --> Item3; Item5 --> Item4; + Item5 --> Item1; + Item7 --> Item5; Item6 --> Item1; + Item6 --> Item5; ``` # Final ```mermaid graph TD - N0["Items: [ItemId(ModuleEvaluation), ItemId(0, ImportOfModule)]"]; - N1["Items: [ItemId(Export(("run", #2), "run")), ItemId(0, ImportBinding(0)), ItemId(1, VarDeclarator(0)), ItemId(2, VarDeclarator(0)), ItemId(3, VarDeclarator(0))]"]; + N0["Items: [ItemId(ModuleEvaluation)]"]; + N1["Items: [ItemId(Export(("run", #2), "run"))]"]; + N2["Items: [ItemId(0, ImportOfModule)]"]; + N3["Items: [ItemId(0, ImportBinding(0)), ItemId(1, VarDeclarator(0)), ItemId(2, VarDeclarator(0)), ItemId(3, VarDeclarator(0))]"]; + N0 --> N2; + N0 --> N3; + N1 --> N3; + N3 --> N2; ``` # Entrypoints ``` { ModuleEvaluation: 0, + Exports: 4, Export( "run", ): 1, @@ -229,13 +242,33 @@ graph TD # Modules (dev) ## Part 0 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; "module evaluation"; -import "./index"; ``` ## Part 1 ```js +import { run } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; export { run }; + +``` +## Part 2 +```js +import "./index"; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; import { IPC } from "./index"; const ipc = IPC; const queue = []; @@ -343,10 +376,22 @@ export { run } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 4 +```js +export { run } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export run" +}; + ``` ## Merged (module eval) ```js -import "./index"; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; "module evaluation"; ``` @@ -355,6 +400,7 @@ import "./index"; ``` { ModuleEvaluation: 0, + Exports: 4, Export( "run", ): 1, @@ -365,13 +411,33 @@ import "./index"; # Modules (prod) ## Part 0 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; "module evaluation"; -import "./index"; ``` ## Part 1 ```js +import { run } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; export { run }; + +``` +## Part 2 +```js +import "./index"; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; import { IPC } from "./index"; const ipc = IPC; const queue = []; @@ -479,10 +545,22 @@ export { run } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 4 +```js +export { run } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export run" +}; + ``` ## Merged (module eval) ```js -import "./index"; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; "module evaluation"; ``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/ipc-index/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/ipc-index/output.md index 5220a3ffac6a3..d903445cdfb18 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/ipc-index/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/ipc-index/output.md @@ -251,8 +251,8 @@ process.on("uncaughtException", (err)=>{ ``` - Side effects -- Reads (eventual): `IPC` -- Write (eventual): `IPC` +- Reads: `IPC` +- Write: `IPC` ## Item 12: Stmt 8, `VarDeclarator(0)` @@ -274,6 +274,7 @@ const improveConsole = (name, stream, addStack)=>{ ``` +- Side effects - Declares: `improveConsole` - Write: `improveConsole` @@ -534,15 +535,25 @@ graph TD Item10 -.-> Item5; Item10 -.-> Item4; Item10 -.-> Item7; + Item11 --> Item10; Item11 --> Item1; Item11 --> Item2; Item11 --> Item3; Item11 --> Item9; - Item11 --> Item10; Item11 -.-> Item6; Item11 -.-> Item5; Item11 -.-> Item4; Item11 -.-> Item7; + Item12 --> Item1; + Item12 --> Item2; + Item12 --> Item3; + Item12 --> Item9; + Item12 --> Item10; + Item12 --> Item11; + Item12 -.-> Item6; + Item12 -.-> Item5; + Item12 -.-> Item4; + Item12 -.-> Item7; Item13 --> Item12; Item13 --> Item1; Item13 --> Item2; @@ -840,6 +851,7 @@ graph TD Item28 -.-> Item4; Item28 -.-> Item7; Item30 --> Item7; + Item31 --> Item11; Item31 --> Item10; ``` # Phase 3 @@ -898,15 +910,25 @@ graph TD Item10 -.-> Item5; Item10 -.-> Item4; Item10 -.-> Item7; + Item11 --> Item10; Item11 --> Item1; Item11 --> Item2; Item11 --> Item3; Item11 --> Item9; - Item11 --> Item10; Item11 -.-> Item6; Item11 -.-> Item5; Item11 -.-> Item4; Item11 -.-> Item7; + Item12 --> Item1; + Item12 --> Item2; + Item12 --> Item3; + Item12 --> Item9; + Item12 --> Item10; + Item12 --> Item11; + Item12 -.-> Item6; + Item12 -.-> Item5; + Item12 -.-> Item4; + Item12 -.-> Item7; Item13 --> Item12; Item13 --> Item1; Item13 --> Item2; @@ -1204,12 +1226,12 @@ graph TD Item28 -.-> Item4; Item28 -.-> Item7; Item30 --> Item7; + Item31 --> Item11; Item31 --> Item10; Item7 --> Item6; Item7 --> Item5; Item8 --> Item4; Item8 --> Item7; - Item11 -.-> Item31; ``` # Phase 4 ```mermaid @@ -1267,15 +1289,25 @@ graph TD Item10 -.-> Item5; Item10 -.-> Item4; Item10 -.-> Item7; + Item11 --> Item10; Item11 --> Item1; Item11 --> Item2; Item11 --> Item3; Item11 --> Item9; - Item11 --> Item10; Item11 -.-> Item6; Item11 -.-> Item5; Item11 -.-> Item4; Item11 -.-> Item7; + Item12 --> Item1; + Item12 --> Item2; + Item12 --> Item3; + Item12 --> Item9; + Item12 --> Item10; + Item12 --> Item11; + Item12 -.-> Item6; + Item12 -.-> Item5; + Item12 -.-> Item4; + Item12 -.-> Item7; Item13 --> Item12; Item13 --> Item1; Item13 --> Item2; @@ -1573,18 +1605,19 @@ graph TD Item28 -.-> Item4; Item28 -.-> Item7; Item30 --> Item7; + Item31 --> Item11; Item31 --> Item10; Item7 --> Item6; Item7 --> Item5; Item8 --> Item4; Item8 --> Item7; - Item11 -.-> Item31; Item29 --> Item1; Item29 --> Item2; Item29 --> Item3; Item29 --> Item9; Item29 --> Item10; Item29 --> Item11; + Item29 --> Item12; Item29 --> Item13; Item29 --> Item14; Item29 --> Item15; @@ -1605,7 +1638,7 @@ graph TD # Final ```mermaid graph TD - N0["Items: [ItemId(ModuleEvaluation), ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(7, Normal), ItemId(8, VarDeclarator(0)), ItemId(9, Normal), ItemId(10, Normal), ItemId(11, Normal), ItemId(12, Normal), ItemId(13, Normal), ItemId(14, Normal), ItemId(15, Normal), ItemId(16, Normal), ItemId(17, Normal), ItemId(18, Normal), ItemId(19, Normal), ItemId(20, Normal), ItemId(21, Normal), ItemId(22, Normal), ItemId(23, Normal), ItemId(24, Normal)]"]; + N0["Items: [ItemId(ModuleEvaluation), ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(8, VarDeclarator(0)), ItemId(9, Normal), ItemId(10, Normal), ItemId(11, Normal), ItemId(12, Normal), ItemId(13, Normal), ItemId(14, Normal), ItemId(15, Normal), ItemId(16, Normal), ItemId(17, Normal), ItemId(18, Normal), ItemId(19, Normal), ItemId(20, Normal), ItemId(21, Normal), ItemId(22, Normal), ItemId(23, Normal), ItemId(24, Normal)]"]; N1["Items: [ItemId(Export(("structuredError", #2), "structuredError"))]"]; N2["Items: [ItemId(Export(("IPC", #2), "IPC"))]"]; N3["Items: [ItemId(0, ImportOfModule)]"]; @@ -1614,41 +1647,51 @@ graph TD N6["Items: [ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(3, Normal)]"]; N7["Items: [ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(5, VarDeclarator(0))]"]; N8["Items: [ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(4, Normal), ItemId(6, VarDeclarator(0))]"]; + N9["Items: [ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(7, Normal)]"]; N0 --> N3; N0 --> N4; N0 --> N5; N0 --> N7; N0 --> N8; + N0 --> N9; N0 --> N6; - N0 --> N2; N1 --> N6; + N2 --> N9; N2 --> N8; N4 --> N3; N5 --> N3; N5 --> N4; - N6 --> N8; + N6 --> N9; N7 --> N3; N7 --> N4; N7 --> N5; - N7 --> N8; + N7 --> N9; N7 --> N6; + N8 --> N9; N8 --> N6; N8 --> N7; N8 --> N3; N8 --> N4; N8 --> N5; + N9 --> N8; + N9 --> N3; + N9 --> N4; + N9 --> N5; + N9 --> N7; + N9 --> N6; ``` # Entrypoints ``` { ModuleEvaluation: 0, - Export( - "structuredError", - ): 1, Export( "IPC", ): 2, + Exports: 10, + Export( + "structuredError", + ): 1, } ``` @@ -1668,22 +1711,19 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; -import { IPC } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 6 + __turbopack_part__: 9 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 2 + __turbopack_part__: 6 }; "module evaluation"; import { createConnection } from "node:net"; import { parse as parseStackTrace } from "../compiled/stacktrace-parser"; import { getProperError } from "./error"; -process.on("uncaughtException", (err)=>{ - IPC.sendError(err); -}); const improveConsole = (name, stream, addStack)=>{ const original = console[name]; const stdio = process[stream]; @@ -1738,6 +1778,9 @@ export { structuredError }; ``` ## Part 2 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { IPC } from "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; @@ -1771,7 +1814,7 @@ import "./error"; ## Part 6 ```js import "__TURBOPACK_PART__" assert { - __turbopack_part__: 8 + __turbopack_part__: 9 }; import { parse as parseStackTrace } from "../compiled/stacktrace-parser"; import { getProperError } from "./error"; @@ -1806,7 +1849,7 @@ import "__TURBOPACK_PART__" assert { __turbopack_part__: 5 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 8 + __turbopack_part__: 9 }; import "__TURBOPACK_PART__" assert { __turbopack_part__: 6 @@ -1831,6 +1874,9 @@ export { PORT } from "__TURBOPACK_VAR__" assert { ``` ## Part 8 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { structuredError } from "__TURBOPACK_PART__" assert { __turbopack_part__: 6 }; @@ -1992,6 +2038,53 @@ export { IPC } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 9 +```js +import { IPC } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import { createConnection } from "node:net"; +import { parse as parseStackTrace } from "../compiled/stacktrace-parser"; +import { getProperError } from "./error"; +process.on("uncaughtException", (err)=>{ + IPC.sendError(err); +}); +export { createConnection } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { parseStackTrace } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getProperError } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +export { structuredError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export structuredError" +}; +export { IPC } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export IPC" +}; + ``` ## Merged (module eval) ```js @@ -2007,22 +2100,19 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; -import { IPC } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 6 + __turbopack_part__: 9 }; import "__TURBOPACK_PART__" assert { - __turbopack_part__: 2 + __turbopack_part__: 6 }; import { createConnection } from "node:net"; import { parse as parseStackTrace } from "../compiled/stacktrace-parser"; import { getProperError } from "./error"; "module evaluation"; -process.on("uncaughtException", (err)=>{ - IPC.sendError(err); -}); const improveConsole = (name, stream, addStack)=>{ const original = console[name]; const stdio = process[stream]; @@ -2072,12 +2162,13 @@ export { improveConsole } from "__TURBOPACK_VAR__" assert { ``` { ModuleEvaluation: 0, - Export( - "structuredError", - ): 1, Export( "IPC", ): 2, + Exports: 10, + Export( + "structuredError", + ): 1, } ``` @@ -2097,13 +2188,13 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; -import { IPC } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; "module evaluation"; -process.on("uncaughtException", (err)=>{ - IPC.sendError(err); -}); const improveConsole = (name, stream, addStack)=>{ const original = console[name]; const stdio = process[stream]; @@ -2149,6 +2240,9 @@ export { structuredError }; ``` ## Part 2 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { IPC } from "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; @@ -2374,6 +2468,38 @@ export { IPC } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 9 +```js +import { IPC } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +process.on("uncaughtException", (err)=>{ + IPC.sendError(err); +}); + +``` +## Part 10 +```js +export { structuredError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export structuredError" +}; +export { IPC } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export IPC" +}; + ``` ## Merged (module eval) ```js @@ -2389,13 +2515,13 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; -import { IPC } from "__TURBOPACK_PART__" assert { +import "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; "module evaluation"; -process.on("uncaughtException", (err)=>{ - IPC.sendError(err); -}); const improveConsole = (name, stream, addStack)=>{ const original = console[name]; const stdio = process[stream]; diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/let-bug-1/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/let-bug-1/input.js new file mode 100644 index 0000000000000..2f8e8f3d19740 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/let-bug-1/input.js @@ -0,0 +1,44 @@ +let clientComponentLoadStart = 0; +let clientComponentLoadTimes = 0; +let clientComponentLoadCount = 0; +export function wrapClientComponentLoader(ComponentMod) { + if (!('performance' in globalThis)) { + return ComponentMod.__next_app__; + } + return { + require: (...args)=>{ + const startTime = performance.now(); + if (clientComponentLoadStart === 0) { + clientComponentLoadStart = startTime; + } + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.require(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + }, + loadChunk: (...args)=>{ + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.loadChunk(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + } + }; +} +export function getClientComponentLoaderMetrics(options = {}) { + const metrics = clientComponentLoadStart === 0 ? undefined : { + clientComponentLoadStart, + clientComponentLoadTimes, + clientComponentLoadCount + }; + if (options.reset) { + clientComponentLoadStart = 0; + clientComponentLoadTimes = 0; + clientComponentLoadCount = 0; + } + return metrics; +} diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/let-bug-1/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/let-bug-1/output.md new file mode 100644 index 0000000000000..331a120f8ad52 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/let-bug-1/output.md @@ -0,0 +1,465 @@ +# Items + +Count: 8 + +## Item 1: Stmt 0, `VarDeclarator(0)` + +```js +let clientComponentLoadStart = 0; + +``` + +- Declares: `clientComponentLoadStart` +- Write: `clientComponentLoadStart` + +## Item 2: Stmt 1, `VarDeclarator(0)` + +```js +let clientComponentLoadTimes = 0; + +``` + +- Declares: `clientComponentLoadTimes` +- Write: `clientComponentLoadTimes` + +## Item 3: Stmt 2, `VarDeclarator(0)` + +```js +let clientComponentLoadCount = 0; + +``` + +- Declares: `clientComponentLoadCount` +- Write: `clientComponentLoadCount` + +## Item 4: Stmt 3, `Normal` + +```js +export function wrapClientComponentLoader(ComponentMod) { + if (!('performance' in globalThis)) { + return ComponentMod.__next_app__; + } + return { + require: (...args)=>{ + const startTime = performance.now(); + if (clientComponentLoadStart === 0) { + clientComponentLoadStart = startTime; + } + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.require(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + }, + loadChunk: (...args)=>{ + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.loadChunk(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + } + }; +} + +``` + +- Hoisted +- Declares: `wrapClientComponentLoader` +- Reads (eventual): `clientComponentLoadStart` +- Write: `wrapClientComponentLoader` +- Write (eventual): `clientComponentLoadStart`, `clientComponentLoadCount`, `clientComponentLoadTimes` + +## Item 5: Stmt 4, `Normal` + +```js +export function getClientComponentLoaderMetrics(options = {}) { + const metrics = clientComponentLoadStart === 0 ? undefined : { + clientComponentLoadStart, + clientComponentLoadTimes, + clientComponentLoadCount + }; + if (options.reset) { + clientComponentLoadStart = 0; + clientComponentLoadTimes = 0; + clientComponentLoadCount = 0; + } + return metrics; +} + +``` + +- Hoisted +- Declares: `getClientComponentLoaderMetrics` +- Reads (eventual): `clientComponentLoadStart`, `clientComponentLoadTimes`, `clientComponentLoadCount` +- Write: `getClientComponentLoaderMetrics` +- Write (eventual): `clientComponentLoadStart`, `clientComponentLoadTimes`, `clientComponentLoadCount` + +# Phase 1 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export wrapClientComponentLoader"]; + Item8; + Item8["export getClientComponentLoaderMetrics"]; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export wrapClientComponentLoader"]; + Item8; + Item8["export getClientComponentLoaderMetrics"]; + Item7 --> Item4; + Item8 --> Item5; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export wrapClientComponentLoader"]; + Item8; + Item8["export getClientComponentLoaderMetrics"]; + Item7 --> Item4; + Item8 --> Item5; + Item4 --> Item1; + Item4 --> Item3; + Item4 --> Item2; + Item5 --> Item1; + Item5 --> Item2; + Item5 --> Item3; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export wrapClientComponentLoader"]; + Item8; + Item8["export getClientComponentLoaderMetrics"]; + Item7 --> Item4; + Item8 --> Item5; + Item4 --> Item1; + Item4 --> Item3; + Item4 --> Item2; + Item5 --> Item1; + Item5 --> Item2; + Item5 --> Item3; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation)]"]; + N1["Items: [ItemId(Export(("wrapClientComponentLoader", #2), "wrapClientComponentLoader")), ItemId(3, Normal)]"]; + N2["Items: [ItemId(Export(("getClientComponentLoaderMetrics", #2), "getClientComponentLoaderMetrics")), ItemId(4, Normal)]"]; + N3["Items: [ItemId(0, VarDeclarator(0))]"]; + N4["Items: [ItemId(1, VarDeclarator(0))]"]; + N5["Items: [ItemId(2, VarDeclarator(0))]"]; + N1 --> N3; + N1 --> N5; + N1 --> N4; + N2 --> N3; + N2 --> N4; + N2 --> N5; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "getClientComponentLoaderMetrics", + ): 2, + Exports: 6, + Export( + "wrapClientComponentLoader", + ): 1, +} +``` + + +# Modules (dev) +## Part 0 +```js +"module evaluation"; + +``` +## Part 1 +```js +import { clientComponentLoadStart } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { clientComponentLoadCount } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import { clientComponentLoadTimes } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +export { wrapClientComponentLoader }; +function wrapClientComponentLoader(ComponentMod) { + if (!('performance' in globalThis)) { + return ComponentMod.__next_app__; + } + return { + require: (...args)=>{ + const startTime = performance.now(); + if (clientComponentLoadStart === 0) { + clientComponentLoadStart = startTime; + } + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.require(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + }, + loadChunk: (...args)=>{ + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.loadChunk(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + } + }; +} +export { wrapClientComponentLoader } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +import { clientComponentLoadStart } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { clientComponentLoadTimes } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import { clientComponentLoadCount } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +export { getClientComponentLoaderMetrics }; +function getClientComponentLoaderMetrics(options = {}) { + const metrics = clientComponentLoadStart === 0 ? undefined : { + clientComponentLoadStart, + clientComponentLoadTimes, + clientComponentLoadCount + }; + if (options.reset) { + clientComponentLoadStart = 0; + clientComponentLoadTimes = 0; + clientComponentLoadCount = 0; + } + return metrics; +} +export { getClientComponentLoaderMetrics } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +let clientComponentLoadStart = 0; +export { clientComponentLoadStart } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 4 +```js +let clientComponentLoadTimes = 0; +export { clientComponentLoadTimes } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 5 +```js +let clientComponentLoadCount = 0; +export { clientComponentLoadCount } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 6 +```js +export { wrapClientComponentLoader } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export wrapClientComponentLoader" +}; +export { getClientComponentLoaderMetrics } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getClientComponentLoaderMetrics" +}; + +``` +## Merged (module eval) +```js +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "getClientComponentLoaderMetrics", + ): 2, + Exports: 6, + Export( + "wrapClientComponentLoader", + ): 1, +} +``` + + +# Modules (prod) +## Part 0 +```js +"module evaluation"; + +``` +## Part 1 +```js +import { clientComponentLoadStart } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { clientComponentLoadCount } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import { clientComponentLoadTimes } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +export { wrapClientComponentLoader }; +function wrapClientComponentLoader(ComponentMod) { + if (!('performance' in globalThis)) { + return ComponentMod.__next_app__; + } + return { + require: (...args)=>{ + const startTime = performance.now(); + if (clientComponentLoadStart === 0) { + clientComponentLoadStart = startTime; + } + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.require(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + }, + loadChunk: (...args)=>{ + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.loadChunk(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + } + }; +} +export { wrapClientComponentLoader } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +import { clientComponentLoadStart } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { clientComponentLoadTimes } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import { clientComponentLoadCount } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +export { getClientComponentLoaderMetrics }; +function getClientComponentLoaderMetrics(options = {}) { + const metrics = clientComponentLoadStart === 0 ? undefined : { + clientComponentLoadStart, + clientComponentLoadTimes, + clientComponentLoadCount + }; + if (options.reset) { + clientComponentLoadStart = 0; + clientComponentLoadTimes = 0; + clientComponentLoadCount = 0; + } + return metrics; +} +export { getClientComponentLoaderMetrics } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +let clientComponentLoadStart = 0; +export { clientComponentLoadStart } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 4 +```js +let clientComponentLoadTimes = 0; +export { clientComponentLoadTimes } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 5 +```js +let clientComponentLoadCount = 0; +export { clientComponentLoadCount } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 6 +```js +export { wrapClientComponentLoader } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export wrapClientComponentLoader" +}; +export { getClientComponentLoaderMetrics } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getClientComponentLoaderMetrics" +}; + +``` +## Merged (module eval) +```js +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/logger/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/logger/input.js new file mode 100644 index 0000000000000..831bb0474831b --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/logger/input.js @@ -0,0 +1,44 @@ +let clientComponentLoadStart = 0; +let clientComponentLoadTimes = 0; +let clientComponentLoadCount = 0; +export function wrapClientComponentLoader(ComponentMod) { + if (!('performance' in globalThis)) { + return ComponentMod.__next_app__; + } + return { + require: (...args)=>{ + if (clientComponentLoadStart === 0) { + clientComponentLoadStart = performance.now(); + } + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.require(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + }, + loadChunk: (...args)=>{ + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.loadChunk(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + } + }; +} +export function getClientComponentLoaderMetrics(options = {}) { + const metrics = clientComponentLoadStart === 0 ? undefined : { + clientComponentLoadStart, + clientComponentLoadTimes, + clientComponentLoadCount + }; + if (options.reset) { + clientComponentLoadStart = 0; + clientComponentLoadTimes = 0; + clientComponentLoadCount = 0; + } + return metrics; +} diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/logger/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/logger/output.md new file mode 100644 index 0000000000000..353e06f5e3ad0 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/logger/output.md @@ -0,0 +1,465 @@ +# Items + +Count: 8 + +## Item 1: Stmt 0, `VarDeclarator(0)` + +```js +let clientComponentLoadStart = 0; + +``` + +- Declares: `clientComponentLoadStart` +- Write: `clientComponentLoadStart` + +## Item 2: Stmt 1, `VarDeclarator(0)` + +```js +let clientComponentLoadTimes = 0; + +``` + +- Declares: `clientComponentLoadTimes` +- Write: `clientComponentLoadTimes` + +## Item 3: Stmt 2, `VarDeclarator(0)` + +```js +let clientComponentLoadCount = 0; + +``` + +- Declares: `clientComponentLoadCount` +- Write: `clientComponentLoadCount` + +## Item 4: Stmt 3, `Normal` + +```js +export function wrapClientComponentLoader(ComponentMod) { + if (!('performance' in globalThis)) { + return ComponentMod.__next_app__; + } + return { + require: (...args)=>{ + if (clientComponentLoadStart === 0) { + clientComponentLoadStart = performance.now(); + } + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.require(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + }, + loadChunk: (...args)=>{ + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.loadChunk(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + } + }; +} + +``` + +- Hoisted +- Declares: `wrapClientComponentLoader` +- Reads (eventual): `clientComponentLoadStart` +- Write: `wrapClientComponentLoader` +- Write (eventual): `clientComponentLoadStart`, `clientComponentLoadCount`, `clientComponentLoadTimes` + +## Item 5: Stmt 4, `Normal` + +```js +export function getClientComponentLoaderMetrics(options = {}) { + const metrics = clientComponentLoadStart === 0 ? undefined : { + clientComponentLoadStart, + clientComponentLoadTimes, + clientComponentLoadCount + }; + if (options.reset) { + clientComponentLoadStart = 0; + clientComponentLoadTimes = 0; + clientComponentLoadCount = 0; + } + return metrics; +} + +``` + +- Hoisted +- Declares: `getClientComponentLoaderMetrics` +- Reads (eventual): `clientComponentLoadStart`, `clientComponentLoadTimes`, `clientComponentLoadCount` +- Write: `getClientComponentLoaderMetrics` +- Write (eventual): `clientComponentLoadStart`, `clientComponentLoadTimes`, `clientComponentLoadCount` + +# Phase 1 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export wrapClientComponentLoader"]; + Item8; + Item8["export getClientComponentLoaderMetrics"]; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export wrapClientComponentLoader"]; + Item8; + Item8["export getClientComponentLoaderMetrics"]; + Item7 --> Item4; + Item8 --> Item5; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export wrapClientComponentLoader"]; + Item8; + Item8["export getClientComponentLoaderMetrics"]; + Item7 --> Item4; + Item8 --> Item5; + Item4 --> Item1; + Item4 --> Item3; + Item4 --> Item2; + Item5 --> Item1; + Item5 --> Item2; + Item5 --> Item3; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export wrapClientComponentLoader"]; + Item8; + Item8["export getClientComponentLoaderMetrics"]; + Item7 --> Item4; + Item8 --> Item5; + Item4 --> Item1; + Item4 --> Item3; + Item4 --> Item2; + Item5 --> Item1; + Item5 --> Item2; + Item5 --> Item3; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation)]"]; + N1["Items: [ItemId(Export(("wrapClientComponentLoader", #2), "wrapClientComponentLoader")), ItemId(3, Normal)]"]; + N2["Items: [ItemId(Export(("getClientComponentLoaderMetrics", #2), "getClientComponentLoaderMetrics")), ItemId(4, Normal)]"]; + N3["Items: [ItemId(0, VarDeclarator(0))]"]; + N4["Items: [ItemId(1, VarDeclarator(0))]"]; + N5["Items: [ItemId(2, VarDeclarator(0))]"]; + N1 --> N3; + N1 --> N5; + N1 --> N4; + N2 --> N3; + N2 --> N4; + N2 --> N5; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "getClientComponentLoaderMetrics", + ): 2, + Exports: 6, + Export( + "wrapClientComponentLoader", + ): 1, +} +``` + + +# Modules (dev) +## Part 0 +```js +"module evaluation"; + +``` +## Part 1 +```js +import { clientComponentLoadStart } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { clientComponentLoadCount } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import { clientComponentLoadTimes } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +export { wrapClientComponentLoader }; +function wrapClientComponentLoader(ComponentMod) { + if (!('performance' in globalThis)) { + return ComponentMod.__next_app__; + } + return { + require: (...args)=>{ + if (clientComponentLoadStart === 0) { + clientComponentLoadStart = performance.now(); + } + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.require(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + }, + loadChunk: (...args)=>{ + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.loadChunk(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + } + }; +} +export { wrapClientComponentLoader } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +import { clientComponentLoadStart } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { clientComponentLoadTimes } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import { clientComponentLoadCount } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +export { getClientComponentLoaderMetrics }; +function getClientComponentLoaderMetrics(options = {}) { + const metrics = clientComponentLoadStart === 0 ? undefined : { + clientComponentLoadStart, + clientComponentLoadTimes, + clientComponentLoadCount + }; + if (options.reset) { + clientComponentLoadStart = 0; + clientComponentLoadTimes = 0; + clientComponentLoadCount = 0; + } + return metrics; +} +export { getClientComponentLoaderMetrics } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +let clientComponentLoadStart = 0; +export { clientComponentLoadStart } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 4 +```js +let clientComponentLoadTimes = 0; +export { clientComponentLoadTimes } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 5 +```js +let clientComponentLoadCount = 0; +export { clientComponentLoadCount } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 6 +```js +export { wrapClientComponentLoader } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export wrapClientComponentLoader" +}; +export { getClientComponentLoaderMetrics } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getClientComponentLoaderMetrics" +}; + +``` +## Merged (module eval) +```js +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "getClientComponentLoaderMetrics", + ): 2, + Exports: 6, + Export( + "wrapClientComponentLoader", + ): 1, +} +``` + + +# Modules (prod) +## Part 0 +```js +"module evaluation"; + +``` +## Part 1 +```js +import { clientComponentLoadStart } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { clientComponentLoadCount } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import { clientComponentLoadTimes } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +export { wrapClientComponentLoader }; +function wrapClientComponentLoader(ComponentMod) { + if (!('performance' in globalThis)) { + return ComponentMod.__next_app__; + } + return { + require: (...args)=>{ + if (clientComponentLoadStart === 0) { + clientComponentLoadStart = performance.now(); + } + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.require(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + }, + loadChunk: (...args)=>{ + const startTime = performance.now(); + try { + clientComponentLoadCount += 1; + return ComponentMod.__next_app__.loadChunk(...args); + } finally{ + clientComponentLoadTimes += performance.now() - startTime; + } + } + }; +} +export { wrapClientComponentLoader } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +import { clientComponentLoadStart } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import { clientComponentLoadTimes } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import { clientComponentLoadCount } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +export { getClientComponentLoaderMetrics }; +function getClientComponentLoaderMetrics(options = {}) { + const metrics = clientComponentLoadStart === 0 ? undefined : { + clientComponentLoadStart, + clientComponentLoadTimes, + clientComponentLoadCount + }; + if (options.reset) { + clientComponentLoadStart = 0; + clientComponentLoadTimes = 0; + clientComponentLoadCount = 0; + } + return metrics; +} +export { getClientComponentLoaderMetrics } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +let clientComponentLoadStart = 0; +export { clientComponentLoadStart } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 4 +```js +let clientComponentLoadTimes = 0; +export { clientComponentLoadTimes } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 5 +```js +let clientComponentLoadCount = 0; +export { clientComponentLoadCount } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 6 +```js +export { wrapClientComponentLoader } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export wrapClientComponentLoader" +}; +export { getClientComponentLoaderMetrics } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getClientComponentLoaderMetrics" +}; + +``` +## Merged (module eval) +```js +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/multi-export/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/multi-export/output.md index add30fe1a5e6a..ec441ed9dee16 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/multi-export/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/multi-export/output.md @@ -45,6 +45,7 @@ graph TD Item4["export DOG"]; Item5; Item5["export cat"]; + Item4 --> Item1; Item5 --> Item2; ``` # Phase 3 @@ -58,6 +59,7 @@ graph TD Item4["export DOG"]; Item5; Item5["export cat"]; + Item4 --> Item1; Item5 --> Item2; ``` # Phase 4 @@ -71,8 +73,8 @@ graph TD Item4["export DOG"]; Item5; Item5["export cat"]; - Item5 --> Item2; Item4 --> Item1; + Item5 --> Item2; ``` # Final ```mermaid @@ -86,6 +88,7 @@ graph TD ``` { ModuleEvaluation: 0, + Exports: 3, Export( "cat", ): 2, @@ -119,6 +122,16 @@ export { cat } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 3 +```js +export { DOG } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export DOG" +}; +export { cat } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export cat" +}; + ``` ## Merged (module eval) ```js @@ -130,6 +143,7 @@ export { cat } from "__TURBOPACK_VAR__" assert { ``` { ModuleEvaluation: 0, + Exports: 3, Export( "cat", ): 2, @@ -163,6 +177,16 @@ export { cat } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 3 +```js +export { DOG } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export DOG" +}; +export { cat } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export cat" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nanoid/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nanoid/input.js new file mode 100644 index 0000000000000..bf12a2b2e4066 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nanoid/input.js @@ -0,0 +1,44 @@ +import crypto from 'crypto' +import { urlAlphabet } from './url-alphabet/index.js' +const POOL_SIZE_MULTIPLIER = 128 +let pool, poolOffset +let fillPool = bytes => { + if (!pool || pool.length < bytes) { + pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER) + crypto.randomFillSync(pool) + poolOffset = 0 + } else if (poolOffset + bytes > pool.length) { + crypto.randomFillSync(pool) + poolOffset = 0 + } + poolOffset += bytes +} +let random = bytes => { + fillPool((bytes -= 0)) + return pool.subarray(poolOffset - bytes, poolOffset) +} +let customRandom = (alphabet, size, getRandom) => { + let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1 + let step = Math.ceil((1.6 * mask * size) / alphabet.length) + return () => { + let id = '' + while (true) { + let bytes = getRandom(step) + let i = step + while (i--) { + id += alphabet[bytes[i] & mask] || '' + if (id.length === size) return id + } + } + } +} +let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random) +let nanoid = (size = 21) => { + fillPool((size -= 0)) + let id = '' + for (let i = poolOffset - size; i < poolOffset; i++) { + id += urlAlphabet[pool[i] & 63] + } + return id +} +export { nanoid, customAlphabet, customRandom, urlAlphabet, random } diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nanoid/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nanoid/output.md new file mode 100644 index 0000000000000..229602f229f54 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nanoid/output.md @@ -0,0 +1,960 @@ +# Items + +Count: 18 + +## Item 1: Stmt 0, `ImportOfModule` + +```js +import crypto from 'crypto'; + +``` + +- Hoisted +- Side effects + +## Item 2: Stmt 0, `ImportBinding(0)` + +```js +import crypto from 'crypto'; + +``` + +- Hoisted +- Declares: `crypto` + +## Item 3: Stmt 1, `ImportOfModule` + +```js +import { urlAlphabet } from './url-alphabet/index.js'; + +``` + +- Hoisted +- Side effects + +## Item 4: Stmt 1, `ImportBinding(0)` + +```js +import { urlAlphabet } from './url-alphabet/index.js'; + +``` + +- Hoisted +- Declares: `urlAlphabet` + +## Item 5: Stmt 2, `VarDeclarator(0)` + +```js +const POOL_SIZE_MULTIPLIER = 128; + +``` + +- Declares: `POOL_SIZE_MULTIPLIER` +- Write: `POOL_SIZE_MULTIPLIER` + +## Item 6: Stmt 3, `VarDeclarator(0)` + +```js +let pool, poolOffset; + +``` + +- Declares: `pool` +- Write: `pool` + +## Item 7: Stmt 3, `VarDeclarator(1)` + +```js +let pool, poolOffset; + +``` + +- Declares: `poolOffset` +- Write: `poolOffset` + +## Item 8: Stmt 4, `VarDeclarator(0)` + +```js +let fillPool = (bytes)=>{ + if (!pool || pool.length < bytes) { + pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER); + crypto.randomFillSync(pool); + poolOffset = 0; + } else if (poolOffset + bytes > pool.length) { + crypto.randomFillSync(pool); + poolOffset = 0; + } + poolOffset += bytes; +}; + +``` + +- Side effects +- Declares: `fillPool` +- Reads: `pool`, `POOL_SIZE_MULTIPLIER`, `crypto`, `poolOffset` +- Write: `fillPool`, `pool`, `crypto`, `poolOffset` + +## Item 9: Stmt 5, `VarDeclarator(0)` + +```js +let random = (bytes)=>{ + fillPool((bytes -= 0)); + return pool.subarray(poolOffset - bytes, poolOffset); +}; + +``` + +- Declares: `random` +- Reads: `fillPool`, `pool`, `poolOffset` +- Write: `random`, `pool` + +## Item 10: Stmt 6, `VarDeclarator(0)` + +```js +let customRandom = (alphabet, size, getRandom)=>{ + let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1; + let step = Math.ceil((1.6 * mask * size) / alphabet.length); + return ()=>{ + let id = ''; + while(true){ + let bytes = getRandom(step); + let i = step; + while(i--){ + id += alphabet[bytes[i] & mask] || ''; + if (id.length === size) return id; + } + } + }; +}; + +``` + +- Side effects +- Declares: `customRandom` +- Write: `customRandom` + +## Item 11: Stmt 7, `VarDeclarator(0)` + +```js +let customAlphabet = (alphabet, size)=>customRandom(alphabet, size, random); + +``` + +- Declares: `customAlphabet` +- Reads: `customRandom`, `random` +- Write: `customAlphabet` + +## Item 12: Stmt 8, `VarDeclarator(0)` + +```js +let nanoid = (size = 21)=>{ + fillPool((size -= 0)); + let id = ''; + for(let i = poolOffset - size; i < poolOffset; i++){ + id += urlAlphabet[pool[i] & 63]; + } + return id; +}; + +``` + +- Declares: `nanoid` +- Reads: `fillPool`, `poolOffset`, `urlAlphabet`, `pool` +- Write: `nanoid`, `urlAlphabet`, `pool` + +# Phase 1 +```mermaid +graph TD + Item1; + Item3; + Item2; + Item4; + Item5; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item13["ModuleEvaluation"]; + Item14; + Item14["export nanoid"]; + Item15; + Item15["export customAlphabet"]; + Item16; + Item16["export customRandom"]; + Item17; + Item17["export urlAlphabet"]; + Item18; + Item18["export random"]; + Item2 --> Item1; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item3; + Item2; + Item4; + Item5; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item13["ModuleEvaluation"]; + Item14; + Item14["export nanoid"]; + Item15; + Item15["export customAlphabet"]; + Item16; + Item16["export customRandom"]; + Item17; + Item17["export urlAlphabet"]; + Item18; + Item18["export random"]; + Item2 --> Item1; + Item8 --> Item6; + Item8 --> Item5; + Item8 --> Item3; + Item8 --> Item7; + Item8 --> Item1; + Item8 --> Item2; + Item9 --> Item8; + Item9 --> Item6; + Item9 --> Item7; + Item10 --> Item1; + Item10 --> Item2; + Item10 --> Item8; + Item11 --> Item10; + Item11 --> Item9; + Item12 --> Item8; + Item12 --> Item7; + Item12 --> Item4; + Item12 --> Item9; + Item12 --> Item6; + Item14 --> Item12; + Item15 --> Item11; + Item16 --> Item10; + Item17 --> Item12; + Item17 --> Item4; + Item18 --> Item9; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item3; + Item2; + Item4; + Item5; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item13["ModuleEvaluation"]; + Item14; + Item14["export nanoid"]; + Item15; + Item15["export customAlphabet"]; + Item16; + Item16["export customRandom"]; + Item17; + Item17["export urlAlphabet"]; + Item18; + Item18["export random"]; + Item2 --> Item1; + Item8 --> Item6; + Item8 --> Item5; + Item8 --> Item3; + Item8 --> Item7; + Item8 --> Item1; + Item8 --> Item2; + Item9 --> Item8; + Item9 --> Item6; + Item9 --> Item7; + Item10 --> Item1; + Item10 --> Item2; + Item10 --> Item8; + Item11 --> Item10; + Item11 --> Item9; + Item12 --> Item8; + Item12 --> Item7; + Item12 --> Item4; + Item12 --> Item9; + Item12 --> Item6; + Item14 --> Item12; + Item15 --> Item11; + Item16 --> Item10; + Item17 --> Item12; + Item17 --> Item4; + Item18 --> Item9; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item3; + Item2; + Item4; + Item5; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item13["ModuleEvaluation"]; + Item14; + Item14["export nanoid"]; + Item15; + Item15["export customAlphabet"]; + Item16; + Item16["export customRandom"]; + Item17; + Item17["export urlAlphabet"]; + Item18; + Item18["export random"]; + Item2 --> Item1; + Item8 --> Item6; + Item8 --> Item5; + Item8 --> Item3; + Item8 --> Item7; + Item8 --> Item1; + Item8 --> Item2; + Item9 --> Item8; + Item9 --> Item6; + Item9 --> Item7; + Item10 --> Item1; + Item10 --> Item2; + Item10 --> Item8; + Item11 --> Item10; + Item11 --> Item9; + Item12 --> Item8; + Item12 --> Item7; + Item12 --> Item4; + Item12 --> Item9; + Item12 --> Item6; + Item14 --> Item12; + Item15 --> Item11; + Item16 --> Item10; + Item17 --> Item12; + Item17 --> Item4; + Item18 --> Item9; + Item13 --> Item1; + Item13 --> Item2; + Item13 --> Item8; + Item13 --> Item10; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation)]"]; + N1["Items: [ItemId(Export(("nanoid", #2), "nanoid"))]"]; + N2["Items: [ItemId(Export(("customAlphabet", #2), "customAlphabet")), ItemId(7, VarDeclarator(0))]"]; + N3["Items: [ItemId(Export(("customRandom", #2), "customRandom"))]"]; + N4["Items: [ItemId(Export(("urlAlphabet", #2), "urlAlphabet")), ItemId(1, ImportBinding(0))]"]; + N5["Items: [ItemId(Export(("random", #2), "random"))]"]; + N6["Items: [ItemId(0, ImportOfModule)]"]; + N7["Items: [ItemId(1, ImportOfModule)]"]; + N8["Items: [ItemId(3, VarDeclarator(0))]"]; + N9["Items: [ItemId(3, VarDeclarator(1))]"]; + N10["Items: [ItemId(0, ImportBinding(0)), ItemId(2, VarDeclarator(0)), ItemId(4, VarDeclarator(0))]"]; + N11["Items: [ItemId(5, VarDeclarator(0))]"]; + N12["Items: [ItemId(6, VarDeclarator(0))]"]; + N13["Items: [ItemId(1, ImportBinding(0)), ItemId(8, VarDeclarator(0))]"]; + N0 --> N6; + N0 --> N7; + N0 --> N10; + N0 --> N12; + N1 --> N13; + N2 --> N12; + N2 --> N11; + N3 --> N12; + N4 --> N13; + N5 --> N11; + N7 --> N6; + N10 --> N8; + N10 --> N9; + N10 --> N6; + N10 --> N7; + N11 --> N10; + N11 --> N8; + N11 --> N9; + N12 --> N6; + N12 --> N7; + N12 --> N10; + N13 --> N10; + N13 --> N9; + N13 --> N11; + N13 --> N8; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Exports: 14, + Export( + "customAlphabet", + ): 2, + Export( + "customRandom", + ): 3, + Export( + "urlAlphabet", + ): 4, + Export( + "random", + ): 5, + Export( + "nanoid", + ): 1, +} +``` + + +# Modules (dev) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { nanoid } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +export { nanoid as nanoid }; + +``` +## Part 2 +```js +import { customRandom } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +import { random } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +export { customAlphabet as customAlphabet }; +let customAlphabet = (alphabet, size)=>customRandom(alphabet, size, random); +export { customAlphabet } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +import { customRandom } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +export { customRandom as customRandom }; + +``` +## Part 4 +```js +import { urlAlphabet } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +export { urlAlphabet as urlAlphabet }; +import { urlAlphabet } from './url-alphabet/index.js'; +export { urlAlphabet } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 5 +```js +import { random } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +export { random as random }; + +``` +## Part 6 +```js +import 'crypto'; + +``` +## Part 7 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import './url-alphabet/index.js'; + +``` +## Part 8 +```js +let pool; +export { pool } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 9 +```js +let poolOffset; +export { poolOffset } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +import { pool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { poolOffset } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import crypto from 'crypto'; +const POOL_SIZE_MULTIPLIER = 128; +let fillPool = (bytes)=>{ + if (!pool || pool.length < bytes) { + pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER); + crypto.randomFillSync(pool); + poolOffset = 0; + } else if (poolOffset + bytes > pool.length) { + crypto.randomFillSync(pool); + poolOffset = 0; + } + poolOffset += bytes; +}; +export { crypto } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { POOL_SIZE_MULTIPLIER } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { fillPool } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 11 +```js +import { fillPool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +import { pool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { poolOffset } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +let random = (bytes)=>{ + fillPool((bytes -= 0)); + return pool.subarray(poolOffset - bytes, poolOffset); +}; +export { random } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 12 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +let customRandom = (alphabet, size, getRandom)=>{ + let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1; + let step = Math.ceil((1.6 * mask * size) / alphabet.length); + return ()=>{ + let id = ''; + while(true){ + let bytes = getRandom(step); + let i = step; + while(i--){ + id += alphabet[bytes[i] & mask] || ''; + if (id.length === size) return id; + } + } + }; +}; +export { customRandom } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 13 +```js +import { fillPool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +import { poolOffset } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +import { pool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { urlAlphabet } from './url-alphabet/index.js'; +let nanoid = (size = 21)=>{ + fillPool((size -= 0)); + let id = ''; + for(let i = poolOffset - size; i < poolOffset; i++){ + id += urlAlphabet[pool[i] & 63]; + } + return id; +}; +export { urlAlphabet } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { nanoid } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 14 +```js +export { nanoid } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export nanoid" +}; +export { customAlphabet } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export customAlphabet" +}; +export { customRandom } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export customRandom" +}; +export { urlAlphabet } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export urlAlphabet" +}; +export { random } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export random" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Exports: 14, + Export( + "customAlphabet", + ): 2, + Export( + "customRandom", + ): 3, + Export( + "urlAlphabet", + ): 4, + Export( + "random", + ): 5, + Export( + "nanoid", + ): 1, +} +``` + + +# Modules (prod) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { nanoid } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +export { nanoid as nanoid }; + +``` +## Part 2 +```js +import { customRandom } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +import { random } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +export { customAlphabet as customAlphabet }; +let customAlphabet = (alphabet, size)=>customRandom(alphabet, size, random); +export { customAlphabet } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +import { customRandom } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +export { customRandom as customRandom }; + +``` +## Part 4 +```js +import { urlAlphabet } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +export { urlAlphabet as urlAlphabet }; +import { urlAlphabet } from './url-alphabet/index.js'; +export { urlAlphabet } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 5 +```js +import { random } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +export { random as random }; + +``` +## Part 6 +```js +import 'crypto'; + +``` +## Part 7 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import './url-alphabet/index.js'; + +``` +## Part 8 +```js +let pool; +export { pool } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 9 +```js +let poolOffset; +export { poolOffset } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +import { pool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { poolOffset } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import crypto from 'crypto'; +const POOL_SIZE_MULTIPLIER = 128; +let fillPool = (bytes)=>{ + if (!pool || pool.length < bytes) { + pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER); + crypto.randomFillSync(pool); + poolOffset = 0; + } else if (poolOffset + bytes > pool.length) { + crypto.randomFillSync(pool); + poolOffset = 0; + } + poolOffset += bytes; +}; +export { crypto } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { POOL_SIZE_MULTIPLIER } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { fillPool } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 11 +```js +import { fillPool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +import { pool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { poolOffset } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +let random = (bytes)=>{ + fillPool((bytes -= 0)); + return pool.subarray(poolOffset - bytes, poolOffset); +}; +export { random } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 12 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +let customRandom = (alphabet, size, getRandom)=>{ + let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1; + let step = Math.ceil((1.6 * mask * size) / alphabet.length); + return ()=>{ + let id = ''; + while(true){ + let bytes = getRandom(step); + let i = step; + while(i--){ + id += alphabet[bytes[i] & mask] || ''; + if (id.length === size) return id; + } + } + }; +}; +export { customRandom } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 13 +```js +import { fillPool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +import { poolOffset } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +import { pool } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { urlAlphabet } from './url-alphabet/index.js'; +let nanoid = (size = 21)=>{ + fillPool((size -= 0)); + let id = ''; + for(let i = poolOffset - size; i < poolOffset; i++){ + id += urlAlphabet[pool[i] & 63]; + } + return id; +}; +export { urlAlphabet } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { nanoid } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 14 +```js +export { nanoid } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export nanoid" +}; +export { customAlphabet } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export customAlphabet" +}; +export { customRandom } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export customRandom" +}; +export { urlAlphabet } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export urlAlphabet" +}; +export { random } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export random" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/next-response/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/next-response/input.js new file mode 100644 index 0000000000000..355cd43370ea1 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/next-response/input.js @@ -0,0 +1,122 @@ +import { stringifyCookie } from '../../web/spec-extension/cookies'; +import { NextURL } from '../next-url'; +import { toNodeOutgoingHttpHeaders, validateURL } from '../utils'; +import { ReflectAdapter } from './adapters/reflect'; +import { ResponseCookies } from './cookies'; +const INTERNALS = Symbol('internal response'); +const REDIRECTS = new Set([ + 301, + 302, + 303, + 307, + 308 +]); +function handleMiddlewareField(init, headers) { + var _init_request; + if (init == null ? void 0 : (_init_request = init.request) == null ? void 0 : _init_request.headers) { + if (!(init.request.headers instanceof Headers)) { + throw new Error('request.headers must be an instance of Headers'); + } + const keys = []; + for (const [key, value] of init.request.headers){ + headers.set('x-middleware-request-' + key, value); + keys.push(key); + } + headers.set('x-middleware-override-headers', keys.join(',')); + } +} +/** + * This class extends the [Web `Response` API](https://developer.mozilla.org/docs/Web/API/Response) with additional convenience methods. + * + * Read more: [Next.js Docs: `NextResponse`](https://nextjs.org/docs/app/api-reference/functions/next-response) + */ export class NextResponse extends Response { + constructor(body, init = {}){ + super(body, init); + const headers = this.headers; + const cookies = new ResponseCookies(headers); + const cookiesProxy = new Proxy(cookies, { + get (target, prop, receiver) { + switch(prop){ + case 'delete': + case 'set': + { + return (...args)=>{ + const result = Reflect.apply(target[prop], target, args); + const newHeaders = new Headers(headers); + if (result instanceof ResponseCookies) { + headers.set('x-middleware-set-cookie', result.getAll().map((cookie)=>stringifyCookie(cookie)).join(',')); + } + handleMiddlewareField(init, newHeaders); + return result; + }; + } + default: + return ReflectAdapter.get(target, prop, receiver); + } + } + }); + this[INTERNALS] = { + cookies: cookiesProxy, + url: init.url ? new NextURL(init.url, { + headers: toNodeOutgoingHttpHeaders(headers), + nextConfig: init.nextConfig + }) : undefined + }; + } + [Symbol.for('edge-runtime.inspect.custom')]() { + return { + cookies: this.cookies, + url: this.url, + // rest of props come from Response + body: this.body, + bodyUsed: this.bodyUsed, + headers: Object.fromEntries(this.headers), + ok: this.ok, + redirected: this.redirected, + status: this.status, + statusText: this.statusText, + type: this.type + }; + } + get cookies() { + return this[INTERNALS].cookies; + } + static json(body, init) { + const response = Response.json(body, init); + return new NextResponse(response.body, response); + } + static redirect(url, init) { + const status = typeof init === 'number' ? init : (init == null ? void 0 : init.status) ?? 307; + if (!REDIRECTS.has(status)) { + throw new RangeError('Failed to execute "redirect" on "response": Invalid status code'); + } + const initObj = typeof init === 'object' ? init : {}; + const headers = new Headers(initObj == null ? void 0 : initObj.headers); + headers.set('Location', validateURL(url)); + return new NextResponse(null, { + ...initObj, + headers, + status + }); + } + static rewrite(destination, init) { + const headers = new Headers(init == null ? void 0 : init.headers); + headers.set('x-middleware-rewrite', validateURL(destination)); + handleMiddlewareField(init, headers); + return new NextResponse(null, { + ...init, + headers + }); + } + static next(init) { + const headers = new Headers(init == null ? void 0 : init.headers); + headers.set('x-middleware-next', '1'); + handleMiddlewareField(init, headers); + return new NextResponse(null, { + ...init, + headers + }); + } +} + +//# sourceMappingURL=response.js.map diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/next-response/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/next-response/output.md new file mode 100644 index 0000000000000..20d9b441f54eb --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/next-response/output.md @@ -0,0 +1,1161 @@ +# Items + +Count: 17 + +## Item 1: Stmt 0, `ImportOfModule` + +```js +import { stringifyCookie } from '../../web/spec-extension/cookies'; + +``` + +- Hoisted +- Side effects + +## Item 2: Stmt 0, `ImportBinding(0)` + +```js +import { stringifyCookie } from '../../web/spec-extension/cookies'; + +``` + +- Hoisted +- Declares: `stringifyCookie` + +## Item 3: Stmt 1, `ImportOfModule` + +```js +import { NextURL } from '../next-url'; + +``` + +- Hoisted +- Side effects + +## Item 4: Stmt 1, `ImportBinding(0)` + +```js +import { NextURL } from '../next-url'; + +``` + +- Hoisted +- Declares: `NextURL` + +## Item 5: Stmt 2, `ImportOfModule` + +```js +import { toNodeOutgoingHttpHeaders, validateURL } from '../utils'; + +``` + +- Hoisted +- Side effects + +## Item 6: Stmt 2, `ImportBinding(0)` + +```js +import { toNodeOutgoingHttpHeaders, validateURL } from '../utils'; + +``` + +- Hoisted +- Declares: `toNodeOutgoingHttpHeaders` + +## Item 7: Stmt 2, `ImportBinding(1)` + +```js +import { toNodeOutgoingHttpHeaders, validateURL } from '../utils'; + +``` + +- Hoisted +- Declares: `validateURL` + +## Item 8: Stmt 3, `ImportOfModule` + +```js +import { ReflectAdapter } from './adapters/reflect'; + +``` + +- Hoisted +- Side effects + +## Item 9: Stmt 3, `ImportBinding(0)` + +```js +import { ReflectAdapter } from './adapters/reflect'; + +``` + +- Hoisted +- Declares: `ReflectAdapter` + +## Item 10: Stmt 4, `ImportOfModule` + +```js +import { ResponseCookies } from './cookies'; + +``` + +- Hoisted +- Side effects + +## Item 11: Stmt 4, `ImportBinding(0)` + +```js +import { ResponseCookies } from './cookies'; + +``` + +- Hoisted +- Declares: `ResponseCookies` + +## Item 12: Stmt 5, `VarDeclarator(0)` + +```js +const INTERNALS = Symbol('internal response'); + +``` + +- Side effects +- Declares: `INTERNALS` +- Write: `INTERNALS` + +## Item 13: Stmt 6, `VarDeclarator(0)` + +```js +const REDIRECTS = new Set([ + 301, + 302, + 303, + 307, + 308 +]); + +``` + +- Side effects +- Declares: `REDIRECTS` +- Write: `REDIRECTS` + +## Item 14: Stmt 7, `Normal` + +```js +function handleMiddlewareField(init, headers) { + var _init_request; + if (init == null ? void 0 : (_init_request = init.request) == null ? void 0 : _init_request.headers) { + if (!(init.request.headers instanceof Headers)) { + throw new Error('request.headers must be an instance of Headers'); + } + const keys = []; + for (const [key, value] of init.request.headers){ + headers.set('x-middleware-request-' + key, value); + keys.push(key); + } + headers.set('x-middleware-override-headers', keys.join(',')); + } +} + +``` + +- Hoisted +- Declares: `handleMiddlewareField` +- Write: `handleMiddlewareField` + +## Item 15: Stmt 8, `Normal` + +```js +export class NextResponse extends Response { + constructor(body, init = {}){ + super(body, init); + const headers = this.headers; + const cookies = new ResponseCookies(headers); + const cookiesProxy = new Proxy(cookies, { + get (target, prop, receiver) { + switch(prop){ + case 'delete': + case 'set': + { + return (...args)=>{ + const result = Reflect.apply(target[prop], target, args); + const newHeaders = new Headers(headers); + if (result instanceof ResponseCookies) { + headers.set('x-middleware-set-cookie', result.getAll().map((cookie)=>stringifyCookie(cookie)).join(',')); + } + handleMiddlewareField(init, newHeaders); + return result; + }; + } + default: + return ReflectAdapter.get(target, prop, receiver); + } + } + }); + this[INTERNALS] = { + cookies: cookiesProxy, + url: init.url ? new NextURL(init.url, { + headers: toNodeOutgoingHttpHeaders(headers), + nextConfig: init.nextConfig + }) : undefined + }; + } + [Symbol.for('edge-runtime.inspect.custom')]() { + return { + cookies: this.cookies, + url: this.url, + body: this.body, + bodyUsed: this.bodyUsed, + headers: Object.fromEntries(this.headers), + ok: this.ok, + redirected: this.redirected, + status: this.status, + statusText: this.statusText, + type: this.type + }; + } + get cookies() { + return this[INTERNALS].cookies; + } + static json(body, init) { + const response = Response.json(body, init); + return new NextResponse(response.body, response); + } + static redirect(url, init) { + const status = typeof init === 'number' ? init : (init == null ? void 0 : init.status) ?? 307; + if (!REDIRECTS.has(status)) { + throw new RangeError('Failed to execute "redirect" on "response": Invalid status code'); + } + const initObj = typeof init === 'object' ? init : {}; + const headers = new Headers(initObj == null ? void 0 : initObj.headers); + headers.set('Location', validateURL(url)); + return new NextResponse(null, { + ...initObj, + headers, + status + }); + } + static rewrite(destination, init) { + const headers = new Headers(init == null ? void 0 : init.headers); + headers.set('x-middleware-rewrite', validateURL(destination)); + handleMiddlewareField(init, headers); + return new NextResponse(null, { + ...init, + headers + }); + } + static next(init) { + const headers = new Headers(init == null ? void 0 : init.headers); + headers.set('x-middleware-next', '1'); + handleMiddlewareField(init, headers); + return new NextResponse(null, { + ...init, + headers + }); + } +} + +``` + +- Declares: `NextResponse` +- Reads: `ResponseCookies`, `stringifyCookie`, `handleMiddlewareField`, `ReflectAdapter`, `INTERNALS`, `NextURL`, `toNodeOutgoingHttpHeaders`, `NextResponse`, `REDIRECTS`, `validateURL` +- Write: `ReflectAdapter`, `REDIRECTS`, `NextResponse` + +# Phase 1 +```mermaid +graph TD + Item1; + Item6; + Item2; + Item7; + Item3; + Item8; + Item9; + Item4; + Item10; + Item5; + Item11; + Item12; + Item13; + Item14; + Item15; + Item16; + Item16["ModuleEvaluation"]; + Item17; + Item17["export NextResponse"]; + Item2 --> Item1; + Item3 --> Item1; + Item3 --> Item2; + Item4 --> Item1; + Item4 --> Item2; + Item4 --> Item3; + Item5 --> Item1; + Item5 --> Item2; + Item5 --> Item3; + Item5 --> Item4; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item6; + Item2; + Item7; + Item3; + Item8; + Item9; + Item4; + Item10; + Item5; + Item11; + Item12; + Item13; + Item14; + Item15; + Item16; + Item16["ModuleEvaluation"]; + Item17; + Item17["export NextResponse"]; + Item2 --> Item1; + Item3 --> Item1; + Item3 --> Item2; + Item4 --> Item1; + Item4 --> Item2; + Item4 --> Item3; + Item5 --> Item1; + Item5 --> Item2; + Item5 --> Item3; + Item5 --> Item4; + Item12 --> Item1; + Item12 --> Item2; + Item12 --> Item3; + Item12 --> Item4; + Item12 --> Item5; + Item13 --> Item1; + Item13 --> Item2; + Item13 --> Item3; + Item13 --> Item4; + Item13 --> Item5; + Item13 --> Item12; + Item15 --> Item11; + Item15 --> Item6; + Item15 --> Item14; + Item15 --> Item10; + Item15 --> Item12; + Item15 --> Item7; + Item15 --> Item8; + Item15 --> Item13; + Item15 --> Item9; + Item17 --> Item15; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item6; + Item2; + Item7; + Item3; + Item8; + Item9; + Item4; + Item10; + Item5; + Item11; + Item12; + Item13; + Item14; + Item15; + Item16; + Item16["ModuleEvaluation"]; + Item17; + Item17["export NextResponse"]; + Item2 --> Item1; + Item3 --> Item1; + Item3 --> Item2; + Item4 --> Item1; + Item4 --> Item2; + Item4 --> Item3; + Item5 --> Item1; + Item5 --> Item2; + Item5 --> Item3; + Item5 --> Item4; + Item12 --> Item1; + Item12 --> Item2; + Item12 --> Item3; + Item12 --> Item4; + Item12 --> Item5; + Item13 --> Item1; + Item13 --> Item2; + Item13 --> Item3; + Item13 --> Item4; + Item13 --> Item5; + Item13 --> Item12; + Item15 --> Item11; + Item15 --> Item6; + Item15 --> Item14; + Item15 --> Item10; + Item15 --> Item12; + Item15 --> Item7; + Item15 --> Item8; + Item15 --> Item13; + Item15 --> Item9; + Item17 --> Item15; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item6; + Item2; + Item7; + Item3; + Item8; + Item9; + Item4; + Item10; + Item5; + Item11; + Item12; + Item13; + Item14; + Item15; + Item16; + Item16["ModuleEvaluation"]; + Item17; + Item17["export NextResponse"]; + Item2 --> Item1; + Item3 --> Item1; + Item3 --> Item2; + Item4 --> Item1; + Item4 --> Item2; + Item4 --> Item3; + Item5 --> Item1; + Item5 --> Item2; + Item5 --> Item3; + Item5 --> Item4; + Item12 --> Item1; + Item12 --> Item2; + Item12 --> Item3; + Item12 --> Item4; + Item12 --> Item5; + Item13 --> Item1; + Item13 --> Item2; + Item13 --> Item3; + Item13 --> Item4; + Item13 --> Item5; + Item13 --> Item12; + Item15 --> Item11; + Item15 --> Item6; + Item15 --> Item14; + Item15 --> Item10; + Item15 --> Item12; + Item15 --> Item7; + Item15 --> Item8; + Item15 --> Item13; + Item15 --> Item9; + Item17 --> Item15; + Item16 --> Item1; + Item16 --> Item2; + Item16 --> Item3; + Item16 --> Item4; + Item16 --> Item5; + Item16 --> Item12; + Item16 --> Item13; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation)]"]; + N1["Items: [ItemId(Export(("NextResponse", #2), "NextResponse")), ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(2, ImportBinding(1)), ItemId(3, ImportBinding(0)), ItemId(4, ImportBinding(0)), ItemId(7, Normal), ItemId(8, Normal)]"]; + N2["Items: [ItemId(0, ImportOfModule)]"]; + N3["Items: [ItemId(1, ImportOfModule)]"]; + N4["Items: [ItemId(2, ImportOfModule)]"]; + N5["Items: [ItemId(3, ImportOfModule)]"]; + N6["Items: [ItemId(4, ImportOfModule)]"]; + N7["Items: [ItemId(5, VarDeclarator(0))]"]; + N8["Items: [ItemId(6, VarDeclarator(0))]"]; + N0 --> N2; + N0 --> N3; + N0 --> N4; + N0 --> N5; + N0 --> N6; + N0 --> N7; + N0 --> N8; + N1 --> N7; + N1 --> N8; + N3 --> N2; + N4 --> N2; + N4 --> N3; + N5 --> N2; + N5 --> N3; + N5 --> N4; + N6 --> N2; + N6 --> N3; + N6 --> N4; + N6 --> N5; + N7 --> N2; + N7 --> N3; + N7 --> N4; + N7 --> N5; + N7 --> N6; + N8 --> N2; + N8 --> N3; + N8 --> N4; + N8 --> N5; + N8 --> N6; + N8 --> N7; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "NextResponse", + ): 1, + Exports: 9, +} +``` + + +# Modules (dev) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { INTERNALS } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import { REDIRECTS } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +export { NextResponse }; +import { stringifyCookie } from '../../web/spec-extension/cookies'; +import { NextURL } from '../next-url'; +import { toNodeOutgoingHttpHeaders } from '../utils'; +import { validateURL } from '../utils'; +import { ReflectAdapter } from './adapters/reflect'; +import { ResponseCookies } from './cookies'; +function handleMiddlewareField(init, headers) { + var _init_request; + if (init == null ? void 0 : (_init_request = init.request) == null ? void 0 : _init_request.headers) { + if (!(init.request.headers instanceof Headers)) { + throw new Error('request.headers must be an instance of Headers'); + } + const keys = []; + for (const [key, value] of init.request.headers){ + headers.set('x-middleware-request-' + key, value); + keys.push(key); + } + headers.set('x-middleware-override-headers', keys.join(',')); + } +} +class NextResponse extends Response { + constructor(body, init = {}){ + super(body, init); + const headers = this.headers; + const cookies = new ResponseCookies(headers); + const cookiesProxy = new Proxy(cookies, { + get (target, prop, receiver) { + switch(prop){ + case 'delete': + case 'set': + { + return (...args)=>{ + const result = Reflect.apply(target[prop], target, args); + const newHeaders = new Headers(headers); + if (result instanceof ResponseCookies) { + headers.set('x-middleware-set-cookie', result.getAll().map((cookie)=>stringifyCookie(cookie)).join(',')); + } + handleMiddlewareField(init, newHeaders); + return result; + }; + } + default: + return ReflectAdapter.get(target, prop, receiver); + } + } + }); + this[INTERNALS] = { + cookies: cookiesProxy, + url: init.url ? new NextURL(init.url, { + headers: toNodeOutgoingHttpHeaders(headers), + nextConfig: init.nextConfig + }) : undefined + }; + } + [Symbol.for('edge-runtime.inspect.custom')]() { + return { + cookies: this.cookies, + url: this.url, + body: this.body, + bodyUsed: this.bodyUsed, + headers: Object.fromEntries(this.headers), + ok: this.ok, + redirected: this.redirected, + status: this.status, + statusText: this.statusText, + type: this.type + }; + } + get cookies() { + return this[INTERNALS].cookies; + } + static json(body, init) { + const response = Response.json(body, init); + return new NextResponse(response.body, response); + } + static redirect(url, init) { + const status = typeof init === 'number' ? init : (init == null ? void 0 : init.status) ?? 307; + if (!REDIRECTS.has(status)) { + throw new RangeError('Failed to execute "redirect" on "response": Invalid status code'); + } + const initObj = typeof init === 'object' ? init : {}; + const headers = new Headers(initObj == null ? void 0 : initObj.headers); + headers.set('Location', validateURL(url)); + return new NextResponse(null, { + ...initObj, + headers, + status + }); + } + static rewrite(destination, init) { + const headers = new Headers(init == null ? void 0 : init.headers); + headers.set('x-middleware-rewrite', validateURL(destination)); + handleMiddlewareField(init, headers); + return new NextResponse(null, { + ...init, + headers + }); + } + static next(init) { + const headers = new Headers(init == null ? void 0 : init.headers); + headers.set('x-middleware-next', '1'); + handleMiddlewareField(init, headers); + return new NextResponse(null, { + ...init, + headers + }); + } +} +export { stringifyCookie } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextURL } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { toNodeOutgoingHttpHeaders } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { validateURL } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { ReflectAdapter } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { ResponseCookies } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { handleMiddlewareField } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextResponse } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +import '../../web/spec-extension/cookies'; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import '../next-url'; + +``` +## Part 4 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import '../utils'; + +``` +## Part 5 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import './adapters/reflect'; + +``` +## Part 6 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import './cookies'; + +``` +## Part 7 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +const INTERNALS = Symbol('internal response'); +export { INTERNALS } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 8 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +const REDIRECTS = new Set([ + 301, + 302, + 303, + 307, + 308 +]); +export { REDIRECTS } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 9 +```js +export { NextResponse } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export NextResponse" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "NextResponse", + ): 1, + Exports: 9, +} +``` + + +# Modules (prod) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { INTERNALS } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import { REDIRECTS } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +export { NextResponse }; +import { stringifyCookie } from '../../web/spec-extension/cookies'; +import { NextURL } from '../next-url'; +import { toNodeOutgoingHttpHeaders } from '../utils'; +import { validateURL } from '../utils'; +import { ReflectAdapter } from './adapters/reflect'; +import { ResponseCookies } from './cookies'; +function handleMiddlewareField(init, headers) { + var _init_request; + if (init == null ? void 0 : (_init_request = init.request) == null ? void 0 : _init_request.headers) { + if (!(init.request.headers instanceof Headers)) { + throw new Error('request.headers must be an instance of Headers'); + } + const keys = []; + for (const [key, value] of init.request.headers){ + headers.set('x-middleware-request-' + key, value); + keys.push(key); + } + headers.set('x-middleware-override-headers', keys.join(',')); + } +} +class NextResponse extends Response { + constructor(body, init = {}){ + super(body, init); + const headers = this.headers; + const cookies = new ResponseCookies(headers); + const cookiesProxy = new Proxy(cookies, { + get (target, prop, receiver) { + switch(prop){ + case 'delete': + case 'set': + { + return (...args)=>{ + const result = Reflect.apply(target[prop], target, args); + const newHeaders = new Headers(headers); + if (result instanceof ResponseCookies) { + headers.set('x-middleware-set-cookie', result.getAll().map((cookie)=>stringifyCookie(cookie)).join(',')); + } + handleMiddlewareField(init, newHeaders); + return result; + }; + } + default: + return ReflectAdapter.get(target, prop, receiver); + } + } + }); + this[INTERNALS] = { + cookies: cookiesProxy, + url: init.url ? new NextURL(init.url, { + headers: toNodeOutgoingHttpHeaders(headers), + nextConfig: init.nextConfig + }) : undefined + }; + } + [Symbol.for('edge-runtime.inspect.custom')]() { + return { + cookies: this.cookies, + url: this.url, + body: this.body, + bodyUsed: this.bodyUsed, + headers: Object.fromEntries(this.headers), + ok: this.ok, + redirected: this.redirected, + status: this.status, + statusText: this.statusText, + type: this.type + }; + } + get cookies() { + return this[INTERNALS].cookies; + } + static json(body, init) { + const response = Response.json(body, init); + return new NextResponse(response.body, response); + } + static redirect(url, init) { + const status = typeof init === 'number' ? init : (init == null ? void 0 : init.status) ?? 307; + if (!REDIRECTS.has(status)) { + throw new RangeError('Failed to execute "redirect" on "response": Invalid status code'); + } + const initObj = typeof init === 'object' ? init : {}; + const headers = new Headers(initObj == null ? void 0 : initObj.headers); + headers.set('Location', validateURL(url)); + return new NextResponse(null, { + ...initObj, + headers, + status + }); + } + static rewrite(destination, init) { + const headers = new Headers(init == null ? void 0 : init.headers); + headers.set('x-middleware-rewrite', validateURL(destination)); + handleMiddlewareField(init, headers); + return new NextResponse(null, { + ...init, + headers + }); + } + static next(init) { + const headers = new Headers(init == null ? void 0 : init.headers); + headers.set('x-middleware-next', '1'); + handleMiddlewareField(init, headers); + return new NextResponse(null, { + ...init, + headers + }); + } +} +export { stringifyCookie } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextURL } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { toNodeOutgoingHttpHeaders } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { validateURL } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { ReflectAdapter } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { ResponseCookies } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { handleMiddlewareField } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextResponse } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +import '../../web/spec-extension/cookies'; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import '../next-url'; + +``` +## Part 4 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import '../utils'; + +``` +## Part 5 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import './adapters/reflect'; + +``` +## Part 6 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import './cookies'; + +``` +## Part 7 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +const INTERNALS = Symbol('internal response'); +export { INTERNALS } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 8 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +const REDIRECTS = new Set([ + 301, + 302, + 303, + 307, + 308 +]); +export { REDIRECTS } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 9 +```js +export { NextResponse } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export NextResponse" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nextjs-tracer/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nextjs-tracer/input.js new file mode 100644 index 0000000000000..3fafa47598599 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nextjs-tracer/input.js @@ -0,0 +1,214 @@ +import { LogSpanAllowList, NextVanillaSpanAllowlist } from './constants'; +let api; +// we want to allow users to use their own version of @opentelemetry/api if they +// want to, so we try to require it first, and if it fails we fall back to the +// version that is bundled with Next.js +// this is because @opentelemetry/api has to be synced with the version of +// @opentelemetry/tracing that is used, and we don't want to force users to use +// the version that is bundled with Next.js. +// the API is ~stable, so this should be fine +if (process.env.NEXT_RUNTIME === 'edge') { + api = require('@opentelemetry/api'); +} else { + try { + api = require('@opentelemetry/api'); + } catch (err) { + api = require('next/dist/compiled/@opentelemetry/api'); + } +} +const { context, propagation, trace, SpanStatusCode, SpanKind, ROOT_CONTEXT } = api; +const isPromise = (p)=>{ + return p !== null && typeof p === 'object' && typeof p.then === 'function'; +}; +export class BubbledError extends Error { + constructor(bubble, result){ + super(); + this.bubble = bubble; + this.result = result; + } +} +export function isBubbledError(error) { + if (typeof error !== 'object' || error === null) return false; + return error instanceof BubbledError; +} +const closeSpanWithError = (span, error)=>{ + if (isBubbledError(error) && error.bubble) { + span.setAttribute('next.bubble', true); + } else { + if (error) { + span.recordException(error); + } + span.setStatus({ + code: SpanStatusCode.ERROR, + message: error == null ? void 0 : error.message + }); + } + span.end(); +}; +/** we use this map to propagate attributes from nested spans to the top span */ const rootSpanAttributesStore = new Map(); +const rootSpanIdKey = api.createContextKey('next.rootSpanId'); +let lastSpanId = 0; +const getSpanId = ()=>lastSpanId++; +const clientTraceDataSetter = { + set (carrier, key, value) { + carrier.push({ + key, + value + }); + } +}; +class NextTracerImpl { + /** + * Returns an instance to the trace with configured name. + * Since wrap / trace can be defined in any place prior to actual trace subscriber initialization, + * This should be lazily evaluated. + */ getTracerInstance() { + return trace.getTracer('next.js', '0.0.1'); + } + getContext() { + return context; + } + getTracePropagationData() { + const activeContext = context.active(); + const entries = []; + propagation.inject(activeContext, entries, clientTraceDataSetter); + return entries; + } + getActiveScopeSpan() { + return trace.getSpan(context == null ? void 0 : context.active()); + } + withPropagatedContext(carrier, fn, getter) { + const activeContext = context.active(); + if (trace.getSpanContext(activeContext)) { + // Active span is already set, too late to propagate. + return fn(); + } + const remoteContext = propagation.extract(activeContext, carrier, getter); + return context.with(remoteContext, fn); + } + trace(...args) { + var _trace_getSpanContext; + const [type, fnOrOptions, fnOrEmpty] = args; + // coerce options form overload + const { fn, options } = typeof fnOrOptions === 'function' ? { + fn: fnOrOptions, + options: {} + } : { + fn: fnOrEmpty, + options: { + ...fnOrOptions + } + }; + const spanName = options.spanName ?? type; + if (!NextVanillaSpanAllowlist.includes(type) && process.env.NEXT_OTEL_VERBOSE !== '1' || options.hideSpan) { + return fn(); + } + // Trying to get active scoped span to assign parent. If option specifies parent span manually, will try to use it. + let spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan()); + let isRootSpan = false; + if (!spanContext) { + spanContext = (context == null ? void 0 : context.active()) ?? ROOT_CONTEXT; + isRootSpan = true; + } else if ((_trace_getSpanContext = trace.getSpanContext(spanContext)) == null ? void 0 : _trace_getSpanContext.isRemote) { + isRootSpan = true; + } + const spanId = getSpanId(); + options.attributes = { + 'next.span_name': spanName, + 'next.span_type': type, + ...options.attributes + }; + return context.with(spanContext.setValue(rootSpanIdKey, spanId), ()=>this.getTracerInstance().startActiveSpan(spanName, options, (span)=>{ + const startTime = 'performance' in globalThis && 'measure' in performance ? globalThis.performance.now() : undefined; + const onCleanup = ()=>{ + rootSpanAttributesStore.delete(spanId); + if (startTime && process.env.NEXT_OTEL_PERFORMANCE_PREFIX && LogSpanAllowList.includes(type || '')) { + performance.measure(`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-${(type.split('.').pop() || '').replace(/[A-Z]/g, (match)=>'-' + match.toLowerCase())}`, { + start: startTime, + end: performance.now() + }); + } + }; + if (isRootSpan) { + rootSpanAttributesStore.set(spanId, new Map(Object.entries(options.attributes ?? {}))); + } + try { + if (fn.length > 1) { + return fn(span, (err)=>closeSpanWithError(span, err)); + } + const result = fn(span); + if (isPromise(result)) { + // If there's error make sure it throws + return result.then((res)=>{ + span.end(); + // Need to pass down the promise result, + // it could be react stream response with error { error, stream } + return res; + }).catch((err)=>{ + closeSpanWithError(span, err); + throw err; + }).finally(onCleanup); + } else { + span.end(); + onCleanup(); + } + return result; + } catch (err) { + closeSpanWithError(span, err); + onCleanup(); + throw err; + } + })); + } + wrap(...args) { + const tracer = this; + const [name, options, fn] = args.length === 3 ? args : [ + args[0], + {}, + args[1] + ]; + if (!NextVanillaSpanAllowlist.includes(name) && process.env.NEXT_OTEL_VERBOSE !== '1') { + return fn; + } + return function() { + let optionsObj = options; + if (typeof optionsObj === 'function' && typeof fn === 'function') { + optionsObj = optionsObj.apply(this, arguments); + } + const lastArgId = arguments.length - 1; + const cb = arguments[lastArgId]; + if (typeof cb === 'function') { + const scopeBoundCb = tracer.getContext().bind(context.active(), cb); + return tracer.trace(name, optionsObj, (_span, done)=>{ + arguments[lastArgId] = function(err) { + done == null ? void 0 : done(err); + return scopeBoundCb.apply(this, arguments); + }; + return fn.apply(this, arguments); + }); + } else { + return tracer.trace(name, optionsObj, ()=>fn.apply(this, arguments)); + } + }; + } + startSpan(...args) { + const [type, options] = args; + const spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan()); + return this.getTracerInstance().startSpan(type, options, spanContext); + } + getSpanContext(parentSpan) { + const spanContext = parentSpan ? trace.setSpan(context.active(), parentSpan) : undefined; + return spanContext; + } + getRootSpanAttributes() { + const spanId = context.active().getValue(rootSpanIdKey); + return rootSpanAttributesStore.get(spanId); + } +} +const getTracer = (()=>{ + const tracer = new NextTracerImpl(); + return ()=>tracer; +})(); +export { getTracer, SpanStatusCode, SpanKind }; + +//# sourceMappingURL=tracer.js.map diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nextjs-tracer/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nextjs-tracer/output.md new file mode 100644 index 0000000000000..e8c8203b76367 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/nextjs-tracer/output.md @@ -0,0 +1,1516 @@ +# Items + +Count: 23 + +## Item 1: Stmt 0, `ImportOfModule` + +```js +import { LogSpanAllowList, NextVanillaSpanAllowlist } from './constants'; + +``` + +- Hoisted +- Side effects + +## Item 2: Stmt 0, `ImportBinding(0)` + +```js +import { LogSpanAllowList, NextVanillaSpanAllowlist } from './constants'; + +``` + +- Hoisted +- Declares: `LogSpanAllowList` + +## Item 3: Stmt 0, `ImportBinding(1)` + +```js +import { LogSpanAllowList, NextVanillaSpanAllowlist } from './constants'; + +``` + +- Hoisted +- Declares: `NextVanillaSpanAllowlist` + +## Item 4: Stmt 1, `VarDeclarator(0)` + +```js +let api; + +``` + +- Declares: `api` +- Write: `api` + +## Item 5: Stmt 2, `Normal` + +```js +if (process.env.NEXT_RUNTIME === 'edge') { + api = require('@opentelemetry/api'); +} else { + try { + api = require('@opentelemetry/api'); + } catch (err) { + api = require('next/dist/compiled/@opentelemetry/api'); + } +} + +``` + +- Side effects +- Write: `api` + +## Item 6: Stmt 3, `VarDeclarator(0)` + +```js +const { context, propagation, trace, SpanStatusCode, SpanKind, ROOT_CONTEXT } = api; + +``` + +- Declares: `context`, `propagation`, `trace`, `SpanStatusCode`, `SpanKind`, `ROOT_CONTEXT` +- Reads: `api` +- Write: `context`, `propagation`, `trace`, `SpanStatusCode`, `SpanKind`, `ROOT_CONTEXT` + +## Item 7: Stmt 4, `VarDeclarator(0)` + +```js +const isPromise = (p)=>{ + return p !== null && typeof p === 'object' && typeof p.then === 'function'; +}; + +``` + +- Declares: `isPromise` +- Write: `isPromise` + +## Item 8: Stmt 5, `Normal` + +```js +export class BubbledError extends Error { + constructor(bubble, result){ + super(); + this.bubble = bubble; + this.result = result; + } +} + +``` + +- Declares: `BubbledError` +- Write: `BubbledError` + +## Item 9: Stmt 6, `Normal` + +```js +export function isBubbledError(error) { + if (typeof error !== 'object' || error === null) return false; + return error instanceof BubbledError; +} + +``` + +- Hoisted +- Declares: `isBubbledError` +- Reads (eventual): `BubbledError` +- Write: `isBubbledError` + +## Item 10: Stmt 7, `VarDeclarator(0)` + +```js +const closeSpanWithError = (span, error)=>{ + if (isBubbledError(error) && error.bubble) { + span.setAttribute('next.bubble', true); + } else { + if (error) { + span.recordException(error); + } + span.setStatus({ + code: SpanStatusCode.ERROR, + message: error == null ? void 0 : error.message + }); + } + span.end(); +}; + +``` + +- Declares: `closeSpanWithError` +- Reads: `isBubbledError`, `SpanStatusCode` +- Write: `closeSpanWithError`, `SpanStatusCode` + +## Item 11: Stmt 8, `VarDeclarator(0)` + +```js +const rootSpanAttributesStore = new Map(); + +``` + +- Side effects +- Declares: `rootSpanAttributesStore` +- Write: `rootSpanAttributesStore` + +## Item 12: Stmt 9, `VarDeclarator(0)` + +```js +const rootSpanIdKey = api.createContextKey('next.rootSpanId'); + +``` + +- Declares: `rootSpanIdKey` +- Reads: `api` +- Write: `rootSpanIdKey`, `api` + +## Item 13: Stmt 10, `VarDeclarator(0)` + +```js +let lastSpanId = 0; + +``` + +- Declares: `lastSpanId` +- Write: `lastSpanId` + +## Item 14: Stmt 11, `VarDeclarator(0)` + +```js +const getSpanId = ()=>lastSpanId++; + +``` + +- Declares: `getSpanId` +- Reads: `lastSpanId` +- Write: `getSpanId` + +## Item 15: Stmt 12, `VarDeclarator(0)` + +```js +const clientTraceDataSetter = { + set (carrier, key, value) { + carrier.push({ + key, + value + }); + } +}; + +``` + +- Declares: `clientTraceDataSetter` +- Write: `clientTraceDataSetter` + +## Item 16: Stmt 13, `Normal` + +```js +class NextTracerImpl { + getTracerInstance() { + return trace.getTracer('next.js', '0.0.1'); + } + getContext() { + return context; + } + getTracePropagationData() { + const activeContext = context.active(); + const entries = []; + propagation.inject(activeContext, entries, clientTraceDataSetter); + return entries; + } + getActiveScopeSpan() { + return trace.getSpan(context == null ? void 0 : context.active()); + } + withPropagatedContext(carrier, fn, getter) { + const activeContext = context.active(); + if (trace.getSpanContext(activeContext)) { + return fn(); + } + const remoteContext = propagation.extract(activeContext, carrier, getter); + return context.with(remoteContext, fn); + } + trace(...args) { + var _trace_getSpanContext; + const [type, fnOrOptions, fnOrEmpty] = args; + const { fn, options } = typeof fnOrOptions === 'function' ? { + fn: fnOrOptions, + options: {} + } : { + fn: fnOrEmpty, + options: { + ...fnOrOptions + } + }; + const spanName = options.spanName ?? type; + if (!NextVanillaSpanAllowlist.includes(type) && process.env.NEXT_OTEL_VERBOSE !== '1' || options.hideSpan) { + return fn(); + } + let spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan()); + let isRootSpan = false; + if (!spanContext) { + spanContext = (context == null ? void 0 : context.active()) ?? ROOT_CONTEXT; + isRootSpan = true; + } else if ((_trace_getSpanContext = trace.getSpanContext(spanContext)) == null ? void 0 : _trace_getSpanContext.isRemote) { + isRootSpan = true; + } + const spanId = getSpanId(); + options.attributes = { + 'next.span_name': spanName, + 'next.span_type': type, + ...options.attributes + }; + return context.with(spanContext.setValue(rootSpanIdKey, spanId), ()=>this.getTracerInstance().startActiveSpan(spanName, options, (span)=>{ + const startTime = 'performance' in globalThis && 'measure' in performance ? globalThis.performance.now() : undefined; + const onCleanup = ()=>{ + rootSpanAttributesStore.delete(spanId); + if (startTime && process.env.NEXT_OTEL_PERFORMANCE_PREFIX && LogSpanAllowList.includes(type || '')) { + performance.measure(`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-${(type.split('.').pop() || '').replace(/[A-Z]/g, (match)=>'-' + match.toLowerCase())}`, { + start: startTime, + end: performance.now() + }); + } + }; + if (isRootSpan) { + rootSpanAttributesStore.set(spanId, new Map(Object.entries(options.attributes ?? {}))); + } + try { + if (fn.length > 1) { + return fn(span, (err)=>closeSpanWithError(span, err)); + } + const result = fn(span); + if (isPromise(result)) { + return result.then((res)=>{ + span.end(); + return res; + }).catch((err)=>{ + closeSpanWithError(span, err); + throw err; + }).finally(onCleanup); + } else { + span.end(); + onCleanup(); + } + return result; + } catch (err) { + closeSpanWithError(span, err); + onCleanup(); + throw err; + } + })); + } + wrap(...args) { + const tracer = this; + const [name, options, fn] = args.length === 3 ? args : [ + args[0], + {}, + args[1] + ]; + if (!NextVanillaSpanAllowlist.includes(name) && process.env.NEXT_OTEL_VERBOSE !== '1') { + return fn; + } + return function() { + let optionsObj = options; + if (typeof optionsObj === 'function' && typeof fn === 'function') { + optionsObj = optionsObj.apply(this, arguments); + } + const lastArgId = arguments.length - 1; + const cb = arguments[lastArgId]; + if (typeof cb === 'function') { + const scopeBoundCb = tracer.getContext().bind(context.active(), cb); + return tracer.trace(name, optionsObj, (_span, done)=>{ + arguments[lastArgId] = function(err) { + done == null ? void 0 : done(err); + return scopeBoundCb.apply(this, arguments); + }; + return fn.apply(this, arguments); + }); + } else { + return tracer.trace(name, optionsObj, ()=>fn.apply(this, arguments)); + } + }; + } + startSpan(...args) { + const [type, options] = args; + const spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan()); + return this.getTracerInstance().startSpan(type, options, spanContext); + } + getSpanContext(parentSpan) { + const spanContext = parentSpan ? trace.setSpan(context.active(), parentSpan) : undefined; + return spanContext; + } + getRootSpanAttributes() { + const spanId = context.active().getValue(rootSpanIdKey); + return rootSpanAttributesStore.get(spanId); + } +} + +``` + +- Declares: `NextTracerImpl` +- Reads: `trace`, `context`, `propagation`, `clientTraceDataSetter`, `NextVanillaSpanAllowlist`, `ROOT_CONTEXT`, `getSpanId`, `rootSpanIdKey`, `rootSpanAttributesStore`, `LogSpanAllowList`, `closeSpanWithError`, `isPromise` +- Write: `trace`, `context`, `propagation`, `NextVanillaSpanAllowlist`, `rootSpanAttributesStore`, `LogSpanAllowList`, `NextTracerImpl` + +## Item 17: Stmt 14, `VarDeclarator(0)` + +```js +const getTracer = (()=>{ + const tracer = new NextTracerImpl(); + return ()=>tracer; +})(); + +``` + +- Declares: `getTracer` +- Reads: `NextTracerImpl` +- Write: `getTracer` + +# Phase 1 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item14; + Item15; + Item16; + Item17; + Item18; + Item18["ModuleEvaluation"]; + Item19; + Item19["export BubbledError"]; + Item20; + Item20["export isBubbledError"]; + Item21; + Item21["export getTracer"]; + Item22; + Item22["export SpanStatusCode"]; + Item23; + Item23["export SpanKind"]; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item14; + Item15; + Item16; + Item17; + Item18; + Item18["ModuleEvaluation"]; + Item19; + Item19["export BubbledError"]; + Item20; + Item20["export isBubbledError"]; + Item21; + Item21["export getTracer"]; + Item22; + Item22["export SpanStatusCode"]; + Item23; + Item23["export SpanKind"]; + Item5 --> Item4; + Item5 --> Item1; + Item6 --> Item5; + Item6 --> Item4; + Item10 --> Item9; + Item10 --> Item6; + Item11 --> Item1; + Item11 --> Item5; + Item11 -.-> Item8; + Item12 --> Item5; + Item12 --> Item4; + Item12 -.-> Item6; + Item14 --> Item13; + Item16 --> Item6; + Item16 --> Item15; + Item16 --> Item3; + Item16 --> Item14; + Item16 --> Item12; + Item16 --> Item11; + Item16 --> Item2; + Item16 --> Item10; + Item16 --> Item7; + Item17 --> Item16; + Item19 --> Item8; + Item20 --> Item9; + Item21 --> Item17; + Item22 --> Item10; + Item22 --> Item6; + Item23 --> Item6; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item14; + Item15; + Item16; + Item17; + Item18; + Item18["ModuleEvaluation"]; + Item19; + Item19["export BubbledError"]; + Item20; + Item20["export isBubbledError"]; + Item21; + Item21["export getTracer"]; + Item22; + Item22["export SpanStatusCode"]; + Item23; + Item23["export SpanKind"]; + Item5 --> Item4; + Item5 --> Item1; + Item6 --> Item5; + Item6 --> Item4; + Item10 --> Item9; + Item10 --> Item6; + Item11 --> Item1; + Item11 --> Item5; + Item11 -.-> Item8; + Item12 --> Item5; + Item12 --> Item4; + Item12 -.-> Item6; + Item14 --> Item13; + Item16 --> Item6; + Item16 --> Item15; + Item16 --> Item3; + Item16 --> Item14; + Item16 --> Item12; + Item16 --> Item11; + Item16 --> Item2; + Item16 --> Item10; + Item16 --> Item7; + Item17 --> Item16; + Item19 --> Item8; + Item20 --> Item9; + Item21 --> Item17; + Item22 --> Item10; + Item22 --> Item6; + Item23 --> Item6; + Item9 --> Item8; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item7; + Item8; + Item9; + Item10; + Item11; + Item12; + Item13; + Item14; + Item15; + Item16; + Item17; + Item18; + Item18["ModuleEvaluation"]; + Item19; + Item19["export BubbledError"]; + Item20; + Item20["export isBubbledError"]; + Item21; + Item21["export getTracer"]; + Item22; + Item22["export SpanStatusCode"]; + Item23; + Item23["export SpanKind"]; + Item5 --> Item4; + Item5 --> Item1; + Item6 --> Item5; + Item6 --> Item4; + Item10 --> Item9; + Item10 --> Item6; + Item11 --> Item1; + Item11 --> Item5; + Item11 -.-> Item8; + Item12 --> Item5; + Item12 --> Item4; + Item12 -.-> Item6; + Item14 --> Item13; + Item16 --> Item6; + Item16 --> Item15; + Item16 --> Item3; + Item16 --> Item14; + Item16 --> Item12; + Item16 --> Item11; + Item16 --> Item2; + Item16 --> Item10; + Item16 --> Item7; + Item17 --> Item16; + Item19 --> Item8; + Item20 --> Item9; + Item21 --> Item17; + Item22 --> Item10; + Item22 --> Item6; + Item23 --> Item6; + Item9 --> Item8; + Item18 --> Item1; + Item18 --> Item5; + Item18 --> Item11; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation)]"]; + N1["Items: [ItemId(Export(("BubbledError", #2), "BubbledError"))]"]; + N2["Items: [ItemId(Export(("isBubbledError", #2), "isBubbledError"))]"]; + N3["Items: [ItemId(Export(("getTracer", #2), "getTracer")), ItemId(0, ImportBinding(0)), ItemId(0, ImportBinding(1)), ItemId(4, VarDeclarator(0)), ItemId(9, VarDeclarator(0)), ItemId(10, VarDeclarator(0)), ItemId(11, VarDeclarator(0)), ItemId(12, VarDeclarator(0)), ItemId(13, Normal), ItemId(14, VarDeclarator(0))]"]; + N4["Items: [ItemId(Export(("SpanStatusCode", #2), "SpanStatusCode"))]"]; + N5["Items: [ItemId(Export(("SpanKind", #2), "SpanKind"))]"]; + N6["Items: [ItemId(0, ImportOfModule)]"]; + N7["Items: [ItemId(1, VarDeclarator(0))]"]; + N8["Items: [ItemId(2, Normal)]"]; + N9["Items: [ItemId(3, VarDeclarator(0))]"]; + N10["Items: [ItemId(5, Normal)]"]; + N11["Items: [ItemId(6, Normal)]"]; + N12["Items: [ItemId(7, VarDeclarator(0))]"]; + N13["Items: [ItemId(8, VarDeclarator(0))]"]; + N0 --> N6; + N0 --> N8; + N0 --> N13; + N1 --> N10; + N2 --> N11; + N3 --> N8; + N3 --> N7; + N3 --> N9; + N3 --> N13; + N3 --> N12; + N4 --> N12; + N4 --> N9; + N5 --> N9; + N8 --> N7; + N8 --> N6; + N9 --> N8; + N9 --> N7; + N11 --> N10; + N12 --> N11; + N12 --> N9; + N13 --> N6; + N13 --> N8; + N13 --> N10; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "isBubbledError", + ): 2, + Export( + "SpanKind", + ): 5, + Exports: 14, + Export( + "SpanStatusCode", + ): 4, + Export( + "BubbledError", + ): 1, + Export( + "getTracer", + ): 3, +} +``` + + +# Modules (dev) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { BubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +export { BubbledError }; + +``` +## Part 2 +```js +import { isBubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +export { isBubbledError }; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { api } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import { context, propagation, trace, ROOT_CONTEXT } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +import { rootSpanAttributesStore } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +import { closeSpanWithError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +export { getTracer as getTracer }; +import { LogSpanAllowList } from './constants'; +import { NextVanillaSpanAllowlist } from './constants'; +const isPromise = (p)=>{ + return p !== null && typeof p === 'object' && typeof p.then === 'function'; +}; +const rootSpanIdKey = api.createContextKey('next.rootSpanId'); +let lastSpanId = 0; +const getSpanId = ()=>lastSpanId++; +const clientTraceDataSetter = { + set (carrier, key, value) { + carrier.push({ + key, + value + }); + } +}; +class NextTracerImpl { + getTracerInstance() { + return trace.getTracer('next.js', '0.0.1'); + } + getContext() { + return context; + } + getTracePropagationData() { + const activeContext = context.active(); + const entries = []; + propagation.inject(activeContext, entries, clientTraceDataSetter); + return entries; + } + getActiveScopeSpan() { + return trace.getSpan(context == null ? void 0 : context.active()); + } + withPropagatedContext(carrier, fn, getter) { + const activeContext = context.active(); + if (trace.getSpanContext(activeContext)) { + return fn(); + } + const remoteContext = propagation.extract(activeContext, carrier, getter); + return context.with(remoteContext, fn); + } + trace(...args) { + var _trace_getSpanContext; + const [type, fnOrOptions, fnOrEmpty] = args; + const { fn, options } = typeof fnOrOptions === 'function' ? { + fn: fnOrOptions, + options: {} + } : { + fn: fnOrEmpty, + options: { + ...fnOrOptions + } + }; + const spanName = options.spanName ?? type; + if (!NextVanillaSpanAllowlist.includes(type) && process.env.NEXT_OTEL_VERBOSE !== '1' || options.hideSpan) { + return fn(); + } + let spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan()); + let isRootSpan = false; + if (!spanContext) { + spanContext = (context == null ? void 0 : context.active()) ?? ROOT_CONTEXT; + isRootSpan = true; + } else if ((_trace_getSpanContext = trace.getSpanContext(spanContext)) == null ? void 0 : _trace_getSpanContext.isRemote) { + isRootSpan = true; + } + const spanId = getSpanId(); + options.attributes = { + 'next.span_name': spanName, + 'next.span_type': type, + ...options.attributes + }; + return context.with(spanContext.setValue(rootSpanIdKey, spanId), ()=>this.getTracerInstance().startActiveSpan(spanName, options, (span)=>{ + const startTime = 'performance' in globalThis && 'measure' in performance ? globalThis.performance.now() : undefined; + const onCleanup = ()=>{ + rootSpanAttributesStore.delete(spanId); + if (startTime && process.env.NEXT_OTEL_PERFORMANCE_PREFIX && LogSpanAllowList.includes(type || '')) { + performance.measure(`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-${(type.split('.').pop() || '').replace(/[A-Z]/g, (match)=>'-' + match.toLowerCase())}`, { + start: startTime, + end: performance.now() + }); + } + }; + if (isRootSpan) { + rootSpanAttributesStore.set(spanId, new Map(Object.entries(options.attributes ?? {}))); + } + try { + if (fn.length > 1) { + return fn(span, (err)=>closeSpanWithError(span, err)); + } + const result = fn(span); + if (isPromise(result)) { + return result.then((res)=>{ + span.end(); + return res; + }).catch((err)=>{ + closeSpanWithError(span, err); + throw err; + }).finally(onCleanup); + } else { + span.end(); + onCleanup(); + } + return result; + } catch (err) { + closeSpanWithError(span, err); + onCleanup(); + throw err; + } + })); + } + wrap(...args) { + const tracer = this; + const [name, options, fn] = args.length === 3 ? args : [ + args[0], + {}, + args[1] + ]; + if (!NextVanillaSpanAllowlist.includes(name) && process.env.NEXT_OTEL_VERBOSE !== '1') { + return fn; + } + return function() { + let optionsObj = options; + if (typeof optionsObj === 'function' && typeof fn === 'function') { + optionsObj = optionsObj.apply(this, arguments); + } + const lastArgId = arguments.length - 1; + const cb = arguments[lastArgId]; + if (typeof cb === 'function') { + const scopeBoundCb = tracer.getContext().bind(context.active(), cb); + return tracer.trace(name, optionsObj, (_span, done)=>{ + arguments[lastArgId] = function(err) { + done == null ? void 0 : done(err); + return scopeBoundCb.apply(this, arguments); + }; + return fn.apply(this, arguments); + }); + } else { + return tracer.trace(name, optionsObj, ()=>fn.apply(this, arguments)); + } + }; + } + startSpan(...args) { + const [type, options] = args; + const spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan()); + return this.getTracerInstance().startSpan(type, options, spanContext); + } + getSpanContext(parentSpan) { + const spanContext = parentSpan ? trace.setSpan(context.active(), parentSpan) : undefined; + return spanContext; + } + getRootSpanAttributes() { + const spanId = context.active().getValue(rootSpanIdKey); + return rootSpanAttributesStore.get(spanId); + } +} +const getTracer = (()=>{ + const tracer = new NextTracerImpl(); + return ()=>tracer; +})(); +export { LogSpanAllowList } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextVanillaSpanAllowlist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { isPromise } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { rootSpanIdKey } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { lastSpanId } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getSpanId } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { clientTraceDataSetter } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextTracerImpl } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getTracer } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 4 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +import { SpanStatusCode } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +export { SpanStatusCode as SpanStatusCode }; + +``` +## Part 5 +```js +import { SpanKind } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +export { SpanKind as SpanKind }; + +``` +## Part 6 +```js +import './constants'; + +``` +## Part 7 +```js +let api; +export { api } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 8 +```js +import { api } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +if (process.env.NEXT_RUNTIME === 'edge') { + api = require('@opentelemetry/api'); +} else { + try { + api = require('@opentelemetry/api'); + } catch (err) { + api = require('next/dist/compiled/@opentelemetry/api'); + } +} + +``` +## Part 9 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { api } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +const { context, propagation, trace, SpanStatusCode, SpanKind, ROOT_CONTEXT } = api; +export { context } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { propagation } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { trace } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { SpanStatusCode } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { SpanKind } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { ROOT_CONTEXT } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +class BubbledError extends Error { + constructor(bubble, result){ + super(); + this.bubble = bubble; + this.result = result; + } +} +export { BubbledError } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 11 +```js +import { BubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +function isBubbledError(error) { + if (typeof error !== 'object' || error === null) return false; + return error instanceof BubbledError; +} +export { isBubbledError } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 12 +```js +import { isBubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +import { SpanStatusCode } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +const closeSpanWithError = (span, error)=>{ + if (isBubbledError(error) && error.bubble) { + span.setAttribute('next.bubble', true); + } else { + if (error) { + span.recordException(error); + } + span.setStatus({ + code: SpanStatusCode.ERROR, + message: error == null ? void 0 : error.message + }); + } + span.end(); +}; +export { closeSpanWithError } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 13 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +const rootSpanAttributesStore = new Map(); +export { rootSpanAttributesStore } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 14 +```js +export { BubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export BubbledError" +}; +export { isBubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export isBubbledError" +}; +export { getTracer } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getTracer" +}; +export { SpanStatusCode } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export SpanStatusCode" +}; +export { SpanKind } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export SpanKind" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "isBubbledError", + ): 2, + Export( + "SpanKind", + ): 5, + Exports: 14, + Export( + "SpanStatusCode", + ): 4, + Export( + "BubbledError", + ): 1, + Export( + "getTracer", + ): 3, +} +``` + + +# Modules (prod) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { BubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +export { BubbledError }; + +``` +## Part 2 +```js +import { isBubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +export { isBubbledError }; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { api } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import { context, propagation, trace, ROOT_CONTEXT } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +import { rootSpanAttributesStore } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +import { closeSpanWithError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +export { getTracer as getTracer }; +import { LogSpanAllowList } from './constants'; +import { NextVanillaSpanAllowlist } from './constants'; +const isPromise = (p)=>{ + return p !== null && typeof p === 'object' && typeof p.then === 'function'; +}; +const rootSpanIdKey = api.createContextKey('next.rootSpanId'); +let lastSpanId = 0; +const getSpanId = ()=>lastSpanId++; +const clientTraceDataSetter = { + set (carrier, key, value) { + carrier.push({ + key, + value + }); + } +}; +class NextTracerImpl { + getTracerInstance() { + return trace.getTracer('next.js', '0.0.1'); + } + getContext() { + return context; + } + getTracePropagationData() { + const activeContext = context.active(); + const entries = []; + propagation.inject(activeContext, entries, clientTraceDataSetter); + return entries; + } + getActiveScopeSpan() { + return trace.getSpan(context == null ? void 0 : context.active()); + } + withPropagatedContext(carrier, fn, getter) { + const activeContext = context.active(); + if (trace.getSpanContext(activeContext)) { + return fn(); + } + const remoteContext = propagation.extract(activeContext, carrier, getter); + return context.with(remoteContext, fn); + } + trace(...args) { + var _trace_getSpanContext; + const [type, fnOrOptions, fnOrEmpty] = args; + const { fn, options } = typeof fnOrOptions === 'function' ? { + fn: fnOrOptions, + options: {} + } : { + fn: fnOrEmpty, + options: { + ...fnOrOptions + } + }; + const spanName = options.spanName ?? type; + if (!NextVanillaSpanAllowlist.includes(type) && process.env.NEXT_OTEL_VERBOSE !== '1' || options.hideSpan) { + return fn(); + } + let spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan()); + let isRootSpan = false; + if (!spanContext) { + spanContext = (context == null ? void 0 : context.active()) ?? ROOT_CONTEXT; + isRootSpan = true; + } else if ((_trace_getSpanContext = trace.getSpanContext(spanContext)) == null ? void 0 : _trace_getSpanContext.isRemote) { + isRootSpan = true; + } + const spanId = getSpanId(); + options.attributes = { + 'next.span_name': spanName, + 'next.span_type': type, + ...options.attributes + }; + return context.with(spanContext.setValue(rootSpanIdKey, spanId), ()=>this.getTracerInstance().startActiveSpan(spanName, options, (span)=>{ + const startTime = 'performance' in globalThis && 'measure' in performance ? globalThis.performance.now() : undefined; + const onCleanup = ()=>{ + rootSpanAttributesStore.delete(spanId); + if (startTime && process.env.NEXT_OTEL_PERFORMANCE_PREFIX && LogSpanAllowList.includes(type || '')) { + performance.measure(`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-${(type.split('.').pop() || '').replace(/[A-Z]/g, (match)=>'-' + match.toLowerCase())}`, { + start: startTime, + end: performance.now() + }); + } + }; + if (isRootSpan) { + rootSpanAttributesStore.set(spanId, new Map(Object.entries(options.attributes ?? {}))); + } + try { + if (fn.length > 1) { + return fn(span, (err)=>closeSpanWithError(span, err)); + } + const result = fn(span); + if (isPromise(result)) { + return result.then((res)=>{ + span.end(); + return res; + }).catch((err)=>{ + closeSpanWithError(span, err); + throw err; + }).finally(onCleanup); + } else { + span.end(); + onCleanup(); + } + return result; + } catch (err) { + closeSpanWithError(span, err); + onCleanup(); + throw err; + } + })); + } + wrap(...args) { + const tracer = this; + const [name, options, fn] = args.length === 3 ? args : [ + args[0], + {}, + args[1] + ]; + if (!NextVanillaSpanAllowlist.includes(name) && process.env.NEXT_OTEL_VERBOSE !== '1') { + return fn; + } + return function() { + let optionsObj = options; + if (typeof optionsObj === 'function' && typeof fn === 'function') { + optionsObj = optionsObj.apply(this, arguments); + } + const lastArgId = arguments.length - 1; + const cb = arguments[lastArgId]; + if (typeof cb === 'function') { + const scopeBoundCb = tracer.getContext().bind(context.active(), cb); + return tracer.trace(name, optionsObj, (_span, done)=>{ + arguments[lastArgId] = function(err) { + done == null ? void 0 : done(err); + return scopeBoundCb.apply(this, arguments); + }; + return fn.apply(this, arguments); + }); + } else { + return tracer.trace(name, optionsObj, ()=>fn.apply(this, arguments)); + } + }; + } + startSpan(...args) { + const [type, options] = args; + const spanContext = this.getSpanContext((options == null ? void 0 : options.parentSpan) ?? this.getActiveScopeSpan()); + return this.getTracerInstance().startSpan(type, options, spanContext); + } + getSpanContext(parentSpan) { + const spanContext = parentSpan ? trace.setSpan(context.active(), parentSpan) : undefined; + return spanContext; + } + getRootSpanAttributes() { + const spanId = context.active().getValue(rootSpanIdKey); + return rootSpanAttributesStore.get(spanId); + } +} +const getTracer = (()=>{ + const tracer = new NextTracerImpl(); + return ()=>tracer; +})(); +export { LogSpanAllowList } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextVanillaSpanAllowlist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { isPromise } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { rootSpanIdKey } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { lastSpanId } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getSpanId } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { clientTraceDataSetter } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { NextTracerImpl } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getTracer } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 4 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 12 +}; +import { SpanStatusCode } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +export { SpanStatusCode as SpanStatusCode }; + +``` +## Part 5 +```js +import { SpanKind } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +export { SpanKind as SpanKind }; + +``` +## Part 6 +```js +import './constants'; + +``` +## Part 7 +```js +let api; +export { api } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 8 +```js +import { api } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +if (process.env.NEXT_RUNTIME === 'edge') { + api = require('@opentelemetry/api'); +} else { + try { + api = require('@opentelemetry/api'); + } catch (err) { + api = require('next/dist/compiled/@opentelemetry/api'); + } +} + +``` +## Part 9 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import { api } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +const { context, propagation, trace, SpanStatusCode, SpanKind, ROOT_CONTEXT } = api; +export { context } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { propagation } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { trace } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { SpanStatusCode } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { SpanKind } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { ROOT_CONTEXT } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +class BubbledError extends Error { + constructor(bubble, result){ + super(); + this.bubble = bubble; + this.result = result; + } +} +export { BubbledError } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 11 +```js +import { BubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; +function isBubbledError(error) { + if (typeof error !== 'object' || error === null) return false; + return error instanceof BubbledError; +} +export { isBubbledError } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 12 +```js +import { isBubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 11 +}; +import { SpanStatusCode } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +const closeSpanWithError = (span, error)=>{ + if (isBubbledError(error) && error.bubble) { + span.setAttribute('next.bubble', true); + } else { + if (error) { + span.recordException(error); + } + span.setStatus({ + code: SpanStatusCode.ERROR, + message: error == null ? void 0 : error.message + }); + } + span.end(); +}; +export { closeSpanWithError } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 13 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +const rootSpanAttributesStore = new Map(); +export { rootSpanAttributesStore } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 14 +```js +export { BubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export BubbledError" +}; +export { isBubbledError } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export isBubbledError" +}; +export { getTracer } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getTracer" +}; +export { SpanStatusCode } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export SpanStatusCode" +}; +export { SpanKind } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export SpanKind" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 13 +}; +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-fetch/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-fetch/input.js new file mode 100644 index 0000000000000..dbdeaab4b2c2a --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-fetch/input.js @@ -0,0 +1,9 @@ +import Stream from 'node:stream'; + +const streamDestructionSupported = 'destroy' in Stream.Readable.prototype; + +function fetch(){ + +} + +export default fetch; diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-fetch/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-fetch/output.md new file mode 100644 index 0000000000000..e5e48962003c7 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-fetch/output.md @@ -0,0 +1,278 @@ +# Items + +Count: 7 + +## Item 1: Stmt 0, `ImportOfModule` + +```js +import Stream from 'node:stream'; + +``` + +- Hoisted +- Side effects + +## Item 2: Stmt 0, `ImportBinding(0)` + +```js +import Stream from 'node:stream'; + +``` + +- Hoisted +- Declares: `Stream` + +## Item 3: Stmt 1, `VarDeclarator(0)` + +```js +const streamDestructionSupported = 'destroy' in Stream.Readable.prototype; + +``` + +- Declares: `streamDestructionSupported` +- Reads: `Stream` +- Write: `streamDestructionSupported`, `Stream` + +## Item 4: Stmt 2, `Normal` + +```js +function fetch() {} + +``` + +- Hoisted +- Declares: `fetch` +- Write: `fetch` + +## Item 5: Stmt 3, `Normal` + +```js +export default fetch; + +``` + +- Side effects +- Declares: `__TURBOPACK__default__export__` +- Reads: `fetch` +- Write: `__TURBOPACK__default__export__` + +# Phase 1 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export default"]; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export default"]; + Item3 --> Item2; + Item5 --> Item4; + Item5 --> Item1; + Item7 --> Item5; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export default"]; + Item3 --> Item2; + Item5 --> Item4; + Item5 --> Item1; + Item7 --> Item5; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item6; + Item6["ModuleEvaluation"]; + Item7; + Item7["export default"]; + Item3 --> Item2; + Item5 --> Item4; + Item5 --> Item1; + Item7 --> Item5; + Item6 --> Item1; + Item6 --> Item5; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation)]"]; + N1["Items: [ItemId(Export(("__TURBOPACK__default__export__", #4), "default"))]"]; + N2["Items: [ItemId(0, ImportOfModule)]"]; + N3["Items: [ItemId(2, Normal), ItemId(3, Normal)]"]; + N0 --> N2; + N0 --> N3; + N1 --> N3; + N3 --> N2; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "default", + ): 1, + Exports: 4, +} +``` + + +# Modules (dev) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { __TURBOPACK__default__export__ } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +export { __TURBOPACK__default__export__ as default }; + +``` +## Part 2 +```js +import 'node:stream'; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +function fetch() {} +const __TURBOPACK__default__export__ = fetch; +export { fetch } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { __TURBOPACK__default__export__ } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 4 +```js +export { default } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export default" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Export( + "default", + ): 1, + Exports: 4, +} +``` + + +# Modules (prod) +## Part 0 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +"module evaluation"; + +``` +## Part 1 +```js +import { __TURBOPACK__default__export__ } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +export { __TURBOPACK__default__export__ as default }; + +``` +## Part 2 +```js +import 'node:stream'; + +``` +## Part 3 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +function fetch() {} +const __TURBOPACK__default__export__ = fetch; +export { fetch } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { __TURBOPACK__default__export__ } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 4 +```js +export { default } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export default" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-globals/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-globals/output.md index 77cd4a23f2131..6cc6ecc74a347 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-globals/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/node-globals/output.md @@ -50,6 +50,7 @@ graph TD ``` { ModuleEvaluation: 0, + Exports: 1, } ``` @@ -60,6 +61,10 @@ graph TD "module evaluation"; process.turbopack = {}; +``` +## Part 1 +```js + ``` ## Merged (module eval) ```js @@ -72,6 +77,7 @@ process.turbopack = {}; ``` { ModuleEvaluation: 0, + Exports: 1, } ``` @@ -82,6 +88,10 @@ process.turbopack = {}; "module evaluation"; process.turbopack = {}; +``` +## Part 1 +```js + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/otel-core/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/otel-core/input.js new file mode 100644 index 0000000000000..0d99a1926bc63 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/otel-core/input.js @@ -0,0 +1,27 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { DEFAULT_ENVIRONMENT, parseEnvironment, } from '../../utils/environment'; +import { _globalThis } from './globalThis'; +/** + * Gets the environment variables + */ +export function getEnv() { + var globalEnv = parseEnvironment(_globalThis); + return Object.assign({}, DEFAULT_ENVIRONMENT, globalEnv); +} +export function getEnvWithoutDefaults() { + return parseEnvironment(_globalThis); +} diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/otel-core/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/otel-core/output.md new file mode 100644 index 0000000000000..0ff6ef1b2b064 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/otel-core/output.md @@ -0,0 +1,355 @@ +# Items + +Count: 10 + +## Item 1: Stmt 0, `ImportOfModule` + +```js +import { DEFAULT_ENVIRONMENT, parseEnvironment } from '../../utils/environment'; + +``` + +- Hoisted +- Side effects + +## Item 2: Stmt 0, `ImportBinding(0)` + +```js +import { DEFAULT_ENVIRONMENT, parseEnvironment } from '../../utils/environment'; + +``` + +- Hoisted +- Declares: `DEFAULT_ENVIRONMENT` + +## Item 3: Stmt 0, `ImportBinding(1)` + +```js +import { DEFAULT_ENVIRONMENT, parseEnvironment } from '../../utils/environment'; + +``` + +- Hoisted +- Declares: `parseEnvironment` + +## Item 4: Stmt 1, `ImportOfModule` + +```js +import { _globalThis } from './globalThis'; + +``` + +- Hoisted +- Side effects + +## Item 5: Stmt 1, `ImportBinding(0)` + +```js +import { _globalThis } from './globalThis'; + +``` + +- Hoisted +- Declares: `_globalThis` + +## Item 6: Stmt 2, `Normal` + +```js +export function getEnv() { + var globalEnv = parseEnvironment(_globalThis); + return Object.assign({}, DEFAULT_ENVIRONMENT, globalEnv); +} + +``` + +- Hoisted +- Declares: `getEnv` +- Reads (eventual): `parseEnvironment`, `_globalThis`, `DEFAULT_ENVIRONMENT` +- Write: `getEnv` + +## Item 7: Stmt 3, `Normal` + +```js +export function getEnvWithoutDefaults() { + return parseEnvironment(_globalThis); +} + +``` + +- Hoisted +- Declares: `getEnvWithoutDefaults` +- Reads (eventual): `parseEnvironment`, `_globalThis` +- Write: `getEnvWithoutDefaults` + +# Phase 1 +```mermaid +graph TD + Item1; + Item3; + Item4; + Item2; + Item5; + Item6; + Item7; + Item8; + Item8["ModuleEvaluation"]; + Item9; + Item9["export getEnv"]; + Item10; + Item10["export getEnvWithoutDefaults"]; + Item2 --> Item1; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item3; + Item4; + Item2; + Item5; + Item6; + Item7; + Item8; + Item8["ModuleEvaluation"]; + Item9; + Item9["export getEnv"]; + Item10; + Item10["export getEnvWithoutDefaults"]; + Item2 --> Item1; + Item9 --> Item6; + Item10 --> Item7; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item3; + Item4; + Item2; + Item5; + Item6; + Item7; + Item8; + Item8["ModuleEvaluation"]; + Item9; + Item9["export getEnv"]; + Item10; + Item10["export getEnvWithoutDefaults"]; + Item2 --> Item1; + Item9 --> Item6; + Item10 --> Item7; + Item6 --> Item4; + Item6 --> Item5; + Item6 --> Item3; + Item7 --> Item4; + Item7 --> Item5; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item3; + Item4; + Item2; + Item5; + Item6; + Item7; + Item8; + Item8["ModuleEvaluation"]; + Item9; + Item9["export getEnv"]; + Item10; + Item10["export getEnvWithoutDefaults"]; + Item2 --> Item1; + Item9 --> Item6; + Item10 --> Item7; + Item6 --> Item4; + Item6 --> Item5; + Item6 --> Item3; + Item7 --> Item4; + Item7 --> Item5; + Item8 --> Item1; + Item8 --> Item2; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation), ItemId(0, ImportOfModule), ItemId(1, ImportOfModule)]"]; + N1["Items: [ItemId(Export(("getEnv", #2), "getEnv")), ItemId(0, ImportBinding(0)), ItemId(0, ImportBinding(1)), ItemId(1, ImportBinding(0)), ItemId(2, Normal)]"]; + N2["Items: [ItemId(Export(("getEnvWithoutDefaults", #2), "getEnvWithoutDefaults")), ItemId(0, ImportBinding(1)), ItemId(1, ImportBinding(0)), ItemId(3, Normal)]"]; + N1 --> N2; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Exports: 3, + Export( + "getEnv", + ): 1, + Export( + "getEnvWithoutDefaults", + ): 2, +} +``` + + +# Modules (dev) +## Part 0 +```js +"module evaluation"; +import '../../utils/environment'; +import './globalThis'; + +``` +## Part 1 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +export { getEnv }; +import { DEFAULT_ENVIRONMENT } from '../../utils/environment'; +import { parseEnvironment } from '../../utils/environment'; +import { _globalThis } from './globalThis'; +function getEnv() { + var globalEnv = parseEnvironment(_globalThis); + return Object.assign({}, DEFAULT_ENVIRONMENT, globalEnv); +} +export { DEFAULT_ENVIRONMENT } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { parseEnvironment } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { _globalThis } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getEnv } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +export { getEnvWithoutDefaults }; +import { parseEnvironment } from '../../utils/environment'; +import { _globalThis } from './globalThis'; +function getEnvWithoutDefaults() { + return parseEnvironment(_globalThis); +} +export { parseEnvironment } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { _globalThis } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getEnvWithoutDefaults } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +export { getEnv } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getEnv" +}; +export { getEnvWithoutDefaults } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getEnvWithoutDefaults" +}; + +``` +## Merged (module eval) +```js +import '../../utils/environment'; +import './globalThis'; +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Exports: 3, + Export( + "getEnv", + ): 1, + Export( + "getEnvWithoutDefaults", + ): 2, +} +``` + + +# Modules (prod) +## Part 0 +```js +"module evaluation"; +import '../../utils/environment'; +import './globalThis'; + +``` +## Part 1 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 2 +}; +export { getEnv }; +import { DEFAULT_ENVIRONMENT } from '../../utils/environment'; +import { parseEnvironment } from '../../utils/environment'; +import { _globalThis } from './globalThis'; +function getEnv() { + var globalEnv = parseEnvironment(_globalThis); + return Object.assign({}, DEFAULT_ENVIRONMENT, globalEnv); +} +export { DEFAULT_ENVIRONMENT } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { parseEnvironment } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { _globalThis } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getEnv } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +export { getEnvWithoutDefaults }; +import { parseEnvironment } from '../../utils/environment'; +import { _globalThis } from './globalThis'; +function getEnvWithoutDefaults() { + return parseEnvironment(_globalThis); +} +export { parseEnvironment } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { _globalThis } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getEnvWithoutDefaults } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +export { getEnv } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getEnv" +}; +export { getEnvWithoutDefaults } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getEnvWithoutDefaults" +}; + +``` +## Merged (module eval) +```js +import '../../utils/environment'; +import './globalThis'; +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-handler/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-handler/input.js new file mode 100644 index 0000000000000..3fa42f80742e1 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-handler/input.js @@ -0,0 +1,9 @@ +import { NextResponse } from "next/server"; + +export const GET = (req) => { + return NextResponse.json({ + pathname: req.nextUrl.pathname, + }); +}; + +export const runtime = "edge"; diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-handler/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-handler/output.md new file mode 100644 index 0000000000000..c6c9a53751928 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-handler/output.md @@ -0,0 +1,252 @@ +# Items + +Count: 7 + +## Item 1: Stmt 0, `ImportOfModule` + +```js +import { NextResponse } from "next/server"; + +``` + +- Hoisted +- Side effects + +## Item 2: Stmt 0, `ImportBinding(0)` + +```js +import { NextResponse } from "next/server"; + +``` + +- Hoisted +- Declares: `NextResponse` + +## Item 3: Stmt 1, `VarDeclarator(0)` + +```js +export const GET = (req)=>{ + return NextResponse.json({ + pathname: req.nextUrl.pathname + }); +}; + +``` + +- Declares: `GET` +- Reads: `NextResponse` +- Write: `GET`, `NextResponse` + +## Item 4: Stmt 2, `VarDeclarator(0)` + +```js +export const runtime = "edge"; + +``` + +- Declares: `runtime` +- Write: `runtime` + +# Phase 1 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item5["ModuleEvaluation"]; + Item6; + Item6["export GET"]; + Item7; + Item7["export runtime"]; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item5["ModuleEvaluation"]; + Item6; + Item6["export GET"]; + Item7; + Item7["export runtime"]; + Item3 --> Item2; + Item6 --> Item3; + Item7 --> Item4; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item5["ModuleEvaluation"]; + Item6; + Item6["export GET"]; + Item7; + Item7["export runtime"]; + Item3 --> Item2; + Item6 --> Item3; + Item7 --> Item4; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item2; + Item3; + Item4; + Item5; + Item5["ModuleEvaluation"]; + Item6; + Item6["export GET"]; + Item7; + Item7["export runtime"]; + Item3 --> Item2; + Item6 --> Item3; + Item7 --> Item4; + Item5 --> Item1; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation), ItemId(0, ImportOfModule)]"]; + N1["Items: [ItemId(Export(("GET", #2), "GET")), ItemId(0, ImportBinding(0)), ItemId(1, VarDeclarator(0))]"]; + N2["Items: [ItemId(Export(("runtime", #2), "runtime")), ItemId(2, VarDeclarator(0))]"]; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Exports: 3, + Export( + "runtime", + ): 2, + Export( + "GET", + ): 1, +} +``` + + +# Modules (dev) +## Part 0 +```js +"module evaluation"; +import "next/server"; + +``` +## Part 1 +```js +export { GET }; +import { NextResponse } from "next/server"; +const GET = (req)=>{ + return NextResponse.json({ + pathname: req.nextUrl.pathname + }); +}; +export { NextResponse } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { GET } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +export { runtime }; +const runtime = "edge"; +export { runtime } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +export { GET } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export GET" +}; +export { runtime } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export runtime" +}; + +``` +## Merged (module eval) +```js +import "next/server"; +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Exports: 3, + Export( + "runtime", + ): 2, + Export( + "GET", + ): 1, +} +``` + + +# Modules (prod) +## Part 0 +```js +"module evaluation"; +import "next/server"; + +``` +## Part 1 +```js +export { GET }; +import { NextResponse } from "next/server"; +const GET = (req)=>{ + return NextResponse.json({ + pathname: req.nextUrl.pathname + }); +}; +export { NextResponse } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { GET } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +export { runtime }; +const runtime = "edge"; +export { runtime } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 3 +```js +export { GET } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export GET" +}; +export { runtime } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export runtime" +}; + +``` +## Merged (module eval) +```js +import "next/server"; +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-kind/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-kind/output.md index b786b9b06e654..68fb073d93c3c 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-kind/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/route-kind/output.md @@ -98,6 +98,7 @@ graph TD Export( "RouteKind", ): 1, + Exports: 4, } ``` @@ -142,6 +143,13 @@ import { RouteKind } from "__TURBOPACK_PART__" assert { RouteKind["APP_ROUTE"] = "APP_ROUTE"; })(RouteKind || (RouteKind = {})); +``` +## Part 4 +```js +export { RouteKind } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export RouteKind" +}; + ``` ## Merged (module eval) ```js @@ -159,6 +167,7 @@ import "__TURBOPACK_PART__" assert { Export( "RouteKind", ): 1, + Exports: 4, } ``` @@ -203,6 +212,13 @@ import { RouteKind } from "__TURBOPACK_PART__" assert { RouteKind["APP_ROUTE"] = "APP_ROUTE"; })(RouteKind || (RouteKind = {})); +``` +## Part 4 +```js +export { RouteKind } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export RouteKind" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-2/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-2/output.md index ac5007085d28d..271f9598383b2 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-2/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-2/output.md @@ -46,7 +46,7 @@ const shared = { - Declares: `shared` - Reads: `random`, `order` -- Write: `shared`, `order` +- Write: `shared`, `random`, `order` ## Item 5: Stmt 4, `Normal` @@ -71,7 +71,7 @@ export const a = { - Declares: `a` - Reads: `shared` -- Write: `a` +- Write: `a`, `shared` ## Item 7: Stmt 6, `VarDeclarator(0)` @@ -85,7 +85,7 @@ export const b = { - Declares: `b` - Reads: `shared` -- Write: `b` +- Write: `b`, `shared` # Phase 1 ```mermaid @@ -134,6 +134,7 @@ graph TD Item5 --> Item2; Item5 --> Item3; Item6 --> Item4; + Item7 --> Item6; Item7 --> Item4; Item9 --> Item5; Item9 --> Item1; @@ -168,6 +169,7 @@ graph TD Item5 --> Item2; Item5 --> Item3; Item6 --> Item4; + Item7 --> Item6; Item7 --> Item4; Item9 --> Item5; Item9 --> Item1; @@ -202,6 +204,7 @@ graph TD Item5 --> Item2; Item5 --> Item3; Item6 --> Item4; + Item7 --> Item6; Item7 --> Item4; Item9 --> Item5; Item9 --> Item1; @@ -216,19 +219,21 @@ graph TD graph TD N0["Items: [ItemId(ModuleEvaluation)]"]; N1["Items: [ItemId(Export(("order", #2), "order"))]"]; - N2["Items: [ItemId(Export(("a", #2), "a")), ItemId(5, VarDeclarator(0))]"]; + N2["Items: [ItemId(Export(("a", #2), "a"))]"]; N3["Items: [ItemId(Export(("b", #2), "b")), ItemId(6, VarDeclarator(0))]"]; N4["Items: [ItemId(0, VarDeclarator(0))]"]; N5["Items: [ItemId(1, Normal)]"]; N6["Items: [ItemId(2, VarDeclarator(0))]"]; N7["Items: [ItemId(3, VarDeclarator(0))]"]; N8["Items: [ItemId(4, Normal)]"]; + N9["Items: [ItemId(5, VarDeclarator(0))]"]; N0 --> N5; N0 --> N6; N0 --> N8; N1 --> N8; N1 --> N4; - N2 --> N7; + N2 --> N9; + N3 --> N9; N3 --> N7; N5 --> N4; N6 --> N5; @@ -239,6 +244,7 @@ graph TD N8 --> N4; N8 --> N5; N8 --> N6; + N9 --> N7; ``` # Entrypoints @@ -248,6 +254,7 @@ graph TD Export( "order", ): 1, + Exports: 10, Export( "b", ): 3, @@ -286,21 +293,17 @@ export { order }; ``` ## Part 2 ```js -import { shared } from "__TURBOPACK_PART__" assert { - __turbopack_part__: 7 +import { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 }; export { a }; -const a = { - shared, - a: "aaaaaaaaaaa" -}; -export { a } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 3 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { shared } from "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; @@ -377,6 +380,33 @@ import "__TURBOPACK_PART__" assert { }; order.push("c"); +``` +## Part 9 +```js +import { shared } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +const a = { + shared, + a: "aaaaaaaaaaa" +}; +export { a } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +export { order } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export order" +}; +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + ``` ## Merged (module eval) ```js @@ -400,6 +430,7 @@ import "__TURBOPACK_PART__" assert { Export( "order", ): 1, + Exports: 10, Export( "b", ): 3, @@ -438,21 +469,17 @@ export { order }; ``` ## Part 2 ```js -import { shared } from "__TURBOPACK_PART__" assert { - __turbopack_part__: 7 +import { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 }; export { a }; -const a = { - shared, - a: "aaaaaaaaaaa" -}; -export { a } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 3 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { shared } from "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; @@ -529,6 +556,33 @@ import "__TURBOPACK_PART__" assert { }; order.push("c"); +``` +## Part 9 +```js +import { shared } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +const a = { + shared, + a: "aaaaaaaaaaa" +}; +export { a } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +export { order } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export order" +}; +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-and-side-effects/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-and-side-effects/output.md index c20663c4e6941..32f1a2d2b41de 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-and-side-effects/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-and-side-effects/output.md @@ -66,7 +66,7 @@ const shared = { - Declares: `shared` - Reads: `value`, `value2`, `value3` -- Write: `shared` +- Write: `shared`, `value`, `value2`, `value3` ## Item 7: Stmt 6, `Normal` @@ -90,7 +90,7 @@ export const a = { - Declares: `a` - Reads: `shared` -- Write: `a` +- Write: `a`, `shared` ## Item 9: Stmt 8, `VarDeclarator(0)` @@ -104,7 +104,7 @@ export const b = { - Declares: `b` - Reads: `shared` -- Write: `b` +- Write: `b`, `shared` # Phase 1 ```mermaid @@ -163,7 +163,10 @@ graph TD Item7 --> Item4; Item7 --> Item5; Item8 --> Item6; + Item8 -.-> Item7; + Item9 --> Item8; Item9 --> Item6; + Item9 -.-> Item7; Item11 --> Item8; Item12 --> Item9; ``` @@ -205,7 +208,10 @@ graph TD Item7 --> Item4; Item7 --> Item5; Item8 --> Item6; + Item8 -.-> Item7; + Item9 --> Item8; Item9 --> Item6; + Item9 -.-> Item7; Item11 --> Item8; Item12 --> Item9; ``` @@ -247,7 +253,10 @@ graph TD Item7 --> Item4; Item7 --> Item5; Item8 --> Item6; + Item8 -.-> Item7; + Item9 --> Item8; Item9 --> Item6; + Item9 -.-> Item7; Item11 --> Item8; Item12 --> Item9; Item10 --> Item1; @@ -260,8 +269,8 @@ graph TD # Final ```mermaid graph TD - N0["Items: [ItemId(ModuleEvaluation), ItemId(6, Normal)]"]; - N1["Items: [ItemId(Export(("a", #2), "a")), ItemId(7, VarDeclarator(0))]"]; + N0["Items: [ItemId(ModuleEvaluation)]"]; + N1["Items: [ItemId(Export(("a", #2), "a"))]"]; N2["Items: [ItemId(Export(("b", #2), "b")), ItemId(8, VarDeclarator(0))]"]; N3["Items: [ItemId(0, Normal)]"]; N4["Items: [ItemId(1, VarDeclarator(0))]"]; @@ -269,14 +278,18 @@ graph TD N6["Items: [ItemId(3, Normal)]"]; N7["Items: [ItemId(4, VarDeclarator(0))]"]; N8["Items: [ItemId(5, VarDeclarator(0))]"]; + N9["Items: [ItemId(6, Normal)]"]; + N10["Items: [ItemId(7, VarDeclarator(0))]"]; N0 --> N3; N0 --> N4; N0 --> N5; N0 --> N6; N0 --> N7; - N0 --> N8; - N1 --> N8; + N0 --> N9; + N1 --> N10; + N2 --> N10; N2 --> N8; + N2 --> N9; N4 --> N3; N5 --> N3; N5 --> N4; @@ -290,12 +303,21 @@ graph TD N8 --> N4; N8 --> N5; N8 --> N7; + N9 --> N8; + N9 --> N3; + N9 --> N4; + N9 --> N5; + N9 --> N6; + N9 --> N7; + N10 --> N8; + N10 --> N9; ``` # Entrypoints ``` { ModuleEvaluation: 0, + Exports: 11, Export( "b", ): 2, @@ -324,33 +346,31 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; -import { shared } from "__TURBOPACK_PART__" assert { - __turbopack_part__: 8 +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 }; "module evaluation"; -console.log(shared); ``` ## Part 1 ```js -import { shared } from "__TURBOPACK_PART__" assert { - __turbopack_part__: 8 +import { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 }; export { a }; -const a = { - shared, - a: "aaaaaaaaaaa" -}; -export { a } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 2 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 10 +}; import { shared } from "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; export { b }; const b = { shared, @@ -446,8 +466,11 @@ export { shared } from "__TURBOPACK_VAR__" assert { }; ``` -## Merged (module eval) +## Part 9 ```js +import { shared } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; import "__TURBOPACK_PART__" assert { __turbopack_part__: 3 }; @@ -463,11 +486,57 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; +console.log(shared); + +``` +## Part 10 +```js import { shared } from "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; +const a = { + shared, + a: "aaaaaaaaaaa" +}; +export { a } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 11 +```js +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + +``` +## Merged (module eval) +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 3 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 6 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; "module evaluation"; -console.log(shared); ``` # Entrypoints @@ -475,6 +544,7 @@ console.log(shared); ``` { ModuleEvaluation: 0, + Exports: 10, Export( "b", ): 2, @@ -512,21 +582,17 @@ console.log(shared); ``` ## Part 1 ```js -import { shared } from "__TURBOPACK_PART__" assert { - __turbopack_part__: 8 +import { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 }; export { a }; -const a = { - shared, - a: "aaaaaaaaaaa" -}; -export { a } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 2 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { shared } from "__TURBOPACK_PART__" assert { __turbopack_part__: 8 }; @@ -624,6 +690,30 @@ export { shared } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 9 +```js +import { shared } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 8 +}; +const a = { + shared, + a: "aaaaaaaaaaa" +}; +export { a } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-regression/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-regression/output.md index ac5007085d28d..271f9598383b2 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-regression/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/shared-regression/output.md @@ -46,7 +46,7 @@ const shared = { - Declares: `shared` - Reads: `random`, `order` -- Write: `shared`, `order` +- Write: `shared`, `random`, `order` ## Item 5: Stmt 4, `Normal` @@ -71,7 +71,7 @@ export const a = { - Declares: `a` - Reads: `shared` -- Write: `a` +- Write: `a`, `shared` ## Item 7: Stmt 6, `VarDeclarator(0)` @@ -85,7 +85,7 @@ export const b = { - Declares: `b` - Reads: `shared` -- Write: `b` +- Write: `b`, `shared` # Phase 1 ```mermaid @@ -134,6 +134,7 @@ graph TD Item5 --> Item2; Item5 --> Item3; Item6 --> Item4; + Item7 --> Item6; Item7 --> Item4; Item9 --> Item5; Item9 --> Item1; @@ -168,6 +169,7 @@ graph TD Item5 --> Item2; Item5 --> Item3; Item6 --> Item4; + Item7 --> Item6; Item7 --> Item4; Item9 --> Item5; Item9 --> Item1; @@ -202,6 +204,7 @@ graph TD Item5 --> Item2; Item5 --> Item3; Item6 --> Item4; + Item7 --> Item6; Item7 --> Item4; Item9 --> Item5; Item9 --> Item1; @@ -216,19 +219,21 @@ graph TD graph TD N0["Items: [ItemId(ModuleEvaluation)]"]; N1["Items: [ItemId(Export(("order", #2), "order"))]"]; - N2["Items: [ItemId(Export(("a", #2), "a")), ItemId(5, VarDeclarator(0))]"]; + N2["Items: [ItemId(Export(("a", #2), "a"))]"]; N3["Items: [ItemId(Export(("b", #2), "b")), ItemId(6, VarDeclarator(0))]"]; N4["Items: [ItemId(0, VarDeclarator(0))]"]; N5["Items: [ItemId(1, Normal)]"]; N6["Items: [ItemId(2, VarDeclarator(0))]"]; N7["Items: [ItemId(3, VarDeclarator(0))]"]; N8["Items: [ItemId(4, Normal)]"]; + N9["Items: [ItemId(5, VarDeclarator(0))]"]; N0 --> N5; N0 --> N6; N0 --> N8; N1 --> N8; N1 --> N4; - N2 --> N7; + N2 --> N9; + N3 --> N9; N3 --> N7; N5 --> N4; N6 --> N5; @@ -239,6 +244,7 @@ graph TD N8 --> N4; N8 --> N5; N8 --> N6; + N9 --> N7; ``` # Entrypoints @@ -248,6 +254,7 @@ graph TD Export( "order", ): 1, + Exports: 10, Export( "b", ): 3, @@ -286,21 +293,17 @@ export { order }; ``` ## Part 2 ```js -import { shared } from "__TURBOPACK_PART__" assert { - __turbopack_part__: 7 +import { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 }; export { a }; -const a = { - shared, - a: "aaaaaaaaaaa" -}; -export { a } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 3 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { shared } from "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; @@ -377,6 +380,33 @@ import "__TURBOPACK_PART__" assert { }; order.push("c"); +``` +## Part 9 +```js +import { shared } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +const a = { + shared, + a: "aaaaaaaaaaa" +}; +export { a } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +export { order } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export order" +}; +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + ``` ## Merged (module eval) ```js @@ -400,6 +430,7 @@ import "__TURBOPACK_PART__" assert { Export( "order", ): 1, + Exports: 10, Export( "b", ): 3, @@ -438,21 +469,17 @@ export { order }; ``` ## Part 2 ```js -import { shared } from "__TURBOPACK_PART__" assert { - __turbopack_part__: 7 +import { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 }; export { a }; -const a = { - shared, - a: "aaaaaaaaaaa" -}; -export { a } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 3 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 9 +}; import { shared } from "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; @@ -529,6 +556,33 @@ import "__TURBOPACK_PART__" assert { }; order.push("c"); +``` +## Part 9 +```js +import { shared } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 7 +}; +const a = { + shared, + a: "aaaaaaaaaaa" +}; +export { a } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 10 +```js +export { order } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export order" +}; +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/simple-vars-1/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/simple-vars-1/output.md index c734bad84fb88..458f65fd2d252 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/simple-vars-1/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/simple-vars-1/output.md @@ -88,6 +88,7 @@ graph TD ``` { ModuleEvaluation: 0, + Exports: 3, Export( "b", ): 2, @@ -121,6 +122,16 @@ export { b } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 3 +```js +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + ``` ## Merged (module eval) ```js @@ -132,6 +143,7 @@ export { b } from "__TURBOPACK_VAR__" assert { ``` { ModuleEvaluation: 0, + Exports: 3, Export( "b", ): 2, @@ -165,6 +177,16 @@ export { b } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 3 +```js +export { a } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export a" +}; +export { b } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export b" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/simple/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/simple/output.md index bd41e8eaf0706..b9f46f6616847 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/simple/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/simple/output.md @@ -130,6 +130,7 @@ graph TD ``` { ModuleEvaluation: 0, + Exports: 4, Export( "DOG", ): 1, @@ -181,6 +182,16 @@ export { dog } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 4 +```js +export { DOG } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export DOG" +}; +export { CHIMERA } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export CHIMERA" +}; + ``` ## Merged (module eval) ```js @@ -192,6 +203,7 @@ export { dog } from "__TURBOPACK_VAR__" assert { ``` { ModuleEvaluation: 0, + Exports: 4, Export( "DOG", ): 1, @@ -243,6 +255,16 @@ export { dog } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 4 +```js +export { DOG } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export DOG" +}; +export { CHIMERA } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export CHIMERA" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/template-pages/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/template-pages/output.md index 7b1d2a8064a9b..bcac867d26d06 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/template-pages/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/template-pages/output.md @@ -266,7 +266,7 @@ export const routeModule = new PagesRouteModule({ - Declares: `routeModule` - Reads: `PagesRouteModule`, `RouteKind`, `App`, `Document`, `userland` -- Write: `routeModule`, `RouteKind` +- Write: `routeModule`, `RouteKind`, `App`, `Document`, `userland` # Phase 1 ```mermaid @@ -438,6 +438,18 @@ graph TD Item24 --> Item11; Item24 --> Item10; Item24 --> Item12; + Item24 -.-> Item13; + Item24 -.-> Item14; + Item24 -.-> Item15; + Item24 -.-> Item16; + Item24 -.-> Item17; + Item24 -.-> Item18; + Item24 -.-> Item19; + Item24 -.-> Item20; + Item24 -.-> Item21; + Item24 -.-> Item22; + Item24 -.-> Item23; + Item26 --> Item13; Item27 --> Item14; Item28 --> Item15; Item29 --> Item16; @@ -551,6 +563,18 @@ graph TD Item24 --> Item11; Item24 --> Item10; Item24 --> Item12; + Item24 -.-> Item13; + Item24 -.-> Item14; + Item24 -.-> Item15; + Item24 -.-> Item16; + Item24 -.-> Item17; + Item24 -.-> Item18; + Item24 -.-> Item19; + Item24 -.-> Item20; + Item24 -.-> Item21; + Item24 -.-> Item22; + Item24 -.-> Item23; + Item26 --> Item13; Item27 --> Item14; Item28 --> Item15; Item29 --> Item16; @@ -664,6 +688,18 @@ graph TD Item24 --> Item11; Item24 --> Item10; Item24 --> Item12; + Item24 -.-> Item13; + Item24 -.-> Item14; + Item24 -.-> Item15; + Item24 -.-> Item16; + Item24 -.-> Item17; + Item24 -.-> Item18; + Item24 -.-> Item19; + Item24 -.-> Item20; + Item24 -.-> Item21; + Item24 -.-> Item22; + Item24 -.-> Item23; + Item26 --> Item13; Item27 --> Item14; Item28 --> Item15; Item29 --> Item16; @@ -682,23 +718,22 @@ graph TD Item25 --> Item5; Item25 --> Item6; Item25 --> Item13; - Item26 --> Item13; ``` # Final ```mermaid graph TD N0["Items: [ItemId(ModuleEvaluation)]"]; N1["Items: [ItemId(Export(("__TURBOPACK__default__export__", #3), "default"))]"]; - N2["Items: [ItemId(Export(("getStaticProps", #2), "getStaticProps")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(7, VarDeclarator(0))]"]; - N3["Items: [ItemId(Export(("getStaticPaths", #2), "getStaticPaths")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(8, VarDeclarator(0))]"]; - N4["Items: [ItemId(Export(("getServerSideProps", #2), "getServerSideProps")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(9, VarDeclarator(0))]"]; - N5["Items: [ItemId(Export(("config", #2), "config")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(10, VarDeclarator(0))]"]; - N6["Items: [ItemId(Export(("reportWebVitals", #2), "reportWebVitals")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(11, VarDeclarator(0))]"]; - N7["Items: [ItemId(Export(("unstable_getStaticProps", #2), "unstable_getStaticProps")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(12, VarDeclarator(0))]"]; - N8["Items: [ItemId(Export(("unstable_getStaticPaths", #2), "unstable_getStaticPaths")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(13, VarDeclarator(0))]"]; - N9["Items: [ItemId(Export(("unstable_getStaticParams", #2), "unstable_getStaticParams")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(14, VarDeclarator(0))]"]; - N10["Items: [ItemId(Export(("unstable_getServerProps", #2), "unstable_getServerProps")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(15, VarDeclarator(0))]"]; - N11["Items: [ItemId(Export(("unstable_getServerSideProps", #2), "unstable_getServerSideProps")), ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(16, VarDeclarator(0))]"]; + N2["Items: [ItemId(Export(("getStaticProps", #2), "getStaticProps"))]"]; + N3["Items: [ItemId(Export(("getStaticPaths", #2), "getStaticPaths"))]"]; + N4["Items: [ItemId(Export(("getServerSideProps", #2), "getServerSideProps"))]"]; + N5["Items: [ItemId(Export(("config", #2), "config"))]"]; + N6["Items: [ItemId(Export(("reportWebVitals", #2), "reportWebVitals"))]"]; + N7["Items: [ItemId(Export(("unstable_getStaticProps", #2), "unstable_getStaticProps"))]"]; + N8["Items: [ItemId(Export(("unstable_getStaticPaths", #2), "unstable_getStaticPaths"))]"]; + N9["Items: [ItemId(Export(("unstable_getStaticParams", #2), "unstable_getStaticParams"))]"]; + N10["Items: [ItemId(Export(("unstable_getServerProps", #2), "unstable_getServerProps"))]"]; + N11["Items: [ItemId(Export(("unstable_getServerSideProps", #2), "unstable_getServerSideProps"))]"]; N12["Items: [ItemId(Export(("routeModule", #2), "routeModule")), ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(3, ImportBinding(0)), ItemId(4, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(17, VarDeclarator(0))]"]; N13["Items: [ItemId(0, ImportOfModule)]"]; N14["Items: [ItemId(1, ImportOfModule)]"]; @@ -707,6 +742,16 @@ graph TD N17["Items: [ItemId(4, ImportOfModule)]"]; N18["Items: [ItemId(5, ImportOfModule)]"]; N19["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(6, Normal)]"]; + N20["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(7, VarDeclarator(0))]"]; + N21["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(8, VarDeclarator(0))]"]; + N22["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(9, VarDeclarator(0))]"]; + N23["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(10, VarDeclarator(0))]"]; + N24["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(11, VarDeclarator(0))]"]; + N25["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(12, VarDeclarator(0))]"]; + N26["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(13, VarDeclarator(0))]"]; + N27["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(14, VarDeclarator(0))]"]; + N28["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(15, VarDeclarator(0))]"]; + N29["Items: [ItemId(2, ImportBinding(0)), ItemId(5, ImportBinding(0)), ItemId(16, VarDeclarator(0))]"]; N0 --> N13; N0 --> N14; N0 --> N15; @@ -715,17 +760,27 @@ graph TD N0 --> N18; N0 --> N19; N1 --> N19; - N2 --> N19; - N3 --> N19; - N4 --> N19; - N5 --> N19; - N6 --> N19; - N7 --> N19; - N8 --> N19; - N9 --> N19; - N10 --> N19; - N11 --> N19; + N2 --> N20; + N3 --> N21; + N4 --> N22; + N5 --> N23; + N6 --> N24; + N7 --> N25; + N8 --> N26; + N9 --> N27; + N10 --> N28; + N11 --> N29; + N12 --> N29; N12 --> N19; + N12 --> N20; + N12 --> N21; + N12 --> N22; + N12 --> N23; + N12 --> N24; + N12 --> N25; + N12 --> N26; + N12 --> N27; + N12 --> N28; N14 --> N13; N15 --> N13; N15 --> N14; @@ -741,12 +796,22 @@ graph TD N18 --> N15; N18 --> N16; N18 --> N17; + N19 --> N29; N19 --> N13; N19 --> N14; N19 --> N15; N19 --> N16; N19 --> N17; N19 --> N18; + N20 --> N29; + N21 --> N29; + N22 --> N29; + N23 --> N29; + N24 --> N29; + N25 --> N29; + N26 --> N29; + N27 --> N29; + N28 --> N29; ``` # Entrypoints @@ -780,6 +845,7 @@ graph TD Export( "unstable_getStaticProps", ): 7, + Exports: 30, Export( "default", ): 1, @@ -830,209 +896,119 @@ export { __TURBOPACK__default__export__ as default }; ``` ## Part 2 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { getStaticProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 20 }; export { getStaticProps }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const getStaticProps = hoist(userland, 'getStaticProps'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { getStaticProps } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 3 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { getStaticPaths } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 21 }; export { getStaticPaths }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const getStaticPaths = hoist(userland, 'getStaticPaths'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { getStaticPaths } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 4 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { getServerSideProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 22 }; export { getServerSideProps }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const getServerSideProps = hoist(userland, 'getServerSideProps'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { getServerSideProps } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 5 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { config } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 23 }; export { config }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const config = hoist(userland, 'config'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { config } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 6 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { reportWebVitals } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 24 }; export { reportWebVitals }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const reportWebVitals = hoist(userland, 'reportWebVitals'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { reportWebVitals } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 7 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { unstable_getStaticProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 25 }; export { unstable_getStaticProps }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const unstable_getStaticProps = hoist(userland, 'unstable_getStaticProps'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { unstable_getStaticProps } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 8 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { unstable_getStaticPaths } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 26 }; export { unstable_getStaticPaths }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const unstable_getStaticPaths = hoist(userland, 'unstable_getStaticPaths'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { unstable_getStaticPaths } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 9 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { unstable_getStaticParams } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 27 }; export { unstable_getStaticParams }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const unstable_getStaticParams = hoist(userland, 'unstable_getStaticParams'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { unstable_getStaticParams } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 10 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { unstable_getServerProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 28 }; export { unstable_getServerProps }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const unstable_getServerProps = hoist(userland, 'unstable_getServerProps'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { unstable_getServerProps } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 11 ```js -import "__TURBOPACK_PART__" assert { - __turbopack_part__: 19 +import { unstable_getServerSideProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 }; export { unstable_getServerSideProps }; -import { hoist } from './helpers'; -import * as userland from 'VAR_USERLAND'; -const unstable_getServerSideProps = hoist(userland, 'unstable_getServerSideProps'); -export { hoist } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { userland } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; -export { unstable_getServerSideProps } from "__TURBOPACK_VAR__" assert { - __turbopack_var__: true -}; ``` ## Part 12 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; import "__TURBOPACK_PART__" assert { __turbopack_part__: 19 }; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 20 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 21 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 22 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 23 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 24 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 25 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 26 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 27 +}; +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 28 +}; export { routeModule }; import { PagesRouteModule } from '../../server/future/route-modules/pages/module.compiled'; import { RouteKind } from '../../server/future/route-kind'; @@ -1150,6 +1126,9 @@ import 'VAR_USERLAND'; ``` ## Part 19 ```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; import "__TURBOPACK_PART__" assert { __turbopack_part__: 13 }; @@ -1181,6 +1160,233 @@ export { __TURBOPACK__default__export__ } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 20 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const getStaticProps = hoist(userland, 'getStaticProps'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getStaticProps } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 21 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const getStaticPaths = hoist(userland, 'getStaticPaths'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getStaticPaths } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 22 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const getServerSideProps = hoist(userland, 'getServerSideProps'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { getServerSideProps } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 23 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const config = hoist(userland, 'config'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { config } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 24 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const reportWebVitals = hoist(userland, 'reportWebVitals'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { reportWebVitals } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 25 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const unstable_getStaticProps = hoist(userland, 'unstable_getStaticProps'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { unstable_getStaticProps } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 26 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const unstable_getStaticPaths = hoist(userland, 'unstable_getStaticPaths'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { unstable_getStaticPaths } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 27 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const unstable_getStaticParams = hoist(userland, 'unstable_getStaticParams'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { unstable_getStaticParams } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 28 +```js +import "__TURBOPACK_PART__" assert { + __turbopack_part__: 29 +}; +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const unstable_getServerProps = hoist(userland, 'unstable_getServerProps'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { unstable_getServerProps } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 29 +```js +import { hoist } from './helpers'; +import * as userland from 'VAR_USERLAND'; +const unstable_getServerSideProps = hoist(userland, 'unstable_getServerSideProps'); +export { hoist } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { userland } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { unstable_getServerSideProps } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 30 +```js +export { default } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export default" +}; +export { getStaticProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getStaticProps" +}; +export { getStaticPaths } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getStaticPaths" +}; +export { getServerSideProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getServerSideProps" +}; +export { config } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export config" +}; +export { reportWebVitals } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export reportWebVitals" +}; +export { unstable_getStaticProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getStaticProps" +}; +export { unstable_getStaticPaths } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getStaticPaths" +}; +export { unstable_getStaticParams } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getStaticParams" +}; +export { unstable_getServerProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getServerProps" +}; +export { unstable_getServerSideProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getServerSideProps" +}; +export { routeModule } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export routeModule" +}; + ``` ## Merged (module eval) ```js @@ -1240,6 +1446,7 @@ import "__TURBOPACK_PART__" assert { Export( "unstable_getStaticProps", ): 7, + Exports: 20, Export( "default", ): 1, @@ -1641,6 +1848,46 @@ export { __TURBOPACK__default__export__ } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 20 +```js +export { default } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export default" +}; +export { getStaticProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getStaticProps" +}; +export { getStaticPaths } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getStaticPaths" +}; +export { getServerSideProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export getServerSideProps" +}; +export { config } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export config" +}; +export { reportWebVitals } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export reportWebVitals" +}; +export { unstable_getStaticProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getStaticProps" +}; +export { unstable_getStaticPaths } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getStaticPaths" +}; +export { unstable_getStaticParams } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getStaticParams" +}; +export { unstable_getServerProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getServerProps" +}; +export { unstable_getServerSideProps } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export unstable_getServerSideProps" +}; +export { routeModule } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export routeModule" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/test-config-1/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/test-config-1/output.md index 867b54c937d69..a682a132f414d 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/test-config-1/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/test-config-1/output.md @@ -274,11 +274,14 @@ graph TD Item18 --> Item13; Item11 --> Item2; Item11 --> Item8; + Item11 --> Item3; Item12 --> Item11; Item12 --> Item8; + Item12 --> Item3; Item13 -.-> Item4; Item13 -.-> Item7; Item13 -.-> Item15; + Item13 --> Item3; ``` # Phase 4 ```mermaid @@ -331,11 +334,14 @@ graph TD Item18 --> Item13; Item11 --> Item2; Item11 --> Item8; + Item11 --> Item3; Item12 --> Item11; Item12 --> Item8; + Item12 --> Item3; Item13 -.-> Item4; Item13 -.-> Item7; Item13 -.-> Item15; + Item13 --> Item3; Item14 --> Item1; Item14 --> Item9; ``` @@ -362,9 +368,11 @@ graph TD N2 --> N6; N3 --> N10; N3 --> N9; + N3 --> N5; N4 --> N6; N4 --> N8; N4 --> N1; + N4 --> N5; N6 --> N5; N7 --> N5; N7 --> N6; @@ -375,6 +383,7 @@ graph TD N9 --> N6; N9 --> N8; N10 --> N9; + N10 --> N5; ``` # Entrypoints @@ -384,6 +393,7 @@ graph TD Export( "external1", ): 3, + Exports: 11, Export( "foo", ): 2, @@ -448,6 +458,9 @@ import { internal } from "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external1 }; function external1() { return internal() + foobar; @@ -468,6 +481,9 @@ import "__TURBOPACK_PART__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 1 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external2 }; function external2() { foobar += "."; @@ -547,6 +563,9 @@ foobar += "foo"; import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; import { upper } from "module"; function internal() { return upper(foobar); @@ -558,6 +577,22 @@ export { internal } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 11 +```js +export { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foobar" +}; +export { foo } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foo" +}; +export { external1 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external1" +}; +export { external2 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external2" +}; + ``` ## Merged (module eval) ```js @@ -590,6 +625,7 @@ export { upper } from "__TURBOPACK_VAR__" assert { Export( "external1", ): 3, + Exports: 8, Export( "foo", ): 2, @@ -649,6 +685,9 @@ export { foo } from "__TURBOPACK_VAR__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external1 }; import { upper } from "module"; function internal() { @@ -670,6 +709,9 @@ export { external1 } from "__TURBOPACK_VAR__" assert { ``` ## Part 4 ```js +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; export { external2 }; function external2() { foobar += "."; @@ -709,6 +751,22 @@ import { foobar } from "__TURBOPACK_PART__" assert { }; foobar += "foo"; +``` +## Part 8 +```js +export { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foobar" +}; +export { foo } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export foo" +}; +export { external1 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external1" +}; +export { external2 } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export external2" +}; + ``` ## Merged (module eval) ```js @@ -735,6 +793,7 @@ export { foobarCopy } from "__TURBOPACK_VAR__" assert { Export( "external1", ): 3, + Exports: 8, Export( "foo", ): 2, @@ -753,6 +812,9 @@ export { foobarCopy } from "__TURBOPACK_VAR__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; import { upper } from "module"; export { external1 }; function internal() { @@ -780,6 +842,7 @@ export { external1 } from "__TURBOPACK_VAR__" assert { Export( "external1", ): 3, + Exports: 8, Export( "foo", ): 2, @@ -798,6 +861,9 @@ export { external1 } from "__TURBOPACK_VAR__" assert { import "__TURBOPACK_PART__" assert { __turbopack_part__: 7 }; +import { foobar } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 5 +}; import { upper } from "module"; export { external1 }; function internal() { diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/tla-1/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/tla-1/output.md index 2324a75bfe17d..c36079b04971c 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/tla-1/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/tla-1/output.md @@ -115,12 +115,13 @@ graph TD ``` { ModuleEvaluation: 0, - Export( - "effects", - ): 1, Export( "effect", ): 2, + Exports: 4, + Export( + "effects", + ): 1, } ``` @@ -164,6 +165,16 @@ export { effects } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 4 +```js +export { effects } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export effects" +}; +export { effect } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export effect" +}; + ``` ## Merged (module eval) ```js @@ -176,12 +187,13 @@ await Promise.resolve(); ``` { ModuleEvaluation: 0, - Export( - "effects", - ): 1, Export( "effect", ): 2, + Exports: 4, + Export( + "effects", + ): 1, } ``` @@ -222,6 +234,16 @@ export { effects } from "__TURBOPACK_VAR__" assert { __turbopack_var__: true }; +``` +## Part 4 +```js +export { effects } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export effects" +}; +export { effect } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export effect" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/typeof-1/input.js b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/typeof-1/input.js new file mode 100644 index 0000000000000..fd7505a005999 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/typeof-1/input.js @@ -0,0 +1,10 @@ +import { NextResponse } from 'next/server' +import { ClientComponent } from '../../ClientComponent' +import { MyModuleClientComponent } from 'my-module/MyModuleClientComponent' + +export function GET() { + return NextResponse.json({ + clientComponent: typeof ClientComponent, + myModuleClientComponent: typeof MyModuleClientComponent, + }) +} diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/typeof-1/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/typeof-1/output.md new file mode 100644 index 0000000000000..20fc2ac084576 --- /dev/null +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/typeof-1/output.md @@ -0,0 +1,298 @@ +# Items + +Count: 9 + +## Item 1: Stmt 0, `ImportOfModule` + +```js +import { NextResponse } from 'next/server'; + +``` + +- Hoisted +- Side effects + +## Item 2: Stmt 0, `ImportBinding(0)` + +```js +import { NextResponse } from 'next/server'; + +``` + +- Hoisted +- Declares: `NextResponse` + +## Item 3: Stmt 1, `ImportOfModule` + +```js +import { ClientComponent } from '../../ClientComponent'; + +``` + +- Hoisted +- Side effects + +## Item 4: Stmt 1, `ImportBinding(0)` + +```js +import { ClientComponent } from '../../ClientComponent'; + +``` + +- Hoisted +- Declares: `ClientComponent` + +## Item 5: Stmt 2, `ImportOfModule` + +```js +import { MyModuleClientComponent } from 'my-module/MyModuleClientComponent'; + +``` + +- Hoisted +- Side effects + +## Item 6: Stmt 2, `ImportBinding(0)` + +```js +import { MyModuleClientComponent } from 'my-module/MyModuleClientComponent'; + +``` + +- Hoisted +- Declares: `MyModuleClientComponent` + +## Item 7: Stmt 3, `Normal` + +```js +export function GET() { + return NextResponse.json({ + clientComponent: typeof ClientComponent, + myModuleClientComponent: typeof MyModuleClientComponent + }); +} + +``` + +- Hoisted +- Declares: `GET` +- Reads (eventual): `NextResponse`, `ClientComponent`, `MyModuleClientComponent` +- Write: `GET` +- Write (eventual): `NextResponse` + +# Phase 1 +```mermaid +graph TD + Item1; + Item4; + Item2; + Item5; + Item3; + Item6; + Item7; + Item8; + Item8["ModuleEvaluation"]; + Item9; + Item9["export GET"]; + Item2 --> Item1; + Item3 --> Item1; + Item3 --> Item2; +``` +# Phase 2 +```mermaid +graph TD + Item1; + Item4; + Item2; + Item5; + Item3; + Item6; + Item7; + Item8; + Item8["ModuleEvaluation"]; + Item9; + Item9["export GET"]; + Item2 --> Item1; + Item3 --> Item1; + Item3 --> Item2; + Item9 --> Item7; +``` +# Phase 3 +```mermaid +graph TD + Item1; + Item4; + Item2; + Item5; + Item3; + Item6; + Item7; + Item8; + Item8["ModuleEvaluation"]; + Item9; + Item9["export GET"]; + Item2 --> Item1; + Item3 --> Item1; + Item3 --> Item2; + Item9 --> Item7; + Item7 --> Item4; + Item7 --> Item5; + Item7 --> Item6; +``` +# Phase 4 +```mermaid +graph TD + Item1; + Item4; + Item2; + Item5; + Item3; + Item6; + Item7; + Item8; + Item8["ModuleEvaluation"]; + Item9; + Item9["export GET"]; + Item2 --> Item1; + Item3 --> Item1; + Item3 --> Item2; + Item9 --> Item7; + Item7 --> Item4; + Item7 --> Item5; + Item7 --> Item6; + Item8 --> Item1; + Item8 --> Item2; + Item8 --> Item3; +``` +# Final +```mermaid +graph TD + N0["Items: [ItemId(ModuleEvaluation), ItemId(0, ImportOfModule), ItemId(1, ImportOfModule), ItemId(2, ImportOfModule)]"]; + N1["Items: [ItemId(Export(("GET", #2), "GET")), ItemId(0, ImportBinding(0)), ItemId(1, ImportBinding(0)), ItemId(2, ImportBinding(0)), ItemId(3, Normal)]"]; +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Exports: 2, + Export( + "GET", + ): 1, +} +``` + + +# Modules (dev) +## Part 0 +```js +"module evaluation"; +import 'next/server'; +import '../../ClientComponent'; +import 'my-module/MyModuleClientComponent'; + +``` +## Part 1 +```js +export { GET }; +import { NextResponse } from 'next/server'; +import { ClientComponent } from '../../ClientComponent'; +import { MyModuleClientComponent } from 'my-module/MyModuleClientComponent'; +function GET() { + return NextResponse.json({ + clientComponent: typeof ClientComponent, + myModuleClientComponent: typeof MyModuleClientComponent + }); +} +export { NextResponse } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { ClientComponent } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { MyModuleClientComponent } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { GET } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +export { GET } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export GET" +}; + +``` +## Merged (module eval) +```js +import 'next/server'; +import '../../ClientComponent'; +import 'my-module/MyModuleClientComponent'; +"module evaluation"; + +``` +# Entrypoints + +``` +{ + ModuleEvaluation: 0, + Exports: 2, + Export( + "GET", + ): 1, +} +``` + + +# Modules (prod) +## Part 0 +```js +"module evaluation"; +import 'next/server'; +import '../../ClientComponent'; +import 'my-module/MyModuleClientComponent'; + +``` +## Part 1 +```js +export { GET }; +import { NextResponse } from 'next/server'; +import { ClientComponent } from '../../ClientComponent'; +import { MyModuleClientComponent } from 'my-module/MyModuleClientComponent'; +function GET() { + return NextResponse.json({ + clientComponent: typeof ClientComponent, + myModuleClientComponent: typeof MyModuleClientComponent + }); +} +export { NextResponse } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { ClientComponent } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { MyModuleClientComponent } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; +export { GET } from "__TURBOPACK_VAR__" assert { + __turbopack_var__: true +}; + +``` +## Part 2 +```js +export { GET } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export GET" +}; + +``` +## Merged (module eval) +```js +import 'next/server'; +import '../../ClientComponent'; +import 'my-module/MyModuleClientComponent'; +"module evaluation"; + +``` diff --git a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/write-order/output.md b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/write-order/output.md index 052eb476d8e5f..28ecbdfae8a6a 100644 --- a/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/write-order/output.md +++ b/crates/turbopack-ecmascript/tests/tree-shaker/analyzer/write-order/output.md @@ -171,6 +171,7 @@ graph TD Item10 --> Item2; Item11 --> Item6; Item2 --> Item7; + Item2 --> Item1; Item2 -.-> Item9; ``` # Phase 4 @@ -207,6 +208,7 @@ graph TD Item10 --> Item2; Item11 --> Item6; Item2 --> Item7; + Item2 --> Item1; Item2 -.-> Item9; Item8 --> Item3; Item8 --> Item4; @@ -233,6 +235,7 @@ graph TD N1 --> N9; N1 --> N4; N2 --> N9; + N2 --> N4; N2 --> N1; N3 --> N8; N5 --> N4; @@ -255,6 +258,7 @@ graph TD Export( "order", ): 1, + Exports: 10, Export( "func", ): 2, @@ -299,6 +303,9 @@ export { order }; import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import { order } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; import "__TURBOPACK_PART__" assert { __turbopack_part__: 1 }; @@ -395,6 +402,19 @@ import "__TURBOPACK_PART__" assert { }; order.push("c"); +``` +## Part 10 +```js +export { order } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export order" +}; +export { func } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export func" +}; +export { shared } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export shared" +}; + ``` ## Merged (module eval) ```js @@ -421,6 +441,7 @@ import "__TURBOPACK_PART__" assert { Export( "order", ): 1, + Exports: 10, Export( "func", ): 2, @@ -465,6 +486,9 @@ export { order }; import "__TURBOPACK_PART__" assert { __turbopack_part__: 9 }; +import { order } from "__TURBOPACK_PART__" assert { + __turbopack_part__: 4 +}; export { func }; function func() { order.push("d"); @@ -558,6 +582,19 @@ import "__TURBOPACK_PART__" assert { }; order.push("c"); +``` +## Part 10 +```js +export { order } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export order" +}; +export { func } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export func" +}; +export { shared } from "__TURBOPACK_PART__" assert { + __turbopack_part__: "export shared" +}; + ``` ## Merged (module eval) ```js diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_05eac0.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_05eac0.js index b496ad7226a0b..c719e831a0c35 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_05eac0.js +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_05eac0.js @@ -141,12 +141,14 @@ __turbopack_esm__({ }); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$dynamic$2d$import$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/input/lib.js [test] (ecmascript) "); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$dynamic$2d$import$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__8$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/input/lib.js [test] (ecmascript) "); +var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$dynamic$2d$import$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/input/lib.js [test] (ecmascript) "); "__TURBOPACK__ecmascript__hoisting__location__"; ; ; ; +; function getChimera() { - return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$dynamic$2d$import$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + dog; + return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$dynamic$2d$import$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$dynamic$2d$import$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__["dog"]; } ; diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_05eac0.js.map b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_05eac0.js.map index 8771340104287..76543693cb889 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_05eac0.js.map +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_05eac0.js.map @@ -18,12 +18,12 @@ {"offset": {"line": 91, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, {"offset": {"line": 96, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAMA,SAAS;IACP,OAAO,qOAAA,CAAA,MAAG;AACZ;AAMA,SAAS,OAAO,MAAM;IACpB,qOAAA,CAAA,MAAG,GAAG;AACR;AAMO,MAAM,SAAS;IACpB,SAAS,qOAAA,CAAA,MAAG;IACZ,KAAK;IACL,KAAK;AACP"}}, {"offset": {"line": 130, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 135, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,qOAAA,CAAA,MAAG,GAAG;AACf"}}, - {"offset": {"line": 151, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 156, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,qOAAA,CAAA,MAAG"}}, - {"offset": {"line": 168, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 173, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 189, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 194, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 205, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 135, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,qOAAA,CAAA,MAAG,GAAG,qOAAA,CAAA,MAAG;AAClB"}}, + {"offset": {"line": 153, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 158, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,qOAAA,CAAA,MAAG"}}, + {"offset": {"line": 170, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 175, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 191, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 196, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 207, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js similarity index 91% rename from crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js rename to crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js index d6d4fa4d32efa..61beca3561f02 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js @@ -1,4 +1,4 @@ -(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js", { +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js", { "[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) ": (({ r: __turbopack_require__, f: __turbopack_module_context__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, n: __turbopack_export_namespace__, c: __turbopack_cache__, M: __turbopack_modules__, l: __turbopack_load__, j: __turbopack_dynamic__, P: __turbopack_resolve_absolute_path__, U: __turbopack_relative_url__, R: __turbopack_resolve_module_id_path__, g: global, __dirname }) => (() => { "use strict"; @@ -150,12 +150,14 @@ __turbopack_esm__({ }); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) "); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__8$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) "); +var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) "); "__TURBOPACK__ecmascript__hoisting__location__"; ; ; ; +; function getChimera() { - return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + dog; + return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__["dog"]; } ; @@ -197,32 +199,16 @@ var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests ; ; -})()), -"[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) ": (({ r: __turbopack_require__, f: __turbopack_module_context__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, n: __turbopack_export_namespace__, c: __turbopack_cache__, M: __turbopack_modules__, l: __turbopack_load__, j: __turbopack_dynamic__, P: __turbopack_resolve_absolute_path__, U: __turbopack_relative_url__, R: __turbopack_resolve_module_id_path__, g: global, __dirname }) => (() => { -"use strict"; - -__turbopack_esm__({ - "cat": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__["cat"], - "dogRef": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__["dogRef"], - "getChimera": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__["getChimera"], - "initialCat": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__["initialCat"] -}); -var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$module__evaluation$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) "); -var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) "); -"__TURBOPACK__ecmascript__hoisting__location__"; -; -; - })()), "[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/module.js [test] (ecmascript) ": (({ r: __turbopack_require__, f: __turbopack_module_context__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, n: __turbopack_export_namespace__, c: __turbopack_cache__, M: __turbopack_modules__, l: __turbopack_load__, j: __turbopack_dynamic__, P: __turbopack_resolve_absolute_path__, U: __turbopack_relative_url__, R: __turbopack_resolve_module_id_path__, g: global, __dirname }) => (() => { "use strict"; __turbopack_esm__({ - "__TURBOPACK__reexport__lib__": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$facade$3e$__, - "lib": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$facade$3e$__ + "__TURBOPACK__reexport__lib__": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__, + "lib": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__ }); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$module__evaluation$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) "); -var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$facade$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) "); +var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) "); "__TURBOPACK__ecmascript__hoisting__location__"; ; ; @@ -260,4 +246,4 @@ var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests })()), }]); -//# sourceMappingURL=79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js.map \ No newline at end of file +//# sourceMappingURL=79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js.map \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js.map b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js.map similarity index 89% rename from crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js.map rename to crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js.map index 20adfc3eda1b6..08028c332a04d 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_b4b68a._.js.map +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e0a7d8._.js.map @@ -20,18 +20,16 @@ {"offset": {"line": 100, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, {"offset": {"line": 105, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAMA,SAAS;IACP,OAAO,uOAAA,CAAA,MAAG;AACZ;AAMA,SAAS,OAAO,MAAM;IACpB,uOAAA,CAAA,MAAG,GAAG;AACR;AAMO,MAAM,SAAS;IACpB,SAAS,uOAAA,CAAA,MAAG;IACZ,KAAK;IACL,KAAK;AACP"}}, {"offset": {"line": 139, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 144, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,uOAAA,CAAA,MAAG,GAAG;AACf"}}, - {"offset": {"line": 160, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 165, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,uOAAA,CAAA,MAAG"}}, - {"offset": {"line": 177, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 182, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 198, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 203, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 214, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 219, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 229, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 234, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/index.js"],"sourcesContent":["import {lib} from './module'\n\nconsole.log(lib.cat)\n"],"names":[],"mappings":";;;;;;;;;;;;AAEA,QAAQ,GAAG,CAAC,oOAAA,CAAA,MAAG,CAAC,GAAG"}}, - {"offset": {"line": 248, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 253, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 258, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 144, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,uOAAA,CAAA,MAAG,GAAG,uOAAA,CAAA,MAAG;AAClB"}}, + {"offset": {"line": 162, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 167, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,uOAAA,CAAA,MAAG"}}, + {"offset": {"line": 179, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 184, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 200, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 205, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 215, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 220, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/index.js"],"sourcesContent":["import {lib} from './module'\n\nconsole.log(lib.cat)\n"],"names":[],"mappings":";;;;;;;;;;;;AAEA,QAAQ,GAAG,CAAC,oOAAA,CAAA,MAAG,CAAC,GAAG"}}, + {"offset": {"line": 234, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 239, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 244, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js similarity index 90% rename from crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js rename to crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js index 281bddcb7f575..0e8d24b6eaeed 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js @@ -1,4 +1,4 @@ -(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js", { +(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js", { "[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) ": (({ r: __turbopack_require__, f: __turbopack_module_context__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, n: __turbopack_export_namespace__, c: __turbopack_cache__, M: __turbopack_modules__, l: __turbopack_load__, j: __turbopack_dynamic__, P: __turbopack_resolve_absolute_path__, U: __turbopack_relative_url__, R: __turbopack_resolve_module_id_path__, g: global, __dirname }) => (() => { "use strict"; @@ -141,12 +141,14 @@ __turbopack_esm__({ }); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) "); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__8$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) "); +var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) "); "__TURBOPACK__ecmascript__hoisting__location__"; ; ; ; +; function getChimera() { - return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + dog; + return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__["dog"]; } ; @@ -188,36 +190,20 @@ var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests ; ; -})()), -"[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) ": (({ r: __turbopack_require__, f: __turbopack_module_context__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, n: __turbopack_export_namespace__, c: __turbopack_cache__, M: __turbopack_modules__, l: __turbopack_load__, j: __turbopack_dynamic__, P: __turbopack_resolve_absolute_path__, U: __turbopack_relative_url__, R: __turbopack_resolve_module_id_path__, g: global, __dirname }) => (() => { -"use strict"; - -__turbopack_esm__({ - "cat": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__["cat"], - "dogRef": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__["dogRef"], - "getChimera": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__["getChimera"], - "initialCat": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__["initialCat"] -}); -var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$module__evaluation$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) "); -var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) "); -"__TURBOPACK__ecmascript__hoisting__location__"; -; -; - })()), "[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/index.js [test] (ecmascript) ": (({ r: __turbopack_require__, f: __turbopack_module_context__, i: __turbopack_import__, s: __turbopack_esm__, v: __turbopack_export_value__, n: __turbopack_export_namespace__, c: __turbopack_cache__, M: __turbopack_modules__, l: __turbopack_load__, j: __turbopack_dynamic__, P: __turbopack_resolve_absolute_path__, U: __turbopack_relative_url__, R: __turbopack_resolve_module_id_path__, g: global, __dirname }) => (() => { "use strict"; __turbopack_esm__({ - "lib": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$facade$3e$__ + "lib": ()=>__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__ }); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$module__evaluation$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) "); -var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$facade$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) "); +var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js [test] (ecmascript) "); "__TURBOPACK__ecmascript__hoisting__location__"; "module evaluation"; ; ; -console.log(__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$facade$3e$__.cat); +console.log(__TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$import$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$exports$3e$__.cat); ; })()), @@ -233,4 +219,4 @@ var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests })()), }]); -//# sourceMappingURL=79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js.map \ No newline at end of file +//# sourceMappingURL=79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js.map \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js.map b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js.map similarity index 89% rename from crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js.map rename to crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js.map index a64d074b155f2..bd930bbd92dfd 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_ee0515._.js.map +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/output/79fb1_turbopack-tests_tests_snapshot_basic-tree-shake_import-namespace_input_87afd8._.js.map @@ -18,16 +18,14 @@ {"offset": {"line": 91, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, {"offset": {"line": 96, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAMA,SAAS;IACP,OAAO,uOAAA,CAAA,MAAG;AACZ;AAMA,SAAS,OAAO,MAAM;IACpB,uOAAA,CAAA,MAAG,GAAG;AACR;AAMO,MAAM,SAAS;IACpB,SAAS,uOAAA,CAAA,MAAG;IACZ,KAAK;IACL,KAAK;AACP"}}, {"offset": {"line": 130, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 135, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,uOAAA,CAAA,MAAG,GAAG;AACf"}}, - {"offset": {"line": 151, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 156, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,uOAAA,CAAA,MAAG"}}, - {"offset": {"line": 168, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 173, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 189, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 194, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 205, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 210, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/index.js"],"sourcesContent":["import * as lib from './lib'\n\nconsole.log(lib.cat)\n"],"names":[],"mappings":";;;;;;;;;AAEA,QAAQ,GAAG,CAAC,6NAAI,GAAG"}}, - {"offset": {"line": 221, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 226, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 231, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 135, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,uOAAA,CAAA,MAAG,GAAG,uOAAA,CAAA,MAAG;AAClB"}}, + {"offset": {"line": 153, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 158, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,uOAAA,CAAA,MAAG"}}, + {"offset": {"line": 170, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 175, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 191, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 196, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/import-namespace/input/index.js"],"sourcesContent":["import * as lib from './lib'\n\nconsole.log(lib.cat)\n"],"names":[],"mappings":";;;;;;;;;AAEA,QAAQ,GAAG,CAAC,8NAAI,GAAG"}}, + {"offset": {"line": 207, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 212, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 217, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/output/a587c_tests_snapshot_basic-tree-shake_require-side-effect_input_fa7732._.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/output/a587c_tests_snapshot_basic-tree-shake_require-side-effect_input_fa7732._.js index b829c74cc07a9..7bad58454328f 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/output/a587c_tests_snapshot_basic-tree-shake_require-side-effect_input_fa7732._.js +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/output/a587c_tests_snapshot_basic-tree-shake_require-side-effect_input_fa7732._.js @@ -141,12 +141,14 @@ __turbopack_esm__({ }); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$require$2d$side$2d$effect$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/lib.js [test] (ecmascript) "); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$require$2d$side$2d$effect$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__8$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/lib.js [test] (ecmascript) "); +var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$require$2d$side$2d$effect$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/lib.js [test] (ecmascript) "); "__TURBOPACK__ecmascript__hoisting__location__"; ; ; ; +; function getChimera() { - return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$require$2d$side$2d$effect$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + dog; + return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$require$2d$side$2d$effect$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$require$2d$side$2d$effect$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__["dog"]; } ; diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/output/a587c_tests_snapshot_basic-tree-shake_require-side-effect_input_fa7732._.js.map b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/output/a587c_tests_snapshot_basic-tree-shake_require-side-effect_input_fa7732._.js.map index 729da17d345fd..3156e34ae11ad 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/output/a587c_tests_snapshot_basic-tree-shake_require-side-effect_input_fa7732._.js.map +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/output/a587c_tests_snapshot_basic-tree-shake_require-side-effect_input_fa7732._.js.map @@ -18,14 +18,14 @@ {"offset": {"line": 91, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, {"offset": {"line": 96, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAMA,SAAS;IACP,OAAO,6OAAA,CAAA,MAAG;AACZ;AAMA,SAAS,OAAO,MAAM;IACpB,6OAAA,CAAA,MAAG,GAAG;AACR;AAMO,MAAM,SAAS;IACpB,SAAS,6OAAA,CAAA,MAAG;IACZ,KAAK;IACL,KAAK;AACP"}}, {"offset": {"line": 130, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 135, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,6OAAA,CAAA,MAAG,GAAG;AACf"}}, - {"offset": {"line": 151, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 156, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,6OAAA,CAAA,MAAG"}}, - {"offset": {"line": 168, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 173, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 189, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 194, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 205, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 209, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/index.js"],"sourcesContent":["const {cat} = require('./lib')\n"],"names":[],"mappings":"AAAA,MAAM,EAAC,GAAG,EAAC"}}, - {"offset": {"line": 210, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 135, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,6OAAA,CAAA,MAAG,GAAG,6OAAA,CAAA,MAAG;AAClB"}}, + {"offset": {"line": 153, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 158, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/lib.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,6OAAA,CAAA,MAAG"}}, + {"offset": {"line": 170, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 175, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 191, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 196, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 207, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 211, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/require-side-effect/input/index.js"],"sourcesContent":["const {cat} = require('./lib')\n"],"names":[],"mappings":"AAAA,MAAM,EAAC,GAAG,EAAC"}}, + {"offset": {"line": 212, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/output/a587c_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_2951af.js b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/output/a587c_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_2951af.js index 00dd5c50b44f6..388520019ef8a 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/output/a587c_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_2951af.js +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/output/a587c_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_2951af.js @@ -141,12 +141,14 @@ __turbopack_esm__({ }); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$tree$2d$shake$2d$test$2d$1$2f$input$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js [test] (ecmascript) "); var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$tree$2d$shake$2d$test$2d$1$2f$input$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__8$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js [test] (ecmascript) "); +var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$tree$2d$shake$2d$test$2d$1$2f$input$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js [test] (ecmascript) "); "__TURBOPACK__ecmascript__hoisting__location__"; ; ; ; +; function getChimera() { - return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$tree$2d$shake$2d$test$2d$1$2f$input$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + dog; + return __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$tree$2d$shake$2d$test$2d$1$2f$input$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__9$3e$__["cat"] + __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$tree$2d$shake$2d$test$2d$1$2f$input$2f$index$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__5$3e$__["dog"]; } ; diff --git a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/output/a587c_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_2951af.js.map b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/output/a587c_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_2951af.js.map index 21f0e7a600376..f088827fe8299 100644 --- a/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/output/a587c_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_2951af.js.map +++ b/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/output/a587c_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_2951af.js.map @@ -18,12 +18,12 @@ {"offset": {"line": 91, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, {"offset": {"line": 96, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAMA,SAAS;IACP,OAAO,gPAAA,CAAA,MAAG;AACZ;AAMA,SAAS,OAAO,MAAM;IACpB,gPAAA,CAAA,MAAG,GAAG;AACR;AAMO,MAAM,SAAS;IACpB,SAAS,gPAAA,CAAA,MAAG;IACZ,KAAK;IACL,KAAK;AACP"}}, {"offset": {"line": 130, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 135, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,gPAAA,CAAA,MAAG,GAAG;AACf"}}, - {"offset": {"line": 151, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 156, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,gPAAA,CAAA,MAAG"}}, - {"offset": {"line": 168, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 173, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 189, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 194, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, - {"offset": {"line": 205, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 135, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAgCO,SAAS;IACd,OAAO,gPAAA,CAAA,MAAG,GAAG,gPAAA,CAAA,MAAG;AAClB"}}, + {"offset": {"line": 153, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 158, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js"],"sourcesContent":["let dog = \"dog\";\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction getDog() {\n return dog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nfunction setDog(newDog) {\n dog = newDog;\n}\n\ndog += \"!\";\n\nconsole.log(dog);\n\nexport const dogRef = {\n initial: dog,\n get: getDog,\n set: setDog,\n};\n\nexport let cat = \"cat\";\n\nexport const initialCat = cat;\n\nexport function getChimera() {\n return cat + dog;\n}\n"],"names":[],"mappings":";;;;;;;;;;AA8BO,MAAM,aAAa,gPAAA,CAAA,MAAG"}}, + {"offset": {"line": 170, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 175, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 191, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 196, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}}, + {"offset": {"line": 207, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file diff --git a/crates/turbopack-wasm/src/module_asset.rs b/crates/turbopack-wasm/src/module_asset.rs index c357e7abaf597..da3381b6923e4 100644 --- a/crates/turbopack-wasm/src/module_asset.rs +++ b/crates/turbopack-wasm/src/module_asset.rs @@ -19,7 +19,6 @@ use turbopack_ecmascript::{ EcmascriptChunkPlaceable, EcmascriptChunkType, EcmascriptExports, }, references::async_module::OptionAsyncModule, - EcmascriptModuleAsset, }; use crate::{ @@ -62,24 +61,43 @@ impl WebAssemblyModuleAsset { } #[turbo_tasks::function] - async fn loader(&self) -> Result> { - let query = &*self.source.ident().query().await?; + async fn loader_as_module(self: Vc) -> Result>> { + let this = self.await?; + let query = &*this.source.ident().query().await?; let loader_source = if query == "?module" { - compiling_loader_source(self.source) + compiling_loader_source(this.source) } else { - instantiating_loader_source(self.source) + instantiating_loader_source(this.source) }; - let module = self.asset_context.process( + let module = this.asset_context.process( loader_source, Value::new(ReferenceType::Internal(Vc::cell(indexmap! { - "WASM_PATH".into() => Vc::upcast(RawWebAssemblyModuleAsset::new(self.source, self.asset_context)), + "WASM_PATH".into() => Vc::upcast(RawWebAssemblyModuleAsset::new(this.source, this.asset_context)), }))), ).module(); + Ok(module) + } + #[turbo_tasks::function] + async fn loader_as_resolve_origin(self: Vc) -> Result>> { + let module = self.loader_as_module(); + + let Some(esm_asset) = Vc::try_resolve_sidecast::>(module).await? + else { + bail!("WASM loader was not processed into an EcmascriptModuleAsset"); + }; + + Ok(esm_asset) + } + + #[turbo_tasks::function] + async fn loader(self: Vc) -> Result>> { + let module = self.loader_as_module(); + let Some(esm_asset) = - Vc::try_resolve_downcast_type::(module).await? + Vc::try_resolve_sidecast::>(module).await? else { bail!("WASM loader was not processed into an EcmascriptModuleAsset"); }; @@ -156,7 +174,7 @@ impl ResolveOrigin for WebAssemblyModuleAsset { #[turbo_tasks::function] fn get_inner_asset(self: Vc, request: Vc) -> Vc { - self.loader().get_inner_asset(request) + self.loader_as_resolve_origin().get_inner_asset(request) } } diff --git a/crates/turbopack/src/module_options/mod.rs b/crates/turbopack/src/module_options/mod.rs index 3eedd7243df0b..6e2570c972361 100644 --- a/crates/turbopack/src/module_options/mod.rs +++ b/crates/turbopack/src/module_options/mod.rs @@ -78,6 +78,7 @@ impl ModuleOptions { execution_context, ref rules, esm_url_rewrite_behavior, + special_exports, import_externals, ignore_dynamic_requests, use_swc_css, @@ -122,6 +123,7 @@ impl ModuleOptions { tree_shaking_mode, url_rewrite_behavior: esm_url_rewrite_behavior, import_externals, + special_exports, ignore_dynamic_requests, refresh, ..Default::default() diff --git a/crates/turbopack/src/module_options/module_options_context.rs b/crates/turbopack/src/module_options/module_options_context.rs index 0b4121f69cc6a..41cab2b11815a 100644 --- a/crates/turbopack/src/module_options/module_options_context.rs +++ b/crates/turbopack/src/module_options/module_options_context.rs @@ -141,6 +141,7 @@ pub struct ModuleOptionsContext { pub placeholder_for_future_extensions: (), pub tree_shaking_mode: Option, pub esm_url_rewrite_behavior: Option, + pub special_exports: Vc>, /// References to externals from ESM imports should use `import()` and make /// async modules. pub import_externals: bool,