-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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?
Changes from 8 commits
580175d
1538e6d
6ad0aa4
0106c50
9de9b61
ff1a92b
42c4389
5473b93
d2a9a5f
c187335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
## Usage | ||
|
||
```js | ||
// vite.config.js | ||
import cache from '@vitejs/plugin-persistent-cache' | ||
|
||
export default { | ||
plugins: [ | ||
cache({ | ||
// Options | ||
}), | ||
], | ||
} | ||
``` | ||
|
||
## Options |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: ['src/index'], | ||
clean: true, | ||
declaration: true, | ||
rollup: { | ||
emitCJS: true, | ||
inlineDependencies: true, | ||
esbuild: { | ||
target: 'node18', | ||
}, | ||
}, | ||
}) |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,53 @@ | ||||
{ | ||||
"name": "@vitejs/plugin-persistent-cache", | ||||
"version": "4.1.1", | ||||
"license": "MIT", | ||||
"author": "Guillaume Chau", | ||||
"files": [ | ||||
"dist" | ||||
], | ||||
"keywords": [ | ||||
"frontend", | ||||
"vite", | ||||
"vite-plugin", | ||||
"cache" | ||||
], | ||||
"main": "./dist/index.cjs", | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest adding |
||||
"module": "./dist/index.mjs", | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to include
Suggested change
|
||||
"types": "./dist/index.d.ts", | ||||
"exports": { | ||||
".": { | ||||
"types": "./dist/index.d.ts", | ||||
"import": "./dist/index.mjs", | ||||
"require": "./dist/index.cjs" | ||||
} | ||||
}, | ||||
"scripts": { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing we need to copy the |
||||
"dev": "unbuild --stub", | ||||
"build": "unbuild && pnpm run patch-cjs", | ||||
"patch-cjs": "tsx ../../scripts/patchCJS.ts", | ||||
"prepublishOnly": "npm run build" | ||||
}, | ||||
"engines": { | ||||
"node": "^18.0.0 || >=20.0.0" | ||||
}, | ||||
"repository": { | ||||
"type": "git", | ||||
"url": "git+https://github.com/vitejs/vite.git", | ||||
"directory": "packages/plugin-persistent-cache" | ||||
}, | ||||
"bugs": { | ||||
"url": "https://github.com/vitejs/vite/issues" | ||||
}, | ||||
"homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-persistent-cache#readme", | ||||
"funding": "https://github.com/vitejs/vite?sponsor=1", | ||||
"dependencies": {}, | ||||
"peerDependencies": { | ||||
"vite": "^4.0.0" | ||||
benmccann marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
}, | ||||
"devDependencies": { | ||||
"debug": "^4.3.4", | ||||
"picocolors": "^1.0.0", | ||||
"vite": "workspace:*" | ||||
} | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'node:fs' | ||
import { type ResolvedConfig, version } from 'vite' | ||
import type { ResolvedOptions } from './types.js' | ||
import { getCodeHash } from './utils.js' | ||
|
||
export async function computeCacheVersion( | ||
options: ResolvedOptions, | ||
config: ResolvedConfig, | ||
): Promise<string> { | ||
const hashedVersionFiles = await Promise.all( | ||
options.cacheVersionFromFiles.map((file) => { | ||
if (!fs.existsSync(file)) { | ||
throw new Error(`Persistent cache version file not found: ${file}`) | ||
} | ||
return fs.promises.readFile(file, 'utf-8') | ||
}), | ||
).then((codes) => getCodeHash(codes.join(''))) | ||
|
||
const defineHash = config.define | ||
? getCodeHash(JSON.stringify(config.define)) | ||
: '' | ||
|
||
const envHash = getCodeHash(JSON.stringify(config.env)) | ||
|
||
let configFileHash: string | undefined | ||
if (config.configFile) { | ||
const code = fs.readFileSync(config.configFile, 'utf-8') | ||
configFileHash = getCodeHash(code) | ||
} | ||
|
||
const cacheVersion = [ | ||
options.cacheVersion, | ||
`vite:${version}`, | ||
configFileHash, | ||
hashedVersionFiles, | ||
defineHash, | ||
envHash, | ||
] | ||
.filter(Boolean) | ||
.join('-') | ||
|
||
return cacheVersion | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const DEP_VERSION_RE = /[?&](v=[\w.-]+)\b/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import debug from 'debug' | ||
|
||
export const debugLog = debug('vite:persistent-cache') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import fs from 'node:fs' | ||
import type { DepOptimizationMetadata } from 'vite' | ||
import type { CacheManifest, DepsMetadataManager } from './types.js' | ||
import { debugLog } from './debug.js' | ||
|
||
interface UseDepsMetadataOptions { | ||
manifest: CacheManifest | ||
patchedDuringCurrentSession: Set<string> | ||
} | ||
|
||
export function useDepsMetadata({ | ||
manifest, | ||
patchedDuringCurrentSession, | ||
}: UseDepsMetadataOptions): DepsMetadataManager { | ||
// Optimized deps | ||
|
||
let depsMetadata: DepOptimizationMetadata['optimized'] | null = null | ||
|
||
async function updateDepsMetadata( | ||
metadata: DepOptimizationMetadata['optimized'], | ||
) { | ||
depsMetadata = metadata | ||
|
||
// Update existing cache files | ||
await Promise.all( | ||
Object.keys(manifest.modules).map(async (key) => { | ||
const entry = manifest.modules[key] | ||
if (entry?.fullData) { | ||
// Gather code changes | ||
const optimizedDeps: [string, string][] = [] | ||
for (const m of entry.fullData.importedModules) { | ||
for (const depId in metadata) { | ||
const dep = metadata[depId] | ||
if (dep.file === m.file) { | ||
optimizedDeps.push([ | ||
m.url, | ||
m.url.replace(/v=\w+/, `v=${dep.browserHash}`), | ||
]) | ||
m.id = m.id.replace(/v=\w+/, `v=${dep.browserHash}`) | ||
m.url = m.url.replace(/v=\w+/, `v=${dep.browserHash}`) | ||
break | ||
} | ||
} | ||
} | ||
// Apply code changes | ||
if (optimizedDeps.length) { | ||
let code = await fs.promises.readFile(entry.fileCode, 'utf8') | ||
patchedDuringCurrentSession.add(key) | ||
for (const [from, to] of optimizedDeps) { | ||
code = code.replaceAll(from, to) | ||
} | ||
await fs.promises.writeFile(entry.fileCode, code, 'utf8') | ||
debugLog( | ||
`Updated ${ | ||
entry.id | ||
} with new optimized deps imports: ${optimizedDeps | ||
.map(([from, to]) => `${from} -> ${to}`) | ||
.join(', ')}`, | ||
) | ||
} | ||
} | ||
}), | ||
) | ||
} | ||
|
||
return { | ||
getDepsMetadata: () => depsMetadata, | ||
updateDepsMetadata, | ||
} | ||
} |
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: