Repository: vercel/next.js
File: turbopack/crates/turbopack-ecmascript/src/source_map.rs (Rust)

Code before:
        data
    } else {
        return None;
    };

    data_encoding::BASE64
        .decode(data_b64.as_bytes())
        .ok()
        .map(Rope::from)
}

/// Extracts the sourceMappingURL from raw file content.
/// This searches for a comment at the end of the file (only followed by whitespace).
pub fn extract_source_mapping_url_from_content(file_content: &str) -> Option<&str> {
    // Find a matching comment at the end of the file (only followed by whitespace)
    static SOURCE_MAP_FILE_REFERENCE: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r"\n//[@#]\s*sourceMappingURL=(\S*)[\n\s]*$").unwrap());

    file_content.rfind("\n//").and_then(|start| {
        let line = &file_content[start..];
        SOURCE_MAP_FILE_REFERENCE
            .captures(line)
            .map(|m| m.get(1).unwrap().as_str())
    })
}

pub async fn parse_source_map_comment(
    source: ResolvedVc<Box<dyn Source>>,
    source_mapping_url: Option<&str>,
    origin_path: &FileSystemPath,
) -> Result<
    Option<(
        ResolvedVc<Box<dyn GenerateSourceMap>>,
        Option<ResolvedVc<Box<dyn ModuleReference>>>,
    )>,
> {
    if let Some(path) = source_mapping_url {
        static JSON_DATA_URL_BASE64: LazyLock<Regex> = LazyLock::new(|| {
            Regex::new(r"^data:application\/json;(?:charset=utf-8;)?base64").unwrap()
        });
        if path.ends_with(".map") {
            let source_map_origin = origin_path.parent().join(path)?;
            let reference = SourceMapReference::new(origin_path.clone(), source_map_origin)
                .to_resolved()
                .await?;
            return Ok(Some((
                ResolvedVc::upcast(reference),
                Some(ResolvedVc::upcast(reference)),
            )));
        } else if JSON_DATA_URL_BASE64.is_match(path) {
            return Ok(Some((

Review comment (refactor):
That's not completely correct. It's doesn't have to be the fall comment:

```
//# sourceMappingURL=...
//# sourceURL=...
// could be any extra comment too...
```

Here is the spec how to extract the comment: https://tc39.es/ecma426/#sec-linking-inline
But also see the `ISSUE 1` box here.

Code after:
        data
    } else {
        return None;
    };

    data_encoding::BASE64
        .decode(data_b64.as_bytes())
        .ok()
        .map(Rope::from)
}

/// Extracts the sourceMappingURL from raw file content.
/// This searches for a comment at the end of the file (only followed by whitespace).
pub fn extract_source_mapping_url_from_content(file_content: &str) -> Option<&str> {
    // TODO this should use https://tc39.es/ecma426/#sec-JavaScriptExtractSourceMapURL instead

    // Find a matching comment at the end of the file (only followed by whitespace)
    static SOURCE_MAP_FILE_REFERENCE: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r"\n//[@#]\s*sourceMappingURL=(\S*)[\n\s]*$").unwrap());

    file_content.rfind("\n//").and_then(|start| {
        let line = &file_content[start..];
        SOURCE_MAP_FILE_REFERENCE
            .captures(line)
            .map(|m| m.get(1).unwrap().as_str())
    })
}

pub async fn parse_source_map_comment(
    source: ResolvedVc<Box<dyn Source>>,
    source_mapping_url: Option<&str>,
    origin_path: &FileSystemPath,
) -> Result<
    Option<(
        ResolvedVc<Box<dyn GenerateSourceMap>>,
        Option<ResolvedVc<Box<dyn ModuleReference>>>,
    )>,
> {
    if let Some(path) = source_mapping_url {
        static JSON_DATA_URL_BASE64: LazyLock<Regex> = LazyLock::new(|| {
            Regex::new(r"^data:application\/json;(?:charset=utf-8;)?base64").unwrap()
        });
        if path.ends_with(".map") {
            let source_map_origin = origin_path.parent().join(path)?;
            let reference = SourceMapReference::new(origin_path.clone(), source_map_origin)
                .to_resolved()
                .await?;
            return Ok(Some((
                ResolvedVc::upcast(reference),
                Some(ResolvedVc::upcast(reference)),
            )));