Skip to content

Commit 2b35832

Browse files
committed
Use shared dir for browser installs
1 parent 1db1a1e commit 2b35832

File tree

2 files changed

+85
-28
lines changed

2 files changed

+85
-28
lines changed

programs/develop/webpack/plugin-browsers/browsers-lib/messages.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -433,16 +433,35 @@ export function prettyPuppeteerInstallGuidance(
433433
// ignore expansion errors
434434
}
435435

436-
// Inject --path "<cacheDir>" into Puppeteer install commands if not present
436+
// Normalize browser subdir for our binaries layout (shared across installers)
437+
let browserNorm = ''
438+
439+
if (browser === 'chromium-based') browserNorm = 'chromium'
440+
if (browser === 'gecko-based') browserNorm = 'firefox'
441+
if (
442+
browser === 'chrome' ||
443+
browser === 'chromium' ||
444+
browser === 'firefox' ||
445+
browser === 'edge'
446+
) {
447+
browserNorm = browser
448+
}
449+
// default to chrome for unknown
450+
browserNorm = 'chromium'
451+
452+
const finalCachePath =
453+
browserNorm && cacheDir ? path.join(cacheDir, browserNorm) : cacheDir
454+
455+
// Inject install destinations for Puppeteer/Playwright to land in our cache
437456
try {
438-
if (cacheDir && cacheDir.trim().length > 0) {
457+
if (finalCachePath && finalCachePath.trim().length > 0) {
439458
const lines = cleaned.split(/\r?\n/)
440459
// Puppeteer path injection
441460
const idx = lines.findIndex((l) =>
442461
/npx\s+@puppeteer\/browsers\s+install\s+/i.test(l)
443462
)
444463
if (idx !== -1 && !/--path\s+/i.test(lines[idx])) {
445-
lines[idx] = `${lines[idx].trim()} --path "${cacheDir}"`
464+
lines[idx] = `${lines[idx].trim()} --path "${finalCachePath}"`
446465
cleaned = lines.join('\n')
447466
}
448467
// Playwright custom install dir via PLAYWRIGHT_BROWSERS_PATH
@@ -452,32 +471,12 @@ export function prettyPuppeteerInstallGuidance(
452471
)
453472

454473
if (pwIdx !== -1) {
455-
// Normalize browser subdir for our binaries layout
456-
const browserNorm = (() => {
457-
const b = String(browser || '').toLowerCase()
458-
if (b === 'chromium-based') return 'chromium'
459-
if (b === 'gecko-based') return 'firefox'
460-
if (
461-
b === 'chrome' ||
462-
b === 'chromium' ||
463-
b === 'firefox' ||
464-
b === 'edge'
465-
)
466-
return b
467-
// default to chrome for unknown
468-
return 'chrome'
469-
})()
470-
const finalPath =
471-
browserNorm && cacheDir
472-
? path.join(cacheDir, browserNorm)
473-
: cacheDir
474-
475-
if (finalPath && finalPath.trim().length > 0) {
474+
if (finalCachePath && finalCachePath.trim().length > 0) {
476475
const cmd = lines[pwIdx].trim()
477476
const withEnv =
478477
process.platform === 'win32'
479-
? `set PLAYWRIGHT_BROWSERS_PATH="${finalPath}" && ${cmd}`
480-
: `PLAYWRIGHT_BROWSERS_PATH="${finalPath}" ${cmd}`
478+
? `set PLAYWRIGHT_BROWSERS_PATH="${finalCachePath}" && ${cmd}`
479+
: `PLAYWRIGHT_BROWSERS_PATH="${finalCachePath}" ${cmd}`
481480
lines[pwIdx] = withEnv
482481
cleaned = lines.join('\n')
483482
}
@@ -491,8 +490,8 @@ export function prettyPuppeteerInstallGuidance(
491490
}
492491

493492
body.push(cleaned)
494-
if (cacheDir) {
495-
body.push(`${dim('INSTALL PATH')} ${colors.underline(cacheDir)}`)
493+
if (finalCachePath) {
494+
body.push(`${dim('INSTALL PATH')} ${colors.underline(finalCachePath)}`)
496495
}
497496
return body.join('\n') + '\n'
498497
}

programs/develop/webpack/plugin-browsers/browsers-lib/output-binaries-resolver.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,58 @@ import * as fs from 'fs'
22
import * as path from 'path'
33
import type {Compilation} from '@rspack/core'
44

5+
function computeSharedCacheRoot(): string {
6+
// Highest priority: explicit override
7+
const explicit = String(process.env.EXT_BROWSERS_CACHE_DIR || '').trim()
8+
if (explicit) return path.resolve(explicit)
9+
10+
// Respect XDG cache on Linux
11+
const isWin = process.platform === 'win32'
12+
const isMac = process.platform === 'darwin'
13+
14+
if (isWin) {
15+
const local = String(process.env.LOCALAPPDATA || '').trim()
16+
if (local) return path.join(local, 'extension.js', 'browsers')
17+
18+
const userProfile = String(process.env.USERPROFILE || '').trim()
19+
20+
if (userProfile) {
21+
return path.join(
22+
userProfile,
23+
'AppData',
24+
'Local',
25+
'extension.js',
26+
'browsers'
27+
)
28+
}
29+
// Fallback to cwd if envs are missing (rare)
30+
return path.resolve(process.cwd(), '.cache', 'extension.js', 'browsers')
31+
}
32+
33+
if (isMac) {
34+
const home = String(process.env.HOME || '').trim()
35+
36+
if (home) {
37+
return path.join(home, 'Library', 'Caches', 'extension.js', 'browsers')
38+
}
39+
40+
return path.resolve(process.cwd(), '.cache', 'extension.js', 'browsers')
41+
}
42+
43+
// Linux / others
44+
const xdg = String(process.env.XDG_CACHE_HOME || '').trim()
45+
46+
if (xdg) return path.join(xdg, 'extension.js', 'browsers')
47+
48+
const home = String(process.env.HOME || '').trim()
49+
50+
if (home) {
51+
return path.join(home, '.cache', 'extension.js', 'browsers')
52+
}
53+
54+
return path.resolve(process.cwd(), '.cache', 'extension.js', 'browsers')
55+
}
56+
557
export function getCompilationOutputPath(compilation: Compilation): string {
658
try {
759
return (
@@ -13,6 +65,12 @@ export function getCompilationOutputPath(compilation: Compilation): string {
1365
}
1466

1567
export function computeBinariesBaseDir(compilation: Compilation) {
68+
// New default: per-user shared cache
69+
if (process.env.EXTENSIONJS_BINARIES_IN_DIST !== '1') {
70+
return computeSharedCacheRoot()
71+
}
72+
73+
// Legacy fallback: project dist
1674
const outputDir = getCompilationOutputPath(compilation)
1775

1876
if (outputDir) {

0 commit comments

Comments
 (0)