-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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: persistent cache plugin #14333
base: main
Are you sure you want to change the base?
Conversation
Run & review this pull request in StackBlitz Codeflow. |
We would still need a way to handle plugin's |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this new proposal which is very aligned with what @yyx990803 suggested.
High level review that requires approval from the rest of the team:
- The callbacks should be calculated in advanced which will be better for perf
- The key&read callbacks should be merged in one callback
- This simplify the code
- This don't impose a string key to the plugin authors
- This allow the key to be async
- Because the read cb is async, to avoid to many empty ticks if there is no "cache plugin", the plugin container should expose an optional cb
Isn't |
"cache" | ||
], | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to include module
. It will be ignored since you've defined exports
"module": "./dist/index.mjs", |
"vite-plugin", | ||
"cache" | ||
], | ||
"main": "./dist/index.cjs", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest adding "type": "module"
and only distributing an ESM version since this is a new plugin. Vite is working on dropping the CJS versions of its builds - they're deprecated in v5 and will be removed in v6
@@ -0,0 +1,18 @@ | |||
# @vitejs/plugin-persistent-cache [![npm](https://img.shields.io/npm/v/@vitejs/plugin-persistent-cache.svg)](https://npmjs.com/package/@vitejs/plugin-persistent-cache) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could use some more docs:
- what exactly is being cached?
- how is invalidation handled?
- i'm assuming it applies to the results of other plugins as well?
- does it apply in both dev and prod?
- can you control what directory the info is persisted in?
- is there anything you need to do to use this on GitHub Actions?
- the options filled out below
import fs from 'node:fs' | ||
import path from 'node:path' | ||
|
||
export function getCodeHash(code: string): string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could probably just be called getHash
. it will work regardless of whether the input is a "code" or not
return createHash('sha1').update(code).digest('hex') | ||
} | ||
|
||
export function tryStatSync(file: string): fs.Stats | undefined { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export function tryStatSync(file: string): fs.Stats | undefined { | |
function tryStatSync(file: string): fs.Stats | undefined { |
} | ||
} | ||
|
||
export function lookupFile( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export function lookupFile( | |
/** returns the first matching file path of the given file names */ | |
export function lookupFile( |
"require": "./dist/index.cjs" | ||
} | ||
}, | ||
"scripts": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing we need to copy the lint
, format
, and typecheck
from the main Vite package to be here as well
const loadResult = await pluginContainer.load(id, { ssr }) | ||
let loadResult: LoadResult | ||
|
||
const cacheLoadResult = await pluginContainer.serveLoadCacheRead({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like this happens in both dev and build in which case I don't think we should call it "serve" because it's a bit confusing with Vite's "serve" command (https://vitejs.dev/config/#conditional-config)
maybe we could call it something like getFromLoadCache
]) | ||
if (packageLockFile) { | ||
cacheVersionFromFiles.push(packageLockFile) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we throw an exception if no package lock file is found? I think that would only happen if the user were using a different package manager and the caching logic might not be safe in that case
I don't know if this would be considered out-of-scope, but it would be nice to have a way to cache generated assets, as it can be extremely expensive. E.g. there are requests for caching assets in JonasKruckenberg/imagetools#522, sveltejs/kit#11043, and zerodevx/svelte-img#35. In
And actually, I wonder if this plugin would break for projects with |
I don't think we should worry about supporting side effects generally. I'd imagine we could update However, I do think we should support the |
Description
Continuation of #10671
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).