You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置electron-forge打包时不复制源代码?

How to Prevent Electron Forge from Including Source Code in the Packaged Output

Got it, I've run into this exact issue before! When using Electron Forge's package command, that extra app directory with your raw source code sticks around because of default packaging behavior—but it's totally fixable with a few config tweaks. Here's how to get rid of it:

1. Tweak the Packager Config in forge.config.js

Electron Forge uses electron-packager under the hood, and we can adjust its settings to exclude source files and ensure only the electron.asar bundle is kept.

Open your forge.config.js (or forge.config.ts if you're using TypeScript) and update the packagerConfig section:

module.exports = {
  packagerConfig: {
    asar: true, // Ensure ASAR bundling is enabled (this is default, but explicit is safer)
    ignore: [
      // Add regex patterns to exclude your source directories/files
      /^\/src$/, // Exclude the entire src/ directory (adjust path to match your project)
      /^\/\.git$/, // Exclude git files
      /^\/README.md$/, // Exclude docs you don't need in the build
    ],
    // Optional: Use `asarUnpack` if you have files that can't be bundled in ASAR (e.g., native modules)
    // asarUnpack: ['path/to/native-modules/**'],
  },
  // Keep your existing makers, plugins, and other config here
  makers: [/* ... */],
};

2. Verify Your package.json files Field

Make sure your package.json specifies only the files needed for your app to run, not your raw source code. This tells Electron Forge exactly what to include in the bundle:

{
  "files": [
    "dist/**/*", // If you compile code to a dist/ folder (e.g., TypeScript)
    "node_modules/**/*",
    "package.json",
    "main.js" // Your app's entry point
  ]
}

3. Point Forge to a Compiled App Directory (If Applicable)

If you're using a build step (like compiling TypeScript to JavaScript), you can tell Electron Forge to use the compiled output directly instead of your source directory. Add the appPackage field to your forge config:

module.exports = {
  appPackage: './dist', // Path to your compiled app files
  // Rest of your config...
};

Final Check

After updating these settings, run electron-forge package again. Your out/appname-forge-win32-x64/resources/ directory should now only contain electron.asar—no more raw source code in the app folder! The ASAR bundle already includes everything your .exe needs to run, so the source files are completely unnecessary in the final build.

内容的提问来源于stack exchange,提问作者pingze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:11:05