sveltejs / svelte

web development for the rest of us
https://svelte.dev
MIT License
79.96k stars 4.25k forks source link

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

Open Rich-Harris opened 3 years ago

Rich-Harris commented 3 years ago

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: https://github.com/snowpackjs/snowpack/discussions/1941#discussioncomment-210899.

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.

benmccann commented 3 years ago

CC'ing our recent source mapping experts @dmitrage @milahu :smile:

milahu commented 3 years ago

could be easy as https://github.com/sveltejs/svelte/compare/master...milahu:patch-2

dmitrage commented 3 years ago

Is it how preprocess is expected to work then?

Example: preprocess map sources: ["index.svelte", "external.css"] compile input filename: file:///path/to/project/index.svelte result map sources: ["file:///path/to/project/index.svelte", "file:///path/to/project/external.css"]

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.