[turbopack] Tree shake w/ bar['foo'] syntax for star imports (#96319)

This adds tree-shaking support for this syntax:

```javascript
import * as ns from './lib';
ns["foo"]
```

Instead of expanding the usage to `All` for this one, we can restrict
ourself to just "foo".

See attached fixture.
This commit is contained in:
Sam Poder
2026-07-29 10:42:08 -07:00
committed by GitHub
parent 91c6309c52
commit fec0a517fc
8 changed files with 31 additions and 1 deletions
@@ -1336,7 +1336,12 @@ impl Visit for Analyzer<'_> {
if matches!(
&node.prop,
MemberProp::Ident(..) | MemberProp::PrivateName(..)
MemberProp::Ident(..)
| MemberProp::PrivateName(..)
| MemberProp::Computed(ComputedPropName {
expr: box Expr::Lit(Lit::Str(_)),
..
})
) && let Expr::Ident(ident) = &*node.obj
{
// Intentionally skipping over visit_expr(node.obj) here so that it doesn't get added to
@@ -0,0 +1,4 @@
import * as lit from './lit.js'
// A string-literal key narrows to `foo`, so `unused` leaves the bundle.
console.log(lit['foo'])
@@ -0,0 +1,2 @@
export function foo() {}
export function unused() {}
@@ -0,0 +1,11 @@
{
"scopeHoisting": false,
"removeUnusedImports": true,
"removeUnusedExports": true,
"followReexports": true,
"minifyType": {
"Minify": {
"mangle": "optimal-size"
}
}
}
@@ -0,0 +1,3 @@
(globalThis.TURBOPACK||(globalThis.TURBOPACK=[])).push(["output/1ece_tests_snapshot_remove-unused-imports_star-import-string-key_input_index_1fhdw9l.js",{otherChunks:["output/1jsg_tests_snapshot_remove-unused-imports_star-import-string-key_input_03-1fx5._.js"],runtimeModuleIds:["[project]/turbopack/crates/turbopack-tests/tests/snapshot/remove-unused-imports/star-import-string-key/input/index.js [test] (ecmascript)"]}]);
//# sourceMappingURL=1jsg_tests_snapshot_remove-unused-imports_star-import-string-key_input_index_1fhdw9l.js.map
@@ -0,0 +1,3 @@
(globalThis.TURBOPACK||(globalThis.TURBOPACK=[])).push(["output/1jsg_tests_snapshot_remove-unused-imports_star-import-string-key_input_03-1fx5._.js","[project]/turbopack/crates/turbopack-tests/tests/snapshot/remove-unused-imports/star-import-string-key/input/index.js [test] (ecmascript)",t=>{"use strict";console.log(t.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/remove-unused-imports/star-import-string-key/input/lit.js [test] (ecmascript)").foo),t.s([])},"[project]/turbopack/crates/turbopack-tests/tests/snapshot/remove-unused-imports/star-import-string-key/input/lit.js [test] (ecmascript)",t=>{"use strict";t.s(["foo",0,function(){}])}]);
//# sourceMappingURL=1jsg_tests_snapshot_remove-unused-imports_star-import-string-key_input_03-1fx5._.js.map
@@ -0,0 +1 @@
{"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/remove-unused-imports/star-import-string-key/input/index.js","turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/remove-unused-imports/star-import-string-key/input/lit.js"],"sourcesContent":["import * as lit from './lit.js'\n\n// A string-literal key narrows to `foo`, so `unused` leaves the bundle.\nconsole.log(lit['foo'])\n","export function foo() {}\nexport function unused() {}\n"],"names":["console","log","foo","unused"],"mappings":"kTAGAA,QAAQC,GAAG,CAHX,AAGY,EAHZ,CAAA,CAAA,2IAGY,GAAU,mLCHf,SAASC,EAAO"}