-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
feat: css sourcemap support during dev #7173
Conversation
56aaaee
to
3e54315
Compare
const newContent = await additionalData(source, filename) | ||
const ms = new MagicString(source) | ||
ms.overwrite(0, source.length, newContent) | ||
|
||
return { | ||
content: ms.toString(), | ||
map: generateWithAbsoluteFilenameMap(ms, filename) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe additionalData
should be able to return sourcemap?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that makes sense. For this code, does using MagicString to replace its entire content produce a useful sourcemap? I've not tested this myself, but am curious about this behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this code, does using MagicString to replace its entire content produce a useful sourcemap?
No. It did not produce useful sourcemaps. I was going to remove this after having some advice.
// hack for parse broken with normalized absolute paths on windows (C:/path/to/something). | ||
// escape them to linux like paths | ||
sourcemapList.forEach((sourcemap) => { | ||
sourcemap.sources = sourcemap.sources.map((source) => | ||
source ? escapeToLinuxLikePath(source) : null | ||
) | ||
if (sourcemap.sourceRoot) { | ||
sourcemap.sourceRoot = escapeToLinuxLikePath(sourcemap.sourceRoot) | ||
} | ||
}) | ||
const escapedFilename = escapeToLinuxLikePath(filename) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, paths used with sourcemap (sources
/file
/sourceRoot
properties) are absolute paths which are obtained from id (/home/user/path/to/something.js
or on windows c:/users/user/path/to/something.js
).
There is a problem on windows with this.
const resolve = require('@jridgewell/resolve-uri')
const resolved = resolve('C:/project/foo.sass', 'C:/project/bar.sass')
console.log(resolved) // C:/project/C:/project/foo.sass
To solve this I escaped them like /c/project/foo.sass
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just trying to understand our options here. Could these paths be made relative to avoid the hack?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is a file importing a file from a different drive, it would not be able to express in relative paths.
This can happen in the following situation:
C:/project/src/foo.css
importsC:/project/node_modules/pkg/bar.scss
.preserveSymlinks
is falseC:/project/node_modules/pkg
is a symbolic link toD:/somewhere/pkg
It will not happen normally for pnpm since it does not create cross-drive links.
But if you use npm i C:/absolute/path/to/pkg
or yarn add link:C:/absolute/path/to/pkg
or pnpm link C:/absolute/path/to/pkg
, a symbolic link (on windows accurately a junction) will be created and this can be cross-drive.
Currently I do not have multiple drives in my computer so I did not try it, but it will happen in theory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also JS sourcemaps uses absolute path.
If CSS sourcemaps will use relative path, it would be better to use relative paths for JS sourcemaps for consistency.
JS sourcemap does not need this hack because in JS there is only one input file for one output.
/@jridgewell/resolve-uri/3.0.5: | ||
resolution: {integrity: sha512-VPeQ7+wH0itvQxnG+lIzWgkysKIr3L9sslimFW55rHMdGu/qCQ5z5h9zq4gI8uBtqkpHhsF4Z/OwExufUCThew==} | ||
engines: {node: '>=6.0.0'} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to include this.
jridgewell/resolve-uri@aa653a6
Sorry I did not get the meaning of "validating". |
see reproduction and discussion in #4939 |
This in incredible @sapphi-red, thanks for pushing this feat forward |
I'm thinking that we could enable css sourcemap support as experimental during build. See comment from @benmccann here: #2830 (comment) We could have @dominikg if you have some time to test or add a review for this PR, that would be helpful too. |
This PR only implements it for dev. It needs more work to be able to output sourcemaps for build. |
Tested it locally and it is working well for me. Disregard my previous comment as it doesn't need a new option, I think after some reviews we can merge it and get some testing during the beta. |
👍 for starting with dev-mode sourcemaps in the first iteration. build is a lot more involved, see #2830 (comment) i tried this branch with sveltekit demo app and the css sourcemap was a bit off when looking at it with https://github.com/evanw/source-map-visualization or trying to follow links from chome debugger to source files. This may very well be caused by svelte's css processing or vite-plugin-svelte not providing proper maps. (The virtual css module is a bit tricky) It did work for a vanilla vite project, though the map was kinda pointless there as the css was not minified so it was a 1-1 mapping. I also like the idea of taking this on in a beta, not really worried about sourcemaps being wrong sometimes, as long as this PR doesn't break anything else it is a vast improvement over no sourcemaps at all :) |
Merging this PR so we can test it during the beta. We'll release a new beta patch including it tomorrow. |
I tested this in the beta.6 version now and can confirm it works. Nice work. |
This comment was marked as outdated.
This comment was marked as outdated.
I tested beta.6,found an error, |
|
Description
Adds css sourcemap support with dev server.
This would be useful for debugging.
This PR supports:
<link>
import
additionalData
optionrelated #2830
close #4950
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).