getComponentFromModule
Result<
Get a component from a loaded module.
Handles:
- Named exports:
module.ComponentName - Default exports:
module.defaultormodule.default.ComponentName - Nested paths:
module.Stack.Navigatorvia dot notation
Implementation
Result<JSAny, String> getComponentFromModule(
JSObject module,
String componentPath,
) {
// Handle nested paths like "Stack.Navigator"
final parts = componentPath.split('.');
JSAny? current = module;
for (final part in parts) {
if (current == null) {
return Error('Component path $componentPath not found (null at $part)');
}
final currentObj = current as JSObject;
// Try direct access first
final direct = currentObj[part];
if (direct != null) {
current = direct;
continue;
}
// For the first part, try via default export
if (part == parts.first) {
final defaultExport = currentObj['default'];
if (defaultExport != null) {
final defaultObj = defaultExport as JSObject;
final viaDefault = defaultObj[part];
if (viaDefault != null) {
current = viaDefault;
continue;
}
// If asking for 'default' specifically, return the default export
if (part == 'default') {
current = defaultExport;
continue;
}
}
}
return Error('Component $part not found in module (path: $componentPath)');
}
return switch (current) {
null => Error('Component $componentPath resolved to null'),
final JSAny c => Success(c),
};
}