Skip to content

Conversation

@sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Mar 7, 2025

Description

Built on top of #19586

This PR implements hook filters that are supported by rolldown.
It does not improve performance unless rolldown is used, but this would improve performance when we migrate to rolldown and would reduce the diff between rolldown powered Vite and rollup powered Vite.

@sapphi-red sapphi-red added the p2-nice-to-have Not breaking anything but nice to have (priority) label Mar 7, 2025
@sapphi-red sapphi-red added this to the 6.3 milestone Mar 7, 2025
@sapphi-red
Copy link
Member Author

/ecosystem-ci run

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 19, 2025

Open in Stackblitz

npm i https://pkg.pr.new/vite@19602

commit: 32c1283

@vite-ecosystem-ci

This comment was marked as outdated.

@sapphi-red sapphi-red marked this pull request as ready for review March 24, 2025 05:55
const cwd = process.cwd()
return f
? (id) => {
const normalizedId = normalizePath(path.relative(cwd, id))
Copy link
Member

Choose a reason for hiding this comment

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

what happens with virtual ids here? Won't the virtual id end up mingled with the cwd path?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is passed as-is, probably because path.isAbsolute(id) returns false.
I've added a test for virtual ids 👍 : c3b3f70

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that's right. And it works fine for virtual:module too.

One issue we could have is that some virtual module ids start with a slash. For example, our own /@react-refresh from the plugin react. In this case, you'll end up with something like ../../../@react-refresh and the filter will fail.

Maybe it is fine because when someone adds a filter it is expected that the plugin won't be handling virtual modules and only get paths ids? And given that the relative path is below cwd, they all end up reject (by chance, it feels very hacky but it works).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, virtual modules starting with a slash would be normalized unintentionally. This was happening with createFilter from pluginutils, so I assume it won't be a problem.
https://github.com/rollup/plugins/blob/master/packages/pluginutils/src/createFilter.ts

Comment on lines +1176 to +1178
if (idFilter && !idFilter(id)) {
return
}
Copy link
Member

Choose a reason for hiding this comment

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

nice that you can do this without performance penalty over the current environment injection

@sapphi-red
Copy link
Member Author

I noticed there's a slight difference with rolldown and pluginutils.
To fix that I moved the normalization logic: 5304df3, 01e0593

@sapphi-red
Copy link
Member Author

Added one more change to align with pluginutils: 32c1283

github-merge-queue bot pushed a commit to rolldown/rolldown that referenced this pull request Mar 25, 2025
<!-- Thank you for contributing! -->

### Description

Ported tests from vitejs/vite#19602 to ensure
the behavior is consistent.
@patak-dev patak-dev merged commit 04d58b4 into vitejs:main Mar 25, 2025
16 checks passed
@sapphi-red sapphi-red deleted the feat/implement-hook-filters branch March 25, 2025 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p2-nice-to-have Not breaking anything but nice to have (priority) trigger: preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants