Repository navigation
Add Electron Forge plugin - #2158
Conversation
webpro
left a comment
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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
Propertyvisitor matchesmakers,publishersandpluginskeys at any depth, while only the top level of the exported config counts. - The webpack-typescript template passes
mainConfigandrenderer.configas imported objects, which the AST can't follow, sots-loader,css-loader,style-loader,node-loaderand@vercel/webpack-asset-relocator-loaderare still reported as unused. From the loaded config, object configs can go tofindWebpackDependenciesFromConfigfrom 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'; |
There was a problem hiding this comment.
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']; |
There was a problem hiding this comment.
Please inline these keys where they're used in resolveConfig and remove resolvableKeys.
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>
580f8a2 to
10d9057
Compare
commit: |
|
🚀 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. |
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-typescripttemplate (Forge v8.0.1) that's 6 unused files (forge.config.mts,src/preload.ts, the threevite.*.config.mtsfiles, …) and 10 unused devDependencies (@electron-forge/maker-*,@electron-forge/plugin-vite,vite, …). With this plugin the template comes out clean.What the plugin does:
@electron-forge/cliforge.config.{js,mjs,cjs,ts,mts,cts}(loaded by Knip, handled inresolveConfig) andconfig.forgeinpackage.json: an object, or a path to a config file, as in forge-config.tsnameofmakers,publishersandpluginsentries → dependencies (configuration docs)VitePlugin:build[].entry→ production entries,build[].configandrenderer[].config→ Vite configs (Vite plugin docs)WebpackPlugin:renderer.entryPoints[].jsandpreload.js→ production entries,mainConfig/renderer.config/preload.configpaths → 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.tswith the Vite plugin plus mixed instance and name-based makers and publishers, apackage.jsonconfig with the webpack plugin, andconfig.forgepointing toconfig/forge.config.js. Each fixture also has an unreferenced file that should still be reported. A fourth, modelled on thewebpack-typescripttemplate, importsmainConfig/rendererConfigobjects so the loaders they use are found.Tests:
pnpm run ci(oxlint, oxfmt, installed-check) passes from the root.Prepared with assistance from Claude; I reviewed and tested the change locally.