Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Rich-Harris opened this issue Dec 15, 2020 · 5 comments

Comments

@Rich-Harris
Copy link
Member

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.

@benmccann
Copy link
Member

CC'ing our recent source mapping experts @dmitrage @milahu 😄

@milahu
Copy link
Contributor

milahu commented Dec 16, 2020

could be easy as master...milahu:patch-2

@dmitrage
Copy link
Contributor

Is it how preprocess is expected to work then?

  • We continue passing filename (not file:// url) to preprocess
  • It generates source map with sources relative to component source file location (basename for component itself)
  • When combining (apply_preprocessor_sourcemap), we resolve sources to file:// form relative to passed url

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
Copy link

stale bot commented Jun 26, 2021

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 stale bot added the stale-bot label Jun 26, 2021
@stale stale bot removed the stale-bot label Jun 26, 2021
@stale stale bot removed the stale-bot label Jun 27, 2021
@stale
Copy link

stale bot commented Dec 24, 2021

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 stale bot added the stale-bot label Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants