Skip to content
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

Merged
merged 29 commits into from
Mar 21, 2022

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Mar 4, 2022

Description

Adds css sourcemap support with dev server.
This would be useful for debugging.

This PR supports:

  • CSS imported with <link>
  • CSS imported with import
  • Sass / less / Stylus
  • additionalData option
  • Vue SFC style tag

related #2830

close #4950

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

Comment on lines 1419 to 1426
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)
}
Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

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.

Comment on lines +565 to +575
// 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)
Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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 imports C:/project/node_modules/pkg/bar.scss.
  • preserveSymlinks is false
  • C:/project/node_modules/pkg is a symbolic link to D:/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.

Copy link
Member Author

@sapphi-red sapphi-red Mar 13, 2022

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.

Comment on lines +1979 to +1981
/@jridgewell/resolve-uri/3.0.5:
resolution: {integrity: sha512-VPeQ7+wH0itvQxnG+lIzWgkysKIr3L9sslimFW55rHMdGu/qCQ5z5h9zq4gI8uBtqkpHhsF4Z/OwExufUCThew==}
engines: {node: '>=6.0.0'}
Copy link
Member Author

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

@dominikg
Copy link
Contributor

dominikg commented Mar 4, 2022

whoa! thank you so much for working on this. ❤️

for the record: i did a naive take on this before and it had to be rolled back #4951 because it caused #4939

your code reads like it may take care of that already but still worth validating it doesn't hit that again

@sapphi-red
Copy link
Member Author

still worth validating

Sorry I did not get the meaning of "validating".
Do you mean that I should check my code? or add a runtime validation?

@dominikg
Copy link
Contributor

dominikg commented Mar 6, 2022

still worth validating

Sorry I did not get the meaning of "validating". Do you mean that I should check my code? or add a runtime validation?

see reproduction and discussion in #4939
Manual checking that this PR doesn't produce the error described there would be good, testcases that ensure it even better.
Runtime validation isn't needed i think, if we wanted to add that it could be done via debug logging to avoid permanent overhead

@Niputi Niputi added the p2-nice-to-have Not breaking anything but nice to have (priority) label Mar 6, 2022
@patak-dev
Copy link
Member

This in incredible @sapphi-red, thanks for pushing this feat forward

@patak-dev
Copy link
Member

I'm thinking that we could enable css sourcemap support as experimental during build. See comment from @benmccann here: #2830 (comment)

We could have build.cssSourcemap marked as @experimental, following other options like build.cssCodeSplit. This should let us merge this PR as part of the 2.9 beta and gather some real usage. We can then do a final call on the API for 3.0 (maybe all CSS-related options should be under a single css object?). Thoughts?

@dominikg if you have some time to test or add a review for this PR, that would be helpful too.

packages/plugin-vue/src/style.ts Outdated Show resolved Hide resolved
packages/vite/src/node/__tests__/plugins/css.spec.ts Outdated Show resolved Hide resolved
@sapphi-red
Copy link
Member Author

sapphi-red commented Mar 13, 2022

This PR only implements it for dev. It needs more work to be able to output sourcemaps for build.

@patak-dev patak-dev changed the title feat: css sourcemap with dev feat: css sourcemap support during dev Mar 13, 2022
@patak-dev
Copy link
Member

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.

@dominikg
Copy link
Contributor

dominikg commented Mar 13, 2022

@dominikg if you have some time to test or add a review for this PR, that would be helpful too.

👍 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 :)

@patak-dev
Copy link
Member

Merging this PR so we can test it during the beta. We'll release a new beta patch including it tomorrow.

@patak-dev patak-dev merged commit 38a655f into vitejs:main Mar 21, 2022
patak-dev added a commit that referenced this pull request Mar 21, 2022
@patak-dev patak-dev mentioned this pull request Mar 21, 2022
4 tasks
patak-dev added a commit that referenced this pull request Mar 21, 2022
@mbjorke
Copy link

mbjorke commented Mar 23, 2022

I tested this in the beta.6 version now and can confirm it works. Nice work.

@tyouzu1

This comment was marked as outdated.

@tyouzu1
Copy link
Contributor

tyouzu1 commented Mar 23, 2022

This PR only implements it for dev. It needs more work to be able to output sourcemaps for build.

I tested beta.6,found an error,
Read the less file if it is an empty file,result.map will be undefined,JSON.parse(result.map) will throw an error #7411
and submit a pr https://github.com/vitejs/vite/pull/7412/files

@tyouzu1
Copy link
Contributor

tyouzu1 commented Mar 23, 2022

This PR only implements it for dev. It needs more work to be able to output sourcemaps for build.

I tested beta.6,found an error, Read the less file if it is an empty file,result.map will be undefined,JSON.parse(result.map) will throw an error #7411 and submit a pr https://github.com/vitejs/vite/pull/7412/files

#7412

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: css p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

7 participants