Skip to content

Commit 483adb4

Browse files
committed
fix exports becoming undefined
1 parent 926336c commit 483adb4

1 file changed

Lines changed: 38 additions & 21 deletions

File tree

packages/start/src/config/fs-routes/tree-shake.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,58 +107,75 @@ function treeShakeTransform({ types: t }: typeof Babel): PluginObj<State> {
107107
});
108108
}
109109
},
110-
ExportDefaultDeclaration(exportNamedPath) {
111-
// if opts.keep is true, we don't remove the routeData export
110+
ExportDefaultDeclaration(exportNamedPath) {
112111
if (state.opts.pick && !state.opts.pick.includes("default")) {
113-
exportNamedPath.remove();
112+
const decl = exportNamedPath.get("declaration");
113+
if (decl.node) {
114+
// Keep the declaration, just remove the export
115+
exportNamedPath.replaceWith(decl.node);
116+
} else {
117+
exportNamedPath.remove();
118+
}
114119
}
115120
},
116121
ExportNamedDeclaration(exportNamedPath) {
117-
// if opts.keep is false, we don't remove the routeData export
118122
if (!state.opts.pick) {
119123
return;
120124
}
125+
121126
const specifiers = exportNamedPath.get("specifiers");
127+
128+
// Handle: export { foo, bar }
122129
if (specifiers.length) {
123-
specifiers.forEach(s => {
124-
if (
125-
t.isIdentifier(s.node.exported)
126-
? s.node.exported.name
127-
: state.opts.pick.includes(s.node.exported.value)
128-
) {
129-
s.remove();
130+
specifiers.forEach((s) => {
131+
const exportedName = t.isIdentifier(s.node.exported)
132+
? s.node.exported.name
133+
: s.node.exported.value;
134+
if (!state.opts.pick.includes(exportedName)) {
135+
s.remove(); // Remove from export list, but keep the local binding
130136
}
131137
});
132138
if (exportNamedPath.node.specifiers.length < 1) {
133-
exportNamedPath.remove();
139+
exportNamedPath.remove(); // Remove empty export statement
134140
}
135141
return;
136142
}
143+
137144
const decl = exportNamedPath.get("declaration");
138145
if (decl == null || decl.node == null) {
139146
return;
140147
}
148+
141149
switch (decl.node.type) {
142150
case "FunctionDeclaration": {
143151
const name = decl.node.id?.name;
144-
if (name && state.opts.pick && !state.opts.pick.includes(name)) {
145-
exportNamedPath.remove();
152+
if (name && !state.opts.pick.includes(name)) {
153+
// REPLACE export function foo() {} with function foo() {}
154+
// Don't remove - just remove the export!
155+
exportNamedPath.replaceWith(decl.node);
146156
}
147157
break;
148158
}
149159
case "VariableDeclaration": {
150-
const inner = decl.get("declarations") as Array<NodePath<any>>;
151-
inner.forEach(d => {
152-
if (d.node.id.type !== "Identifier") {
153-
return;
154-
}
160+
const inner = decl.get("declarations");
161+
inner.forEach((d) => {
162+
if (d.node.id.type !== "Identifier") return;
155163
const name = d.node.id.name;
156-
if (state.opts.pick && !state.opts.pick.includes(name)) {
157-
d.remove();
164+
if (!state.opts.pick.includes(name)) {
165+
// Keep the variable, just not exported
166+
// Replace export const foo = ... with const foo = ...
167+
exportNamedPath.replaceWith(decl.node);
158168
}
159169
});
160170
break;
161171
}
172+
case "ClassDeclaration": {
173+
const name = decl.node.id?.name;
174+
if (name && !state.opts.pick.includes(name)) {
175+
exportNamedPath.replaceWith(decl.node);
176+
}
177+
break;
178+
}
162179
default: {
163180
break;
164181
}

0 commit comments

Comments
 (0)