Repository: awslabs/llrt
File: llrt_core/src/modules/require/resolver.rs (Rust)

Code before:
        }
        // Check for platform(browser or node) field
        if let Some(BorrowedValue::String(platform)) = map.get(LLRT_PLATFORM.as_str()) {
            return Ok((platform.as_ref(), None, is_cjs));
        }
        // [ESM only] Check for module field
        if is_esm {
            if let Some(BorrowedValue::String(module)) = map.get("module") {
                return Ok((module.as_ref(), None, is_cjs));
            }
        }
        // Check for main field
        if let Some(BorrowedValue::String(main)) = map.get("main") {
            return Ok((main.as_ref(), None, is_cjs));
        }
    }
    Ok(("./index.js", None, true))
}

fn replace_star(scope: &str, name: &str) -> String {
    if scope.contains('*') {
        scope.replace("*", name)
    } else {
        scope.into()
    }
}

// Implementation equivalent to PACKAGE_IMPORTS_RESOLVE including RESOLVE_ESM_MATCH
fn package_imports_resolve<'a>(
    package_json: &'a BorrowedValue<'a>,
    modules_name: &str,
) -> Option<&'a str> {
    if let BorrowedValue::Object(map) = package_json {
        if let Some(BorrowedValue::Object(imports)) = map.get("imports") {
            if let Some(BorrowedValue::Object(name)) = imports.get(modules_name) {
                // Check for imports -> name -> platform(browser or node)
                if let Some(BorrowedValue::String(platform)) = name.get(LLRT_PLATFORM.as_str()) {
                    return Some(platform.as_ref());
                }
                // Check for imports -> name -> require
                if let Some(BorrowedValue::String(require)) = name.get("require") {
                    return Some(require.as_ref());
                }
                // Check for imports -> name -> module-sync
                if let Some(BorrowedValue::String(module_sync)) = name.get("module-sync") {
                    return Some(module_sync.as_ref());
                }
                // Check for imports -> name -> default
                if let Some(BorrowedValue::String(default)) = name.get("default") {
                    return Some(default.as_ref());
                }

Review comment (suggestion):
We can just call replace since it won't do any thing if there is no "*" and we still need to search in the entire string

```rust
fn replace_star(scope: &str, name: &str) -> String {
    scope.replace('*', name)
}
```

Code after:
        if is_esm {
            if let Some(BorrowedValue::String(module)) = map.get("module") {
                return Ok((module.as_ref(), None, is_cjs));
            }
        }
        // Check for main field
        if let Some(BorrowedValue::String(main)) = map.get("main") {
            return Ok((main.as_ref(), None, is_cjs));
        }
    }
    Ok(("./index.js", None, true))
}

fn replace_star(scope: &str, name: &str) -> String {
    scope.replace("*", name)
}

// Implementation equivalent to PACKAGE_IMPORTS_RESOLVE including RESOLVE_ESM_MATCH
fn package_imports_resolve<'a>(
    package_json: &'a BorrowedValue<'a>,
    modules_name: &str,
) -> Option<&'a str> {
    if let BorrowedValue::Object(map) = package_json {
        if let Some(BorrowedValue::Object(imports)) = map.get("imports") {
            if let Some(BorrowedValue::Object(name)) = imports.get(modules_name) {
                // Check for imports -> name -> platform(browser or node)
                if let Some(BorrowedValue::String(platform)) = name.get(LLRT_PLATFORM.as_str()) {
                    return Some(platform.as_ref());
                }
                // Check for imports -> name -> require
                if let Some(BorrowedValue::String(require)) = name.get("require") {
                    return Some(require.as_ref());
                }
                // Check for imports -> name -> module-sync
                if let Some(BorrowedValue::String(module_sync)) = name.get("module-sync") {
                    return Some(module_sync.as_ref());
                }
                // Check for imports -> name -> default
                if let Some(BorrowedValue::String(default)) = name.get("default") {
                    return Some(default.as_ref());
                }
            }
            // Check for imports -> name
            if let Some(BorrowedValue::String(name)) = imports.get(modules_name) {
                return Some(name.as_ref());
            }
        }
    }
    None
}