Skip to content

Commit 4fb7ad0

Browse files
authoredNov 25, 2022
feat(optimizer): support patch-package (#10286)
1 parent b4da791 commit 4fb7ad0

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed
 

‎docs/guide/dep-pre-bundling.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ Both `include` and `exclude` can be used to deal with this. If the dependency is
7777

7878
Vite caches the pre-bundled dependencies in `node_modules/.vite`. It determines whether it needs to re-run the pre-bundling step based on a few sources:
7979

80-
- The `dependencies` list in your `package.json`.
81-
- Package manager lockfiles, e.g. `package-lock.json`, `yarn.lock`, or `pnpm-lock.yaml`.
80+
- Package manager lockfile content, e.g. `package-lock.json`, `yarn.lock`, `pnpm-lock.yaml` or `bun.lockb`.
81+
- Patches folder modification time.
8282
- Relevant fields in your `vite.config.js`, if present.
83+
- `NODE_ENV` value.
8384

8485
The pre-bundling step will only need to be re-run when one of the above has changed.
8586

‎packages/vite/src/node/optimizer/index.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -1042,14 +1042,36 @@ function isSingleDefaultExport(exports: readonly string[]) {
10421042
}
10431043

10441044
const lockfileFormats = [
1045-
'package-lock.json',
1046-
'yarn.lock',
1047-
'pnpm-lock.yaml',
1048-
'bun.lockb'
1045+
{ name: 'package-lock.json', patchesDirs: ['patches'] }, // Default of https://github.com/ds300/patch-package
1046+
{ name: 'yarn.lock', patchesDirs: ['patches', '.yarn/patches'] }, // .yarn/patches for v2+
1047+
{ name: 'pnpm-lock.yaml', patchesDirs: [] }, // Included in lockfile
1048+
{ name: 'bun.lockb', patchesDirs: ['patches'] }
10491049
]
10501050

10511051
export function getDepHash(config: ResolvedConfig, ssr: boolean): string {
1052-
let content = lookupFile(config.root, lockfileFormats) || ''
1052+
const lockfilePath = lookupFile(
1053+
config.root,
1054+
lockfileFormats.map((l) => l.name),
1055+
{ pathOnly: true }
1056+
)
1057+
let content = lockfilePath ? fs.readFileSync(lockfilePath, 'utf-8') : ''
1058+
if (lockfilePath) {
1059+
const lockfileName = path.basename(lockfilePath)
1060+
const { patchesDirs } = lockfileFormats.find(
1061+
(f) => f.name === lockfileName
1062+
)!
1063+
const dependenciesRoot = path.dirname(lockfilePath)
1064+
for (const patchesDir of patchesDirs) {
1065+
const fullPath = path.join(dependenciesRoot, patchesDir)
1066+
if (fs.existsSync(fullPath)) {
1067+
const stats = fs.statSync(fullPath)
1068+
if (stats.isDirectory()) {
1069+
content += stats.mtimeMs.toString()
1070+
break
1071+
}
1072+
}
1073+
}
1074+
}
10531075
// also take config into account
10541076
// only a subset of config options that can affect dep optimization
10551077
const optimizeDeps = getDepOptimizationConfig(config, ssr)

0 commit comments

Comments
 (0)
Please sign in to comment.