Skip to content

Commit

Permalink
fix(plugin-webpack): preload race condition (#3353)
Browse files Browse the repository at this point in the history
* add plugin

* add unique identifier to entries with their own preload config

* clean up

* why did i add this

* rename var
  • Loading branch information
georgexu99 authored Sep 19, 2023
1 parent 3cc261a commit d7d926d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/plugin/webpack/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export interface WebpackPluginRendererConfig {
entryPoints: WebpackPluginEntryPoint[];
}

export interface EntryPointPluginConfig {
name: string;
}

export interface WebpackPluginConfig {
/**
* The webpack config for your main process
Expand Down
34 changes: 30 additions & 4 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { merge } from 'webpack-merge';

import { WebpackPluginConfig } from './Config';
import ElectronForgeLoggingPlugin from './util/ElectronForgeLogging';
import EntryPointPreloadPlugin from './util/EntryPointPreloadPlugin';
import once from './util/once';
import WebpackConfigGenerator from './WebpackConfig';

Expand Down Expand Up @@ -283,25 +284,50 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`);
};

launchRendererDevServers = async (logger: Logger): Promise<void> => {
const config = await this.configGenerator.getRendererConfig(this.config.renderer.entryPoints);
if (config.length === 0) {
const configs = await this.configGenerator.getRendererConfig(this.config.renderer.entryPoints);
if (configs.length === 0) {
return;
}

for (const entryConfig of config) {
const preloadPlugins: string[] = [];
let numPreloadEntriesWithConfig = 0;
for (const entryConfig of configs) {
if (!entryConfig.plugins) entryConfig.plugins = [];
entryConfig.plugins.push(new ElectronForgeLoggingPlugin(logger.createTab(`Renderer Target Bundle (${entryConfig.target})`)));

const filename = entryConfig.output?.filename as string;
if (filename?.endsWith('preload.js')) {
let name = `entry-point-preload-${entryConfig.target}`;
if (preloadPlugins.includes(name)) {
name = `${name}-${++numPreloadEntriesWithConfig}`;
}
entryConfig.plugins.push(new EntryPointPreloadPlugin({ name }));
preloadPlugins.push(name);
}

entryConfig.infrastructureLogging = {
level: 'none',
};
entryConfig.stats = 'none';
}

const compiler = webpack(config);
const compiler = webpack(configs);

const promises = preloadPlugins.map((preloadPlugin) => {
return new Promise((resolve, reject) => {
compiler.hooks.done.tap(preloadPlugin, (stats) => {
if (stats.hasErrors()) {
return reject(new Error(`Compilation errors in the preload: ${stats.toString()}`));
}
return resolve(undefined);
});
});
});

const webpackDevServer = new WebpackDevServer(this.devServerOptions(), compiler);
await webpackDevServer.start();
this.servers.push(webpackDevServer.server!);
await Promise.all(promises);
};

devServerOptions(): WebpackDevServer.Configuration {
Expand Down
10 changes: 10 additions & 0 deletions packages/plugin/webpack/src/util/EntryPointPreloadPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginBase } from '@electron-forge/plugin-base';

import { EntryPointPluginConfig } from '../Config';

export default class EntryPointPreloadPlugin extends PluginBase<EntryPointPluginConfig> {
name = this.config.name;
apply() {
// noop
}
}

0 comments on commit d7d926d

Please sign in to comment.