Revert "[turbopack] Track re-exports in import_usage inside of compute_import_usage" (#96311)

Reverts vercel/next.js#95989
This commit is contained in:
Sam Poder
2026-07-28 20:29:13 +00:00
committed by GitHub
parent ccfe67aa8d
commit 625dccbc4a
42 changed files with 8 additions and 290 deletions
@@ -6,7 +6,6 @@ use std::{
};
use anyhow::{Context, Result};
use auto_hash_map::AutoSet;
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;
use swc_core::{
@@ -290,8 +289,6 @@ pub(crate) struct ProgramDeclUsage {
pub(crate) decl_usages: FxHashMap<Id, DeclUsage>,
// import -> immediate usage (top level decl)
pub(crate) import_usages: FxHashMap<usize, DeclUsage>,
// import reference -> names it is directly re-exported as (`export { x } from '...'`)
pub(crate) named_reexports: FxHashMap<usize, AutoSet<RcStr>>,
// export name -> top level decl
pub(crate) exports: FxHashMap<RcStr, Id>,
}
@@ -340,24 +337,6 @@ impl ProgramDeclUsage {
);
}
}
// Fold re-exports (`export { x } from "foo"`) into `import_usage` for tree-shaking.
for (reference, names) in &self.named_reexports {
let usage = match import_usage.get(reference) {
Some(ImportUsage::TopLevel) => continue,
// Used locally and re-exported, e.g.
// `import {foo} from 'm'; export function w(){foo()} export {foo} from 'm'`
// → union: Exports({"w"}) ∪ {"foo"}.
Some(ImportUsage::Exports(existing)) => ImportUsage::Exports(
existing
.iter()
.cloned()
.chain(names.iter().cloned())
.collect(),
),
None => ImportUsage::Exports(names.iter().cloned().collect()),
};
import_usage.insert(*reference, usage);
}
import_usage
}
}
@@ -1007,37 +986,26 @@ impl Visit for Analyzer<'_> {
annotations.clone(),
);
let name = match spec {
match spec {
ExportSpecifier::Namespace(n) => {
let name = RcStr::from(n.name.atom().as_str());
self.data
.exports
.insert(name.clone(), Export::ImportedNamespace(i));
name
self.data.exports.insert(
RcStr::from(n.name.atom().as_str()),
Export::ImportedNamespace(i),
);
}
ExportSpecifier::Default(d) => {
let name = RcStr::from(d.exported.sym.as_str());
self.data.exports.insert(
name.clone(),
RcStr::from(d.exported.sym.as_str()),
Export::ImportedBinding(i, rcstr!("default"), false),
);
name
}
ExportSpecifier::Named(n) => {
let name =
RcStr::from(n.exported.as_ref().unwrap_or(&n.orig).atom().as_str());
self.data.exports.insert(
name.clone(),
RcStr::from(n.exported.as_ref().unwrap_or(&n.orig).atom().as_str()),
Export::ImportedBinding(i, RcStr::from(n.orig.atom().as_str()), false),
);
name
}
};
self.program_decl_usage
.named_reexports
.entry(i)
.or_default()
.insert(name);
}
}
} else {
for spec in export.specifiers.iter() {
@@ -1,6 +0,0 @@
async function load() {
const { used } = await import('./lib.js')
console.log(used)
}
load()
@@ -1,2 +0,0 @@
export const used = 'USED'
export { unused } from './unused.js'
@@ -1 +0,0 @@
export const unused = 'UNUSED_MARKER'
@@ -1,6 +0,0 @@
{
"removeUnusedImports": true,
"removeUnusedExports": true,
"treeShakingMode": "reexports-only",
"scopeHoisting": true
}
@@ -1,12 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_basic_input_index_14t7f_0.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/basic/input/index.js [test] (ecmascript)", ((__turbopack_context__, module, exports) => {
async function load() {
const { used } = await __turbopack_context__.A("[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/basic/input/lib.js [test] (ecmascript, async loader)");
console.log(used);
}
load();
}),
]);
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_basic_input_index_14t7f_0.js.map
@@ -1,6 +0,0 @@
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 3, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/basic/input/index.js"],"sourcesContent":["async function load() {\n const { used } = await import('./lib.js')\n console.log(used)\n}\n\nload()\n"],"names":["load","used","console","log"],"mappings":"AAAA,eAAeA;IACb,MAAM,EAAEC,IAAI,EAAE,GAAG;IACjBC,QAAQC,GAAG,CAACF;AACd;AAEAD"}}]
}
@@ -1,15 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_basic_input_lib_02nzs5r.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/basic/input/lib.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
const used = 'USED';
;
__turbopack_context__.s([
"used",
0,
used
]);
}),
]);
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_basic_input_lib_02nzs5r.js.map
@@ -1,6 +0,0 @@
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/basic/input/lib.js"],"sourcesContent":["export const used = 'USED'\nexport { unused } from './unused.js'\n"],"names":["used"],"mappings":"AAAO,MAAMA,OAAO"}}]
}
@@ -1,12 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_basic_input_lib_0u9f373.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/basic/input/lib.js [test] (ecmascript, async loader)", ((__turbopack_context__) => {
__turbopack_context__.v((parentImport) => {
return Promise.all([
"output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_basic_input_lib_02nzs5r.js"
].map((chunk) => __turbopack_context__.l(chunk))).then(() => {
return parentImport("[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/basic/input/lib.js [test] (ecmascript)");
});
});
}),
]);
@@ -1,5 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1i9t_crates_turbopack-tests_tests_snapshot_reexport-drop_basic_input_index_0riermg.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_basic_input_lib_0u9f373.js","output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_basic_input_index_14t7f_0.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/basic/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime
@@ -1,6 +0,0 @@
async function load() {
const { used } = await import('./lib.js')
console.log(used)
}
load()
@@ -1,2 +0,0 @@
export const used = 'USED'
export { unused } from './unused.js'
@@ -1 +0,0 @@
export const unused = 'UNUSED_MARKER'
@@ -1,6 +0,0 @@
{
"removeUnusedImports": true,
"removeUnusedExports": true,
"treeShakingMode": null,
"scopeHoisting": true
}
@@ -1,12 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_dynamic_input_index_138bkae.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/dynamic/input/index.js [test] (ecmascript)", ((__turbopack_context__, module, exports) => {
async function load() {
const { used } = await __turbopack_context__.A("[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/dynamic/input/lib.js [test] (ecmascript, async loader)");
console.log(used);
}
load();
}),
]);
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_dynamic_input_index_138bkae.js.map
@@ -1,6 +0,0 @@
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 3, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/dynamic/input/index.js"],"sourcesContent":["async function load() {\n const { used } = await import('./lib.js')\n console.log(used)\n}\n\nload()\n"],"names":["load","used","console","log"],"mappings":"AAAA,eAAeA;IACb,MAAM,EAAEC,IAAI,EAAE,GAAG;IACjBC,QAAQC,GAAG,CAACF;AACd;AAEAD"}}]
}
@@ -1,12 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_dynamic_input_lib_062uldb.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/dynamic/input/lib.js [test] (ecmascript, async loader)", ((__turbopack_context__) => {
__turbopack_context__.v((parentImport) => {
return Promise.all([
"output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_dynamic_input_lib_1_1s1zh.js"
].map((chunk) => __turbopack_context__.l(chunk))).then(() => {
return parentImport("[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/dynamic/input/lib.js [test] (ecmascript)");
});
});
}),
]);
@@ -1,15 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_dynamic_input_lib_1_1s1zh.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/dynamic/input/lib.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
const used = 'USED';
;
__turbopack_context__.s([
"used",
0,
used
]);
}),
]);
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_dynamic_input_lib_1_1s1zh.js.map
@@ -1,6 +0,0 @@
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/dynamic/input/lib.js"],"sourcesContent":["export const used = 'USED'\nexport { unused } from './unused.js'\n"],"names":["used"],"mappings":"AAAO,MAAMA,OAAO"}}]
}
@@ -1,5 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1i9t_crates_turbopack-tests_tests_snapshot_reexport-drop_dynamic_input_index_1dawwqa.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_dynamic_input_lib_062uldb.js","output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_dynamic_input_index_138bkae.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/dynamic/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime
@@ -1,3 +0,0 @@
import { y } from './lib.js'
console.log(y)
@@ -1,2 +0,0 @@
export { x as y } from './origin.js'
export { z } from './unused.js'
@@ -1 +0,0 @@
export const x = 'X_USED_MARKER'
@@ -1 +0,0 @@
export const z = 'Z_UNUSED_MARKER'
@@ -1,5 +0,0 @@
{
"removeUnusedImports": true,
"removeUnusedExports": true,
"scopeHoisting": true
}
@@ -1,20 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_renamed_input_index_19qb5ne.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/renamed/input/index.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
// MERGED MODULE: [project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/renamed/input/index.js [test] (ecmascript)
;
// MERGED MODULE: [project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/renamed/input/lib.js [test] (ecmascript)
;
// MERGED MODULE: [project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/renamed/input/origin.js [test] (ecmascript)
;
const x = 'X_USED_MARKER';
;
;
;
console.log(x);
__turbopack_context__.s([], "[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/renamed/input/index.js [test] (ecmascript)");
}),
]);
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_renamed_input_index_19qb5ne.js.map
@@ -1,6 +0,0 @@
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/renamed/input/origin.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/renamed/input/index.js"],"sourcesContent":["export const x = 'X_USED_MARKER'\n","import { y } from './lib.js'\n\nconsole.log(y)\n"],"names":["x"],"mappings":";;;;;;AAAO,MAAMA,IAAI;;;;ACEjB,QAAQ,GAAG,CAAC"}}]
}
@@ -1,5 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1i9t_crates_turbopack-tests_tests_snapshot_reexport-drop_renamed_input_index_03l_5yx.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_renamed_input_index_19qb5ne.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/renamed/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime
@@ -1,3 +0,0 @@
import { used } from './lib.js'
console.log(used)
@@ -1,2 +0,0 @@
export const used = 'USED'
export { unused } from './unused.js'
@@ -1 +0,0 @@
export const unused = 'UNUSED_MARKER'
@@ -1 +0,0 @@
{ "removeUnusedImports": true, "removeUnusedExports": true, "scopeHoisting": true }
@@ -1,17 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_static_input_index_01ttfgs.js",
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/static/input/index.js [test] (ecmascript)", ((__turbopack_context__) => {
"use strict";
// MERGED MODULE: [project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/static/input/index.js [test] (ecmascript)
;
// MERGED MODULE: [project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/static/input/lib.js [test] (ecmascript)
;
const used = 'USED';
;
;
console.log(used);
__turbopack_context__.s([], "[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/static/input/index.js [test] (ecmascript)");
}),
]);
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_static_input_index_01ttfgs.js.map
@@ -1,6 +0,0 @@
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/static/input/lib.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/static/input/index.js"],"sourcesContent":["export const used = 'USED'\nexport { unused } from './unused.js'\n","import { used } from './lib.js'\n\nconsole.log(used)\n"],"names":["used"],"mappings":";;;;AAAO,MAAMA,OAAO;;;ACEpB,QAAQ,GAAG,CAAC"}}]
}
@@ -1,5 +0,0 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1i9t_crates_turbopack-tests_tests_snapshot_reexport-drop_static_input_index_1noyz-u.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_reexport-drop_static_input_index_01ttfgs.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/reexport-drop/static/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime