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

Code before:
    }
}

// Implementation equivalent to PACKAGE_EXPORTS_RESOLVE including RESOLVE_ESM_MATCH
fn package_exports_resolve<'a>(
    package_json: &'a BorrowedValue<'a>,
    modules_name: &str,
    is_esm: bool,
) -> Result<(&'a str, Option<String>, bool)> {
    let ident = if is_esm { "import" } else { "require" };

    let modules_name = if modules_name != "." {
        &["./", modules_name].concat()
    } else {
        modules_name
    };

    let n = modules_name.bytes().filter(|&b| b == b'/').count();

    let wildcard = if n > 1 {
        let (_name, _scope, _) = get_name_and_scope(modules_name, n - 1);
        (Some(_name), Some([_scope, "/*"].concat()))
    } else {
        (None, None)
    };

    if let BorrowedValue::Object(map) = package_json {
        let is_cjs =
            !matches!(map.get("type"), Some(BorrowedValue::String(ref _type)) if _type == "module");

        if let Some(BorrowedValue::Object(exports)) = map.get("exports") {
            if let Some(BorrowedValue::Object(name)) = exports.get(modules_name) {
                // Check for exports -> name -> platform(browser or node) -> [import | require]
                if let Some(BorrowedValue::Object(platform)) = name.get(LLRT_PLATFORM.as_str()) {
                    if let Some(BorrowedValue::String(ident)) = platform.get(ident) {
                        return Ok((ident.as_ref(), None, is_cjs));
                    }
                }
                // Check for exports -> name -> [import | require] -> default
                if let Some(BorrowedValue::Object(ident)) = name.get(ident) {
                    if let Some(BorrowedValue::String(default)) = ident.get("default") {
                        return Ok((default.as_ref(), None, is_cjs));
                    }
                }
                // Check for exports -> name -> platform(browser or node)
                if let Some(BorrowedValue::String(platform)) = name.get(LLRT_PLATFORM.as_str()) {
                    return Ok((platform.as_ref(), None, is_cjs));
                }
                // Check for exports -> name -> [import | require]
                if let Some(BorrowedValue::String(ident)) = name.get(ident) {
                    return Ok((ident.as_ref(), None, is_cjs));

Review comment (suggestion):
Maybe better like this?

```rust
let wildcard  = if let Some(pos) = modules_name.rmatch_indices('/').nth(1) {
    let (name, mut scope, _) = get_name_and_scope(modules_name, pos.0);
    
    // reuse the existing scope string allocation
    scope.push_str("/*"); 
    
    (Some(name), Some(scope))
} else {
    (None, None);
}
```

Code after:
    }
}

// Implementation equivalent to PACKAGE_EXPORTS_RESOLVE including RESOLVE_ESM_MATCH
fn package_exports_resolve<'a>(
    package_json: &'a BorrowedValue<'a>,
    modules_name: &str,
    is_esm: bool,
) -> Result<(&'a str, Option<String>, bool)> {
    let ident = if is_esm { "import" } else { "require" };

    let modules_name = if modules_name != "." {
        &["./", modules_name].concat()
    } else {
        modules_name
    };

    let wildcard = if let Some(pos) = modules_name.rmatch_indices('/').nth(1) {
        let (name, scope, _) = get_name_and_scope(modules_name, pos.0);
        (Some(name), Some([scope, "/*"].concat()))
    } else {
        (None, None)
    };

    if let BorrowedValue::Object(map) = package_json {
        let is_cjs =
            !matches!(map.get("type"), Some(BorrowedValue::String(ref _type)) if _type == "module");

        if let Some(BorrowedValue::Object(exports)) = map.get("exports") {
            if let Some(BorrowedValue::Object(name)) = exports.get(modules_name) {
                // Check for exports -> name -> platform(browser or node) -> [import | require]
                if let Some(BorrowedValue::Object(platform)) = name.get(LLRT_PLATFORM.as_str()) {
                    if let Some(BorrowedValue::String(ident)) = platform.get(ident) {
                        return Ok((ident.as_ref(), None, is_cjs));
                    }
                }
                // Check for exports -> name -> [import | require] -> default
                if let Some(BorrowedValue::Object(ident)) = name.get(ident) {
                    if let Some(BorrowedValue::String(default)) = ident.get("default") {
                        return Ok((default.as_ref(), None, is_cjs));
                    }
                }
                // Check for exports -> name -> platform(browser or node)
                if let Some(BorrowedValue::String(platform)) = name.get(LLRT_PLATFORM.as_str()) {
                    return Ok((platform.as_ref(), None, is_cjs));
                }
                // Check for exports -> name -> [import | require]
                if let Some(BorrowedValue::String(ident)) = name.get(ident) {
                    return Ok((ident.as_ref(), None, is_cjs));
                }
                // Check for exports -> name -> default