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): allow port to be configurable #693

Merged
merged 2 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -68,4 +68,8 @@ export interface WebpackPluginConfig {
* Electron Forge webpack configuration for your renderer process
*/
renderer: WebpackPluginRendererConfig;
/**
* The TCP port for the dev servers. Defaults to 3000.
*/
port?: number;
}
19 changes: 15 additions & 4 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import once from './util/once';
import { WebpackPluginConfig, WebpackPluginEntryPoint, WebpackPreloadEntryPoint } from './Config';

const d = debug('electron-forge:plugin:webpack');
const BASE_PORT = 3000;
const DEFAULT_PORT = 3000;

export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
name = 'webpack';
Expand All @@ -30,10 +30,21 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
private watchers: webpack.Compiler.Watching[] = [];
private servers: http.Server[] = [];
private loggers: Logger[] = [];
private port = DEFAULT_PORT;

constructor(c: WebpackPluginConfig) {
super(c);

if (c.port) {
malept marked this conversation as resolved.
Show resolved Hide resolved
if (c.port < 1024) {
throw new Error(`Cannot specify port (${c.port}) below 1024, as they are privileged`);
} else if (c.port > 65535) {
throw new Error(`Port specified (${c.port}) is not a valid TCP port.`);
} else {
this.port = c.port;
}
}

this.startLogic = this.startLogic.bind(this);
this.getHook = this.getHook.bind(this);
}
Expand Down Expand Up @@ -153,12 +164,12 @@ Your packaged app may be larger than expected if you dont ignore everything othe
defines[`${entryPoint.name.toUpperCase().replace(/ /g, '_')}_WEBPACK_ENTRY`] =
this.isProd
? `\`file://\$\{require('path').resolve(__dirname, '../renderer', '${upOneMore ? '..' : '.'}', '${entryPoint.name}', 'index.html')\}\``
: `'http://localhost:${BASE_PORT}/${entryPoint.name}'`;
: `'http://localhost:${this.port}/${entryPoint.name}'`;
} else {
defines[`${entryPoint.name.toUpperCase().replace(/ /g, '_')}_WEBPACK_ENTRY`] =
this.isProd
? `\`file://\$\{require('path').resolve(__dirname, '../renderer', '${upOneMore ? '..' : '.'}', '${entryPoint.name}', 'index.js')\}\``
: `'http://localhost:${BASE_PORT}/${entryPoint.name}/index.js'`;
: `'http://localhost:${this.port}/${entryPoint.name}/index.js'`;
}

const preloadDefineKey = `${entryPoint.name.toUpperCase().replace(/ /g, '_')}_PRELOAD_WEBPACK_ENTRY`;
Expand Down Expand Up @@ -340,7 +351,7 @@ Your packaged app may be larger than expected if you dont ignore everything othe
const app = express();
app.use(server);
app.use(webpackHotMiddleware(compiler));
this.servers.push(app.listen(BASE_PORT));
this.servers.push(app.listen(this.port));
});

await asyncOra('Compiling Preload Scripts', async () => {
Expand Down