fix: 修复打包编译自动化异常问题

This commit is contained in:
DEV_DSW
2026-03-19 16:55:31 +08:00
parent 37be5e8ba7
commit 0dc7f39db2
8 changed files with 90 additions and 56 deletions

View File

@@ -61,31 +61,53 @@ const config: ForgeConfig = {
hooks: {
async packageAfterCopy(forgeConfig, buildPath, electronVersion, platform, arch) {
const src = path.join(__dirname, 'src/main/scripts');
const dest = path.join(buildPath, 'resources', 'scripts');
const dest = path.join(buildPath, '.vite', 'build', 'scripts');
await fs.ensureDir(dest);
// Bundle mjs scripts using esbuild
const files = await fs.readdir(src);
for (const file of files) {
if (file.endsWith('.mjs')) {
if (file.endsWith('.js')) {
await esbuild.build({
entryPoints: [path.join(src, file)],
outfile: path.join(dest, file),
bundle: true,
platform: 'node',
target: 'node16', // Adjust based on Electron version
external: ['electron', 'chromium-bidi/lib/cjs/bidiMapper/BidiMapper', 'chromium-bidi/lib/cjs/cdp/CdpConnection'], // Exclude electron if imported
format: 'esm',
target: 'node24', // Adjust based on Electron version
external: [ 'electron' ], // Exclude electron and playwright dependencies from bundling
format: 'cjs',
});
} else {
// Copy other files (e.g., .md, .png) directly
await fs.copy(path.join(src, file), path.join(dest, file));
}
}
// Force playwright into the packaged node_modules since Forge Vite plugin ignores it
const playwrightSrc = path.join(__dirname, 'node_modules', 'playwright');
const playwrightDest = path.join(buildPath, 'node_modules', 'playwright');
if (await fs.pathExists(playwrightSrc)) {
await fs.ensureDir(path.join(buildPath, 'node_modules'));
await fs.copy(playwrightSrc, playwrightDest);
}
const playwrightCoreSrc = path.join(__dirname, 'node_modules', 'playwright-core');
const playwrightCoreDest = path.join(buildPath, 'node_modules', 'playwright-core');
if (await fs.pathExists(playwrightCoreSrc)) {
await fs.ensureDir(path.join(buildPath, 'node_modules'));
await fs.copy(playwrightCoreSrc, playwrightCoreDest);
}
const chromiumBidiSrc = path.join(__dirname, 'node_modules', 'chromium-bidi');
const chromiumBidiDest = path.join(buildPath, 'node_modules', 'chromium-bidi');
if (await fs.pathExists(chromiumBidiSrc)) {
await fs.ensureDir(path.join(buildPath, 'node_modules'));
await fs.copy(chromiumBidiSrc, chromiumBidiDest);
}
// Force bytenode into the packaged node_modules since Forge Vite plugin ignores it
const bytenodeSrc = path.join(__dirname, 'node_modules', 'bytenode');
const bytenodeDest = path.join(buildPath, 'node_modules', 'bytenode');
if (await fs.pathExists(bytenodeSrc)) {
await fs.ensureDir(path.join(buildPath, 'node_modules'));
await fs.copy(bytenodeSrc, bytenodeDest);