/** * 确保 rollup 当前平台的 optional 原生包已安装。 * * 背景: npm bug #4828 导致跨平台共享 node_modules(如 WSL 与 Windows 挂载同一目录)时, * 一方 npm install 可能清理掉另一方的 rollup 平台包(@rollup/rollup--x64-), * 导致 build/test 报 "Cannot find module @rollup/rollup-"。 * * 方案: 在 postinstall 阶段检测当前平台所需的 rollup 原生包,缺失则自动补装 * (--no-save 不污染依赖清单,--ignore-scripts 防止递归触发 postinstall)。 */ import { execSync } from 'child_process' import { existsSync } from 'fs' import { join, dirname } from 'path' import { fileURLToPath } from 'url' const ROLLUP_VERSION = '4.60.4' const __dirname = dirname(fileURLToPath(import.meta.url)) /** 平台 → 架构 → 对应 rollup 原生包 */ const PLATFORM_PACKAGES = { win32: { x64: '@rollup/rollup-win32-x64-msvc', ia32: '@rollup/rollup-win32-ia32-msvc', arm64: '@rollup/rollup-win32-arm64-msvc', }, linux: { x64: '@rollup/rollup-linux-x64-gnu', arm64: '@rollup/rollup-linux-arm64-gnu', arm: '@rollup/rollup-linux-arm-gnueabihf', }, darwin: { x64: '@rollup/rollup-darwin-x64', arm64: '@rollup/rollup-darwin-arm64', }, } const pkg = PLATFORM_PACKAGES[process.platform]?.[process.arch] if (!pkg) { process.exit(0) } const pkgDir = join(__dirname, '..', 'node_modules', ...pkg.split('/')) if (existsSync(pkgDir)) { process.exit(0) } try { execSync(`npm install --no-save --ignore-scripts ${pkg}@${ROLLUP_VERSION}`, { stdio: 'inherit', cwd: join(__dirname, '..'), }) } catch (err) { // eslint-disable-next-line no-console -- 安装失败仅告警,不阻断 npm install console.warn(`[ensure-rollup-platform] 自动安装 ${pkg} 失败:`, err) }