-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Add preTransform
and postTransform
hooks
#10943
Comments
Persistent caching also needs wrapping around |
Should I try to change the PR to core changes + a (temporary?) builtin plugin? |
@Akryum I have been thinking about Alec's proposal and I believe there is merit to it. The cache isn't free either (regarding disk space for example, and complexity as mentioned). We have a team meeting tomorrow, and we'll try to get to this one. Maybe it is better to wait a bit before doing the work. It would be useful though to sketch what the wrapping around |
I'm likely not understanding the core issue, but wouldn't wrapping They are hacky, but it reduces the API surface layer. vite-plugin-inspect does similar too. These hooks seem to only solve a specific problem so I don't think it's worth adding them yet, especially that we already have |
I don't think it does, because you have access to the
Not for |
There could be multiple plugins using |
While we can't avoid ordering mistakes entirely, it's a lot less likely with First of all, implementing If implementing |
We could rename these Alternatively, we could add a server: {
transformCache: {
read(id, { file, ssr, mode, loadResult, ...etc }) {
},
write(id, transformResult, { file, originalCode, ...etc }) {
}
}
} This would make it clear that only one cache implementation should exist. |
@aleclarson we discussed this proposal in today's team meeting and the general consensus was that we should avoid adding new options or hooks at this point. Remote caching in particular isn't a strong enough reason given how fast rebuilding the cache is compared to download times. We still think that it is a good idea though to be prepared in the future to review this decision and your proposal of abstracting the cache logic as a plugin or module is a good idea also for maintainability. Let's go back to discussing with @Akryum in #10671 how to be able to merge it so we can offer this option in Vite 4, and it is as maintainable as possible moving forward. |
Then why does Turborepo have remote caching? 🤔 |
@aleclarson I think some level of remote caching is good, for example for build artifacts that are costly. We discussed this with @juristr not long ago, maybe a good strategy is to start recommending Nx+Vite. Nx is working on better integration with Vite for their next version. |
It seems that Nx would need the Also, it seems potentially error-prone to have an internal local cache and an external Nx-based cache both being active at the same time. |
I think we would use Nx for build artifacts and maybe pre-bundled dependencies. That may be a good target that is costly to generate so you could get them from a remote cache. But for each individual module cache in your own source, it may be more performant to generate them than to download them from a remote cache. |
Let's close this issue. I don't think we should tackle caching with this API until it is more clear how rolldown and vite will end up integrated. |
Description
I believe it would be best for Vite if #10671 was implemented as a plugin. It would keep inevitable bug reports out of the core repo. It's up to the team to decide if they want to maintain it or let the community handle things. It doesn't matter to me, as long as Vite is able to support other cache implementations. For example, someone might write a remote caching plugin or something with more granular invalidation.
To support #10671 as a plugin, we need some new plugin hooks.
Suggested solution
preTransform
This hook would be called before the following code:
vite/packages/vite/src/node/server/transformRequest.ts
Lines 237 to 242 in 18c71dc
The first plugin to return a
TransformResult
object should end the plugin loop and prevent Vite'stransform
pipeline from running for that file.The hook would receive the following information:
id: string
file: string
(the result ofcleanUrl(id)
)ssr: boolean
Since some plugins may run for SSR only
mode: string
Since some plugins may run for development/production only
loadResult: Readonly<LoadResult> | null
This will be null if no
load
hook returned aLoadResult
so the code was loaded from disk.code: string
map: Readonly<SourceMap> | null
postTransform
This hook would be called before the following code:
vite/packages/vite/src/node/server/transformRequest.ts
Line 281 in 18c71dc
All plugins with a
postTransform
hook will be called (in parallel?). The return value is ignored.The hook would receive everything that
preTransform
does plus the following:transformResult: Readonly<TransformResult>
originalCode: string
The code before
transform
hooks were applied.originalMap: Readonly<SourceMap> | null
The sourcemap before
transform
hooks were applied.Additional context
Note that these hooks will only run for
transformRequest
calls (at least for now).Validations
The text was updated successfully, but these errors were encountered: