Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-webpack): preload race condition #3353

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 31 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,51 @@ 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 preloadEntriesWithConfig = 0;
georgexu99 marked this conversation as resolved.
Show resolved Hide resolved
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}-${++preloadEntriesWithConfig}`;
}
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();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
georgexu99 marked this conversation as resolved.
Show resolved Hide resolved
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
}
}