Skip to content

Add Electron Forge plugin - #2158

Merged
webpro merged 4 commits into
webpro-nl:mainfrom
mehdimirzafaruk:feat/plugin-electron-forge
Oct 24, 2026
Merged

webpro merged 4 commits into
webpro-nl:mainfrom
mehdimirzafaruk:feat/plugin-electron-forge

Conversation

@mehdimirzafaruk

@mehdimirzafaruk mehdimirzafaruk commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Electron Forge is the official tool for packaging and distributing Electron apps.

Forge loads makers, publishers and plugins by package name, and its Vite and webpack plugins point to entry files and bundler configs that nothing imports. Without a plugin, Knip reports all of those as unused. On the official vite-typescript template (Forge v8.0.1) that's 6 unused files (forge.config.mts, src/preload.ts, the three vite.*.config.mts files, …) and 10 unused devDependencies (@electron-forge/maker-*, @electron-forge/plugin-vite, vite, …). With this plugin the template comes out clean.

What the plugin does:

  • enabled by @electron-forge/cli
  • config: forge.config.{js,mjs,cjs,ts,mts,cts} (loaded by Knip, handled in resolveConfig) and config.forge in package.json: an object, or a path to a config file, as in forge-config.ts
  • name of makers, publishers and plugins entries → dependencies (configuration docs)
  • VitePlugin: build[].entry → production entries, build[].config and renderer[].config → Vite configs (Vite plugin docs)
  • WebpackPlugin: renderer.entryPoints[].js and preload.js → production entries, mainConfig / renderer.config / preload.config paths → webpack configs; imported config objects → findWebpackDependenciesFromConfig (webpack plugin docs)

The plugin options are read from both forms Forge supports: new VitePlugin({ ... }) with the class imported from @electron-forge/plugin-vite (or -webpack), and { name: '@electron-forge/plugin-vite', config: { ... } }.

Four fixtures: a forge.config.ts with the Vite plugin plus mixed instance and name-based makers and publishers, a package.json config with the webpack plugin, and config.forge pointing to config/forge.config.js. Each fixture also has an unreferenced file that should still be reported. A fourth, modelled on the webpack-typescript template, imports mainConfig / rendererConfig objects so the loaders they use are found.

Tests:

cd packages/knip
bun test test/plugins/electron-forge   # 4 pass
pnpm test --runtime bun                # 1469 pass
pnpm test --runtime node --smoke       # 1306 pass
pnpm build                             # tsc ok

pnpm run ci (oxlint, oxfmt, installed-check) passes from the root.

Prepared with assistance from Claude; I reviewed and tested the change locally.

Copilot AI balanced review requested due to automatic review settings October 16, 2026 07:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request, Mehdi! I tried the plugin on fresh projects from the Forge v8.0.1 templates, and it removes nearly all the unused files and devDependencies that main reports there. Before we merge, please move the config handling from resolveFromAST to resolveConfig. Details inline.

return getStringValue(node);
};

const resolveFromAST: ResolveFromAST = (program, options) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you drop resolveFromAST and toValue and let Knip load forge.config.* too? resolveConfig already handles the object from package.json, so it can handle the default export of the config file the same way.

This fixes two things:

  • The Property visitor matches makers, publishers and plugins keys at any depth, while only the top level of the exported config counts.
  • The webpack-typescript template passes mainConfig and renderer.config as imported objects, which the AST can't follow, so ts-loader, css-loader, style-loader, node-loader and @vercel/webpack-asset-relocator-loader are still reported as unused. From the loaded config, object configs can go to findWebpackDependenciesFromConfig from the webpack plugin, as the rspack plugin does.

new VitePlugin(...) and new WebpackPlugin(...) instances keep their options on config, with name set to 'vite' or 'webpack'. Maker instances have a name as well, so only plain { name, config } objects should become dependencies. The packages behind the instances are already imported in the config file.

The fixtures will need stub packages in node_modules so the imports in forge.config.ts resolve when the config is loaded.


const config = ['package.json', 'forge.config.{js,mjs,cjs,ts,mts,cts}'];

const isLoadConfig: IsLoadConfig = ({ configFileName }) => configFileName === 'package.json';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With resolveConfig handling every config file, isLoadConfig can go.

const VITE_PLUGIN = '@electron-forge/plugin-vite';
const WEBPACK_PLUGIN = '@electron-forge/plugin-webpack';

const resolvableKeys = ['makers', 'publishers', 'plugins'];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please inline these keys where they're used in resolveConfig and remove resolvableKeys.

mehdimirzafaruk and others added 2 commits October 18, 2026 13:37
Electron Forge loads makers, publishers and plugins by package name from
forge.config.* or the "config.forge" key in package.json, and its Vite and
webpack plugins point to entry files and bundler configs that nothing
imports. Without a plugin, Knip reports those packages as unused
devDependencies and the entries, preload scripts and vite/webpack configs
as unused files.

The plugin:
- enables on @electron-forge/cli
- reads forge.config.{js,mjs,cjs,ts,mts,cts} (AST) and package.json
  config.forge (object, or a path to a config file)
- adds `name` of makers, publishers and plugins as dependencies
- adds VitePlugin `build[].entry` and webpack plugin `entryPoints[].js`
  and `preload.js` as production entries
- hands VitePlugin/webpack plugin config paths to the Vite/webpack plugins

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Drop resolveFromAST and let Knip load forge.config.* like package.json
config.forge, so resolveConfig handles both. Only the top-level makers,
publishers and plugins of the exported config are read now.

Plain { name, config } entries become dependencies. Class instances such
as new VitePlugin(...) or maker instances also carry a name, but their
packages are already imported in the config file, so only their options
on `config` are read (name 'vite' or 'webpack').

Webpack mainConfig and renderer.config can be imported objects, as in the
webpack-typescript template. Those now go to
findWebpackDependenciesFromConfig, like the rspack plugin does, so the
loaders they use are no longer reported as unused.

Fixtures get stub packages so forge.config.ts imports resolve when it is
loaded, plus a webpack-typescript fixture with imported webpack configs.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@mehdimirzafaruk
mehdimirzafaruk force-pushed the feat/plugin-electron-forge branch from 580f8a2 to 10d9057 Compare October 18, 2026 10:42
@pkg-pr-new

pkg-pr-new Bot commented Oct 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/knip@2158
npm i https://pkg.pr.new/@knip/language-server@2158
npm i https://pkg.pr.new/@knip/mcp@2158

commit: f92ee17

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Mehdi! 🧡

@webpro
webpro merged commit 12e4355 into webpro-nl:main Oct 24, 2026
32 checks passed
@webpro

webpro commented Oct 24, 2026

Copy link
Copy Markdown
Member

🚀 This pull request is included in v6.43.0. See Release 6.43.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants