Skip to content

Commit 818cf3e

Browse files
authored
fix(optimizer): detect npm / yarn / pnpm dependency changes correctly (#17336) (#18560)
1 parent 6ff8009 commit 818cf3e

File tree

1 file changed

+38
-11
lines changed
  • packages/vite/src/node/optimizer

1 file changed

+38
-11
lines changed

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,14 +1150,40 @@ function isSingleDefaultExport(exports: readonly string[]) {
11501150
}
11511151

11521152
const lockfileFormats = [
1153-
{ name: 'package-lock.json', checkPatches: true, manager: 'npm' },
1154-
{ name: 'yarn.lock', checkPatches: true, manager: 'yarn' }, // Included in lockfile for v2+
1155-
{ name: 'pnpm-lock.yaml', checkPatches: false, manager: 'pnpm' }, // Included in lockfile
1156-
{ name: 'bun.lockb', checkPatches: true, manager: 'bun' },
1153+
{
1154+
path: 'node_modules/.package-lock.json',
1155+
checkPatches: true,
1156+
manager: 'npm',
1157+
},
1158+
{
1159+
// Yarn non-PnP
1160+
path: 'node_modules/.yarn-state.yml',
1161+
checkPatches: false,
1162+
manager: 'yarn',
1163+
},
1164+
{
1165+
// Yarn PnP
1166+
path: '.yarn/install-state',
1167+
checkPatches: false,
1168+
manager: 'yarn',
1169+
},
1170+
{
1171+
// yarn 1
1172+
path: 'node_modules/.yarn-integrity',
1173+
checkPatches: true,
1174+
manager: 'yarn',
1175+
},
1176+
{
1177+
path: 'node_modules/.pnpm/lock.yaml',
1178+
// Included in lockfile
1179+
checkPatches: false,
1180+
manager: 'pnpm',
1181+
},
1182+
{ name: 'bun.lockb', path: 'bun.lockb', checkPatches: true, manager: 'bun' },
11571183
].sort((_, { manager }) => {
11581184
return process.env.npm_config_user_agent?.startsWith(manager) ? 1 : -1
11591185
})
1160-
const lockfileNames = lockfileFormats.map((l) => l.name)
1186+
const lockfilePaths = lockfileFormats.map((l) => l.path)
11611187

11621188
function getConfigHash(environment: Environment): string {
11631189
// Take config into account
@@ -1195,16 +1221,17 @@ function getConfigHash(environment: Environment): string {
11951221
}
11961222

11971223
function getLockfileHash(environment: Environment): string {
1198-
const lockfilePath = lookupFile(environment.config.root, lockfileNames)
1224+
const lockfilePath = lookupFile(environment.config.root, lockfilePaths)
11991225
let content = lockfilePath ? fs.readFileSync(lockfilePath, 'utf-8') : ''
12001226
if (lockfilePath) {
1201-
const lockfileName = path.basename(lockfilePath)
1202-
const { checkPatches } = lockfileFormats.find(
1203-
(f) => f.name === lockfileName,
1227+
const normalizedLockfilePath = lockfilePath.replaceAll('\\', '/')
1228+
const lockfileFormat = lockfileFormats.find((f) =>
1229+
normalizedLockfilePath.endsWith(f.path),
12041230
)!
1205-
if (checkPatches) {
1231+
if (lockfileFormat.checkPatches) {
12061232
// Default of https://github.com/ds300/patch-package
1207-
const fullPath = path.join(path.dirname(lockfilePath), 'patches')
1233+
const baseDir = lockfilePath.slice(0, -lockfileFormat.path.length)
1234+
const fullPath = path.join(baseDir, 'patches')
12081235
const stat = tryStatSync(fullPath)
12091236
if (stat?.isDirectory()) {
12101237
content += stat.mtimeMs.toString()

0 commit comments

Comments
 (0)