import path from 'path'; import fs from 'fs'; /** * 在构建完成后自动生成 package.json 文件 配置行业 SDK。 * * @param {string} outputDir - 输出目录,通常是构建平台对应的输出路径(如 dist/dev/mp-toutiao) * @param {object} packageContent - 要写入 package.json 的内容对象 * @returns {object} - 一个符合 Vite 插件规范的插件对象 */ function generatePackageJsonPlugin(outputDir, packageContent) { return { name: 'custom-generate-package-json', apply: 'build', generateBundle() { const outputPackageJsonPath = path.resolve(outputDir, 'package.json'); try { fs.mkdirSync(path.dirname(outputPackageJsonPath), { recursive: true }); fs.writeFileSync(outputPackageJsonPath, JSON.stringify(packageContent, null, 2), 'utf-8'); } catch (error) { console.error('[package.json] Failed to generate:', error); } }, }; } module.exports = { generatePackageJsonPlugin };