fix: autoCodeSplitting` with destructured export#6656
Conversation
📝 WalkthroughWalkthroughThe PR adds destructuring-aware code splitting to the router-plugin compiler, implementing tracking of original binding names, identifier collection from destructuring patterns (ObjectPattern, ArrayPattern), and correct import/export specifier generation for destructured bindings across split modules. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
⚔️ Resolve merge conflicts (beta)
Comment |
|
| Command | Status | Duration | Result |
|---|---|---|---|
nx affected --targets=test:eslint,test:unit,tes... |
❌ Failed | 11m 12s | View ↗ |
nx run-many --target=build --exclude=examples/*... |
✅ Succeeded | 32s | View ↗ |
☁️ Nx Cloud last updated this comment at 2026-02-14 01:17:40 UTC
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/router-plugin/src/core/code-splitter/compilers.ts (1)
637-648:⚠️ Potential issue | 🟡 MinorAdd ArrayPattern support for split bindings.
Variable declarators only handle
IdentifierandObjectPattern. Array destructuring patterns (e.g.,const [Component] = createBits()) will trigger the unexpected-type error. This is inconsistent with other parts of the code (e.g.,hasExport()at line 1095–1097 handles both patterns). Addt.isArrayPatternhandling alongsideObjectPattern, preserving positional elements.Additionally, update
removeIdentifierLiteral()to handle array-destructured bindings so that matched elements are removed without shifting indices.💡 Suggested patch
} else if (t.isVariableDeclarator(splitNode)) { if (t.isIdentifier(splitNode.id)) { splitMeta.localExporterIdent = splitNode.id.name splitMeta.shouldRemoveNode = false } else if (t.isObjectPattern(splitNode.id)) { // Destructured binding like `const { component: MyComp } = createBits()` // Use the original identifier name that was tracked before resolving if (originalIdentName) { splitMeta.localExporterIdent = originalIdentName } splitMeta.shouldRemoveNode = false + } else if (t.isArrayPattern(splitNode.id)) { + if (originalIdentName) { + splitMeta.localExporterIdent = originalIdentName + } + splitMeta.shouldRemoveNode = false } else { throw new Error( `Unexpected splitNode type ☝️: ${splitNode.type}`, ) } }

fixes #4787
Summary by CodeRabbit
New Features
Tests