Skip to content

file:// URLs should be treated as absolute when populating sourcemap sources #5793

Open
@Rich-Harris

Description

@Rich-Harris

Describe the bug
If you pass filename and outputFilename options to svelte.compile, the sources property of the resulting sourcemap will contain a relative path from outputFilename to filename (and likewise for cssOutputFilename). This is the only thing outputFilename is used for.

sources is used inconsistently in the wild; it's often unclear whether it's supposed to be describing filepaths or URLs. We treat it as a an array of filepaths, but that sometimes yields unwanted results: FredKSchott/snowpack#1941 (reply in thread).

In the case where to is absolute, relative(from, to) === to should be true. file:// URLs are absolute, but we don't treat them as such.

To Reproduce

const { pathToFileURL } = require('url');
const { compile } = require('svelte/compiler');
const assert = require('assert');

const source = `<h1>Hello world!</h1>`;

const filename = pathToFileURL('index.svelte').href;
const outputFilename = '/components/index.svelte';

const result = compile(source, {
  filename,
  outputFilename
});

assert.deepEqual(
  result.js.map.sources,
  [filename]
);

Expected behavior
outputFilename should be disregarded, sources should equal [filename]. Instead we get ../../../file:///path/to/project/index.svelte.

Severity
Not critical — possible to workaround.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions