Skip to content

fix(optimizer): preserve sourcemaps for transformed optimized deps with follow-up transforms#22428

Open
Andarist wants to merge 6 commits into
vitejs:mainfrom
Andarist:fix/source-map-propagation
Open

fix(optimizer): preserve sourcemaps for transformed optimized deps with follow-up transforms#22428
Andarist wants to merge 6 commits into
vitejs:mainfrom
Andarist:fix/source-map-propagation

Conversation

@Andarist
Copy link
Copy Markdown
Contributor

fixes #13560

image

On the screenshot above, we can see that root fails to be evaluated to the correct identifier (because it can't be properly mapped). I stumbled upon this in a previous version of Vite that was using esbuild and that was always renaming original identifiers that were shadowing some outer bindings (esbuild playground):

// input
export let root = { outer: 1 };

export function test() {
  let root = { inner: 2 };
  return root;
}

// output
export let root = { outer: 1 };
export function test() {
  let root2 = { inner: 2 };
  return root2;
}

In this PR, I had to figure out some oxc-based builtin transform that would be used by the optimizer that would actually rename some original binding (that's why the tests in this PR don't rely on the above example).

Comment on lines +87 to +90
fsp
.readFile(`${file}.map`, 'utf-8')
.then((map) => JSON.parse(map))
.catch(() => null),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is somewhat defensive... but I wasn't sure if you'd prefer a hard crash on invalid data here or how this should otherwise behave

])
if (map) {
return {
code: code.replace(/^\/\/# sourceMappingURL=.*$/m, ''),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required for the added Babel-based test. When it sees the sourceMappingURL comment it actually grabs that map and does something with the information contained in it. That then somehow conflicts with the Rolldown's sourcemap chaining. I think that mechanism is preferred (transform hook itself doesn't give users access to the sourcemap and the user is not requested to pass the input source map to the performed transform and handle that on their own) - so I think it makes sense to just strip it here to avoid issues with doubly-processed source maps.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can set sourcemap: 'hidden' here.


That should remove the comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I pushed out this change.

@Andarist Andarist force-pushed the fix/source-map-propagation branch 2 times, most recently from 1a1da2b to cd9e7e8 Compare May 11, 2026 10:15
@Andarist Andarist force-pushed the fix/source-map-propagation branch from cd9e7e8 to 5b3a987 Compare May 11, 2026 10:23
@sapphi-red sapphi-red added p2-nice-to-have Not breaking anything but nice to have (priority) feat: deps optimizer Esbuild Dependencies Optimization labels May 21, 2026
])
if (map) {
return {
code: code.replace(/^\/\/# sourceMappingURL=.*$/m, ''),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can set sourcemap: 'hidden' here.


That should remove the comment.

Comment thread packages/vite/src/node/utils.ts Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we merge this to playground/js-sourcemap/vite.config.js?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I specifically wanted to isolate it because it relies on specific rolldownOptions. I can merge it back into that config but I felt it's kinda harder to grasp what is actually needed for which test there, as a bunch of different tests were running against that "catch-all" config that had to handle all of them at once.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But ofc if you want to keep them together - I can move this into that existing config

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to be merged for now. The relationship between the tests and the option is not complex. I think we should have a comment on rolldownOptions.transform.target anyways because it's unclear why that is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I got them merged.

@Andarist
Copy link
Copy Markdown
Contributor Author

Andarist commented May 21, 2026

@sapphi-red I'm confused as to why zizmor would fail here. I'm not even touching any workflow files and I synced this with the main branch.

@Andarist Andarist requested a review from sapphi-red May 21, 2026 06:38
@sapphi-red
Copy link
Copy Markdown
Member

You can ignore the zizmor failure. It is failing because the action we are using was taken down temporary due to a recent incident (just to clarify we were / are not affected).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat: deps optimizer Esbuild Dependencies Optimization p2-nice-to-have Not breaking anything but nice to have (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sourcemap support when optimizing dependencies

2 participants