Skip to content
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

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/plugin-persistent-cache/LICENSE
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.
18 changes: 18 additions & 0 deletions packages/plugin-persistent-cache/README.md
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)

Copy link
Collaborator

@benmccann benmccann Oct 29, 2023

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

## Usage

```js
// vite.config.js
import cache from '@vitejs/plugin-persistent-cache'

export default {
plugins: [
cache({
// Options
}),
],
}
```

## Options
14 changes: 14 additions & 0 deletions packages/plugin-persistent-cache/build.config.ts
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',
},
},
})
53 changes: 53 additions & 0 deletions packages/plugin-persistent-cache/package.json
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",
Copy link
Collaborator

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

"module": "./dist/index.mjs",
Copy link
Collaborator

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

Suggested change
"module": "./dist/index.mjs",

"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"scripts": {
Copy link
Collaborator

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

"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:*"
}
}
43 changes: 43 additions & 0 deletions packages/plugin-persistent-cache/src/cache-version.ts
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
}
1 change: 1 addition & 0 deletions packages/plugin-persistent-cache/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEP_VERSION_RE = /[?&](v=[\w.-]+)\b/
3 changes: 3 additions & 0 deletions packages/plugin-persistent-cache/src/debug.ts
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')
66 changes: 66 additions & 0 deletions packages/plugin-persistent-cache/src/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 | null = null

async function updateDepsMetadata(metadata: DepOptimizationMetadata) {
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.optimized) {
const dep = metadata.optimized[depId]
if (dep.file === m.file) {
optimizedDeps.push([
m.url,
m.url.replace(/v=\w+/, `v=${metadata.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,
}
}
151 changes: 151 additions & 0 deletions packages/plugin-persistent-cache/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import type { CacheTransformReadResult, Plugin } from 'vite'
import type {
DepsMetadataManager,
ManifestManager,
Options,
ResolvedOptions,
} from './types'
import { resolveOptions } from './options.js'
import { computeCacheVersion } from './cache-version.js'
import { useCacheManifest } from './manifest.js'
import { getCodeHash } from './utils.js'
import { DEP_VERSION_RE } from './const.js'
import { read } from './read.js'
import { write } from './write.js'
import { useDepsMetadata } from './deps.js'

function vitePersistentCachePlugin(pluginOptions: Options = {}): Plugin {
let resolvedOptions: ResolvedOptions
let cacheVersion: string
let manifestManager: ManifestManager
let depsMetadataManager: DepsMetadataManager

/**
* This is used to skip read for modules that were patched by the cache for future warm restarts
* in case the same module is red from persistent cache again during the same session.
* For example: rewriting optimized deps imports should be "reverted" for the current session
* as they will be incorrect otherwise (vite keeps the version query stable until next restart).
*/
const patchedDuringCurrentSession = new Set<string>()

return {
name: 'vite:persistent-cache',
apply: 'serve',

async configResolved(config) {
resolvedOptions = resolveOptions({
pluginOptions,
cacheDir: config.cacheDir,
root: config.root,
})

cacheVersion = await computeCacheVersion(resolvedOptions, config)

manifestManager = await useCacheManifest(
resolvedOptions.cacheDir,
cacheVersion,
)

depsMetadataManager = useDepsMetadata({
manifest: manifestManager.manifest,
patchedDuringCurrentSession,
})
},

serveLoadCacheGetKey(id, { file, url, ssr }) {
const isIncluded =
!file.includes(resolvedOptions.cacheDir) &&
// Don't cache vite client
!file.includes('vite/dist/client') &&
(!resolvedOptions?.exclude || !resolvedOptions.exclude(url))

if (isIncluded) {
return getCodeHash(id.replace(DEP_VERSION_RE, '')) + (ssr ? '-ssr' : '')
}

return null
},

async serveLoadCacheRead(cacheKey, options) {
return read({
key: cacheKey,
manifest: manifestManager.manifest,
patchedDuringCurrentSession,
})
},

async serveLoadCacheWrite(data) {
await write({
data,
resolvedOptions,
manifestManager,
depsMetadataManager,
patchedDuringCurrentSession,
})
manifestManager.queueManifestWrite()
},

serveTransformCacheGetKey(id, { code, ssr }) {
const isIncluded =
// Exclude glob matching so it's always re-evaluated
!code.includes('import.meta.glob')

if (isIncluded) {
return getCodeHash(id + code) + (ssr ? '-ssr' : '')
}

return null
},

async serveTransformCacheRead(cacheKey, options) {
const cached = await read({
key: cacheKey,
manifest: manifestManager.manifest,
patchedDuringCurrentSession,
})
if (cached) {
let result: CacheTransformReadResult = {
code: cached.code,
map: cached.map,
}

// Restore module graph node info for HMR
const entry = manifestManager.manifest.modules[cacheKey]
if (entry?.fullData) {
const importedBindings = new Map<string, Set<string>>()
for (const [key, value] of Object.entries(
entry.fullData.importedBindings,
)) {
importedBindings.set(key, new Set(value))
}

result = {
...result,
importedModules: new Set(
entry.fullData.importedModules.map(({ url }) => url),
),
importedBindings,
acceptedModules: new Set(entry.fullData.acceptedHmrDeps),
acceptedExports: new Set(entry.fullData.acceptedHmrExports),
isSelfAccepting: entry.fullData.isSelfAccepting,
}
}

return result
}
return null
},

async serveTransformCacheWrite(data) {
await write({
data,
resolvedOptions,
manifestManager,
depsMetadataManager,
patchedDuringCurrentSession,
})
},
}
}

export default vitePersistentCachePlugin