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 2 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
6 changes: 5 additions & 1 deletion packages/plugin/webpack/src/WebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { merge as webpackMerge } from 'webpack-merge';

import { WebpackPluginConfig, WebpackPluginEntryPoint, WebpackPluginEntryPointLocalWindow, WebpackPluginEntryPointPreloadOnly } from './Config';
import AssetRelocatorPatch from './util/AssetRelocatorPatch';
import EntryPointPreloadPlugin from './util/EntryPointPreloadPlugin';
import processConfig from './util/processConfig';
import { isLocalOrNoWindowEntries, isLocalWindow, isNoWindow, isPreloadOnly, isPreloadOnlyEntries } from './util/rendererTypeUtils';

Expand Down Expand Up @@ -304,7 +305,10 @@ export default class WebpackConfigGenerator {
globalObject: 'self',
...(this.isProd ? {} : { publicPath: '/' }),
},
plugins: target === RendererTarget.ElectronPreload ? [] : [new webpack.ExternalsPlugin('commonjs2', externals)],
plugins: [
new EntryPointPreloadPlugin({ name: `entry-point-preload-${target}` }),
...(target !== RendererTarget.ElectronPreload ? [] : [new webpack.ExternalsPlugin('commonjs2', externals)]),
],
};
return webpackMerge(baseConfig, rendererConfig || {}, config);
}
Expand Down
38 changes: 34 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,54 @@ 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')) {
georgexu99 marked this conversation as resolved.
Show resolved Hide resolved
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 = [];
for (const preloadPlugin of preloadPlugins) {
promises.push(
new Promise((resolve, reject) => {
compiler.hooks.done.tap(preloadPlugin, (stats) => {
if (stats.hasErrors()) {
// TODO(georgexu99): add a way to identify preload entries with the same target name ex) preload config entries
return reject(new Error(`Compilation errors in the preload: ${stats.toString()}`));
}
return resolve(undefined);
});
})
);
}
georgexu99 marked this conversation as resolved.
Show resolved Hide resolved
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
}
}