From 83e8705fc2e83acdbd215425dedd276434d551c0 Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Wed, 1 Jan 2025 15:36:47 -0800 Subject: [PATCH 01/12] fix(optimizer): use correct install state path for yarn PnP --- packages/vite/src/node/optimizer/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 1e528190f85b7d..89944e96d46e08 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1183,7 +1183,7 @@ const lockfileFormats = [ }, { // Yarn PnP - path: '.yarn/install-state', + path: '.yarn/install-state.gz', checkPatches: false, manager: 'yarn', }, From d6014bb3e68f35fa155552801e9aec6e52713360 Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Thu, 2 Jan 2025 09:42:12 -0800 Subject: [PATCH 02/12] use pnp file to determine whether to rerun dependency optimization --- packages/vite/src/node/optimizer/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 89944e96d46e08..308c962a4a2abc 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1182,8 +1182,14 @@ const lockfileFormats = [ manager: 'yarn', }, { - // Yarn PnP - path: '.yarn/install-state.gz', + // Yarn v3+ PnP + path: '.pnp.cjs', + checkPatches: false, + manager: 'yarn', + }, + { + // Yarn v2 PnP + path: '.pnp.js', checkPatches: false, manager: 'yarn', }, From 81d2273127d53e172c2810c9a541facb7701f8a2 Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:51:39 -0800 Subject: [PATCH 03/12] correctly handle all different nodelinkers for yarn 2+ --- packages/vite/src/node/optimizer/index.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 308c962a4a2abc..e8d7bd1a75ba2a 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1169,6 +1169,13 @@ function isSingleDefaultExport(exports: readonly string[]) { return exports.length === 1 && exports[0] === 'default' } +let yarnInstallStatePath; +try { + yarnInstallStatePath = execSync('yarn config get installStatePath').toString().trim() +} catch { + yarnInstallStatePath = '.yarn/install-state.gz' +} + const lockfileFormats = [ { path: 'node_modules/.package-lock.json', @@ -1176,20 +1183,8 @@ const lockfileFormats = [ manager: 'npm', }, { - // Yarn non-PnP - path: 'node_modules/.yarn-state.yml', - checkPatches: false, - manager: 'yarn', - }, - { - // Yarn v3+ PnP - path: '.pnp.cjs', - checkPatches: false, - manager: 'yarn', - }, - { - // Yarn v2 PnP - path: '.pnp.js', + // yarn 2+ + path: yarnInstallStatePath, checkPatches: false, manager: 'yarn', }, From 093b78e8b1cf05443ef46a043103814395591186 Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:54:07 -0800 Subject: [PATCH 04/12] add missing import --- packages/vite/src/node/optimizer/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index e8d7bd1a75ba2a..f4c74198573d35 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1,6 +1,7 @@ import fs from 'node:fs' import fsp from 'node:fs/promises' import path from 'node:path' +import { execSync } from 'node:child_process' import { promisify } from 'node:util' import { performance } from 'node:perf_hooks' import colors from 'picocolors' From a75df9097068cc5e39e5481d8bc7d01754441e81 Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:57:29 -0800 Subject: [PATCH 05/12] fix linter --- packages/vite/src/node/optimizer/index.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index f4c74198573d35..a502d7219247e3 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1170,9 +1170,11 @@ function isSingleDefaultExport(exports: readonly string[]) { return exports.length === 1 && exports[0] === 'default' } -let yarnInstallStatePath; +let yarnInstallStatePath try { - yarnInstallStatePath = execSync('yarn config get installStatePath').toString().trim() + yarnInstallStatePath = execSync('yarn config get installStatePath') + .toString() + .trim() } catch { yarnInstallStatePath = '.yarn/install-state.gz' } From e824af850d1b0590dabe6f5b6944e800b07ff659 Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Fri, 3 Jan 2025 08:48:09 -0800 Subject: [PATCH 06/12] switch back to .pnp.js --- packages/vite/src/node/optimizer/index.ts | 26 ++++++++++++----------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index a502d7219247e3..308c962a4a2abc 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1,7 +1,6 @@ import fs from 'node:fs' import fsp from 'node:fs/promises' import path from 'node:path' -import { execSync } from 'node:child_process' import { promisify } from 'node:util' import { performance } from 'node:perf_hooks' import colors from 'picocolors' @@ -1170,15 +1169,6 @@ function isSingleDefaultExport(exports: readonly string[]) { return exports.length === 1 && exports[0] === 'default' } -let yarnInstallStatePath -try { - yarnInstallStatePath = execSync('yarn config get installStatePath') - .toString() - .trim() -} catch { - yarnInstallStatePath = '.yarn/install-state.gz' -} - const lockfileFormats = [ { path: 'node_modules/.package-lock.json', @@ -1186,8 +1176,20 @@ const lockfileFormats = [ manager: 'npm', }, { - // yarn 2+ - path: yarnInstallStatePath, + // Yarn non-PnP + path: 'node_modules/.yarn-state.yml', + checkPatches: false, + manager: 'yarn', + }, + { + // Yarn v3+ PnP + path: '.pnp.cjs', + checkPatches: false, + manager: 'yarn', + }, + { + // Yarn v2 PnP + path: '.pnp.js', checkPatches: false, manager: 'yarn', }, From ff750ca40e4f37a740797064135aa9c4a1f1427a Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Mon, 6 Jan 2025 22:33:49 -0800 Subject: [PATCH 07/12] check patches --- packages/vite/src/node/optimizer/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 308c962a4a2abc..33c0e94aeadaec 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1184,13 +1184,13 @@ const lockfileFormats = [ { // Yarn v3+ PnP path: '.pnp.cjs', - checkPatches: false, + checkPatches: true, manager: 'yarn', }, { // Yarn v2 PnP path: '.pnp.js', - checkPatches: false, + checkPatches: true, manager: 'yarn', }, { From 00abe7e433bb08200315be685ee1061817799eb4 Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:26:04 -0800 Subject: [PATCH 08/12] make check patches dir a string --- packages/vite/src/node/optimizer/index.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 33c0e94aeadaec..2f696d25bd57f2 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1172,40 +1172,40 @@ function isSingleDefaultExport(exports: readonly string[]) { const lockfileFormats = [ { path: 'node_modules/.package-lock.json', - checkPatches: true, + checkPatchesDir: 'patches', manager: 'npm', }, { // Yarn non-PnP path: 'node_modules/.yarn-state.yml', - checkPatches: false, + checkPatchesDir: false, manager: 'yarn', }, { // Yarn v3+ PnP path: '.pnp.cjs', - checkPatches: true, + checkPatchesDir: '.yarn/patches', manager: 'yarn', }, { // Yarn v2 PnP path: '.pnp.js', - checkPatches: true, + checkPatchesDir: '.yarn/patches', manager: 'yarn', }, { // yarn 1 path: 'node_modules/.yarn-integrity', - checkPatches: true, + checkPatchesDir: 'patches', manager: 'yarn', }, { path: 'node_modules/.pnpm/lock.yaml', // Included in lockfile - checkPatches: false, + checkPatchesDir: false, manager: 'pnpm', }, - { name: 'bun.lockb', path: 'bun.lockb', checkPatches: true, manager: 'bun' }, + { name: 'bun.lockb', path: 'bun.lockb', checkPatchesDir: 'patches', manager: 'bun' }, ].sort((_, { manager }) => { return process.env.npm_config_user_agent?.startsWith(manager) ? 1 : -1 }) @@ -1256,10 +1256,10 @@ function getLockfileHash(environment: Environment): string { const lockfileFormat = lockfileFormats.find((f) => normalizedLockfilePath.endsWith(f.path), )! - if (lockfileFormat.checkPatches) { + if (lockfileFormat.checkPatchesDir) { // Default of https://github.com/ds300/patch-package const baseDir = lockfilePath.slice(0, -lockfileFormat.path.length) - const fullPath = path.join(baseDir, 'patches') + const fullPath = path.join(baseDir, lockfileFormat.checkPatchesDir) const stat = tryStatSync(fullPath) if (stat?.isDirectory()) { content += stat.mtimeMs.toString() From 90feac8cbfed8020d6aab9321db3a25c1049afbb Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:33:29 -0800 Subject: [PATCH 09/12] fix typescript --- packages/vite/src/node/optimizer/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 2f696d25bd57f2..a1ea5f06e65812 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1259,7 +1259,7 @@ function getLockfileHash(environment: Environment): string { if (lockfileFormat.checkPatchesDir) { // Default of https://github.com/ds300/patch-package const baseDir = lockfilePath.slice(0, -lockfileFormat.path.length) - const fullPath = path.join(baseDir, lockfileFormat.checkPatchesDir) + const fullPath = path.join(baseDir, lockfileFormat.checkPatchesDir as string) const stat = tryStatSync(fullPath) if (stat?.isDirectory()) { content += stat.mtimeMs.toString() From ad4688b92bc71fd64d0030e916f05307c115c55e Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:35:33 -0800 Subject: [PATCH 10/12] fix linter --- packages/vite/src/node/optimizer/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index a1ea5f06e65812..0636b8235c8b79 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1259,7 +1259,10 @@ function getLockfileHash(environment: Environment): string { if (lockfileFormat.checkPatchesDir) { // Default of https://github.com/ds300/patch-package const baseDir = lockfilePath.slice(0, -lockfileFormat.path.length) - const fullPath = path.join(baseDir, lockfileFormat.checkPatchesDir as string) + const fullPath = path.join( + baseDir, + lockfileFormat.checkPatchesDir as string + ) const stat = tryStatSync(fullPath) if (stat?.isDirectory()) { content += stat.mtimeMs.toString() From be34d471296020f4f753133fbd5079e64c958c2b Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:43:01 -0800 Subject: [PATCH 11/12] fix lint again --- packages/vite/src/node/optimizer/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 0636b8235c8b79..103afad0c7ee67 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1261,7 +1261,7 @@ function getLockfileHash(environment: Environment): string { const baseDir = lockfilePath.slice(0, -lockfileFormat.path.length) const fullPath = path.join( baseDir, - lockfileFormat.checkPatchesDir as string + lockfileFormat.checkPatchesDir as string, ) const stat = tryStatSync(fullPath) if (stat?.isDirectory()) { From f1d3e6118f8fa6bee81cdb105eee69cf74e891f7 Mon Sep 17 00:00:00 2001 From: smeng9 <38666763+smeng9@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:51:44 -0800 Subject: [PATCH 12/12] fix formatter --- packages/vite/src/node/optimizer/index.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 103afad0c7ee67..1ea15cd165e6d3 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1205,7 +1205,12 @@ const lockfileFormats = [ checkPatchesDir: false, manager: 'pnpm', }, - { name: 'bun.lockb', path: 'bun.lockb', checkPatchesDir: 'patches', manager: 'bun' }, + { + name: 'bun.lockb', + path: 'bun.lockb', + checkPatchesDir: 'patches', + manager: 'bun', + }, ].sort((_, { manager }) => { return process.env.npm_config_user_agent?.startsWith(manager) ? 1 : -1 })