Skip to content

sourceMapUrl fallback in ImporterResult creates bloated source map output with source contents as source urls #2712

@ntkme

Description

@ntkme

/// Returns the URL to use in the source map to refer to [canonicalUrl].
///
/// Returns [canonicalUrl] as-is if it hasn't been loaded by this cache.
Uri sourceMapUrl(Uri canonicalUrl) =>
_resultsCache[canonicalUrl]?.sourceMapUrl ?? canonicalUrl;

In ImportCache, the sourceMapUrl always fallback to canonicalUrl for null.

/// An absolute, browser-accessible URL indicating the resolved location of
/// the imported stylesheet.
///
/// This should be a `file:` URL if one is available, but an `http:` URL is
/// acceptable as well. If no URL is supplied, a `data:` URL is generated
/// automatically from [contents].
Uri get sourceMapUrl =>
_sourceMapUrl ?? Uri.dataFromString(contents, encoding: utf8);
final Uri? _sourceMapUrl;

However, in ImporterResult it already fallbacks to a data url for null, so in the ImportCache it will never actually fallback to the canonicalUrl.

This causes undesired bloats with sourceMapIncludeSources: true, where the source contents are included and the sourceMapUrl are encoded version of the exact same source contents. On the other hand, with sourceMapIncludeSources: false, the source map can still contain the source contents via data uri, despite being explicitly told to not expose contents.

The comments around the original code makes be believe the intention was to provide source map for source map url that's not file: protocol. However, modern browser can handle non-file: url in source map just fine, especially when source contents are present.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions