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

Broken source-maps #28

Closed
JannikGM opened this issue Jun 23, 2023 · 6 comments · Fixed by #29
Closed

Broken source-maps #28

JannikGM opened this issue Jun 23, 2023 · 6 comments · Fixed by #29

Comments

@JannikGM
Copy link

I believe this plugin corrupts source-maps.

Chrome devtools will not point at debugger statements; I believe this plugin is causing the misalignment in the source-maps.

See vite-plugin/vite-plugin-utils#8 for my speculation as to why that is.

@JannikGM
Copy link
Author

I've realized that we were using @originjs/vite-plugin-commonjs and not this repository.

However, I believe the argument still stands.

caoxiemeihao pushed a commit that referenced this issue Jun 24, 2023
@caoxiemeihao caoxiemeihao mentioned this issue Jun 24, 2023
@yejimeiming
Copy link
Member

Thanks for your feedback, I'm not sure exactly where the problem you describe is occurring.
I did currently find one that was breaking the sourcemap and I fixed it.


vite-plugin-commonjs will not break the line number of the source code, it will only insert the corresponding runtime code in the first line or after the source code.

https://github.com/vite-plugin/vite-plugin-commonjs/blob/v0.8.0/test/fixtures/src/dynamic.tsx
image

https://github.com/vite-plugin/vite-plugin-commonjs/blob/v0.8.0/test/fixtures/__snapshots__/dynamic.tsx
image

@JannikGM
Copy link
Author

That's quite a hack to avoid source-maps.

I'm not sure exactly where the problem you describe is occurring.

Yes, this report is invalid then.

As I was mentioning: I accidentally reported the bug here while I was using a different plugin with a similar name (vite-plugin/vite-plugin-commonjs vs. originjs/vite-plugins/vite-plugin-commonjs).
Initially I thought you had the same bug as the other plugin, but your hack (only putting things in first line after the comment + after original code) avoids the need for sourcemaps.

@vite-plugin vite-plugin deleted a comment from caoxiemeihao Jun 27, 2023
@yejimeiming
Copy link
Member

Yes, this report is invalid then.

Yeah! I'm not very clear on sourcemap, and I think blindly generating sourcemap would break it, because Vite has already converted the .js or .ts file once and generated sourcemap, so if I generate sourcemap again, which is essentially based on Vite's converted code, it's obviously wrong to do so.

So, I think we should keep the original sourcemap generated by Vite and not generate it again.

@JannikGM
Copy link
Author

[...] so if I generate sourcemap again, which is essentially based on Vite's converted code, it's obviously wrong to do so.

I've limited experience with this, but - I think - I have successfully patched that originjs plugin I mentioned to generate proper source-maps (because they add new lines, instead of your hack).

I believe, Vite expects plugins to return the transformed code + a new source-map for the transformed-code (vs. the code it gave you).
Vite will then merge your source-map, with the source-map of the code it gave you.

So if you modify code, then you should generate and return a source-map for your changes only; Vite appears to deal with the rest.

Your hack avoids the need for this in most cases (because you abuse that the first line is a comment).

@yejimeiming
Copy link
Member

yejimeiming commented Jun 27, 2023

caoxiemeihao pushed a commit that referenced this issue Jul 12, 2023
@yejimeiming yejimeiming mentioned this issue Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants