Skip to content
Permalink
Browse files

fix(plugin-webpack): allow port to be configurable (#693)

* fix(plugin-webpack): allow port to be configurable

ISSUES CLOSED: #691

* Add basic checks to custom webpack port
  • Loading branch information
malept authored and MarshallOfSound committed Feb 26, 2019
1 parent 696b11d commit 4da0230ff6611d1b1ba58e2596aa188f44df65f1
Showing with 19 additions and 4 deletions.
  1. +4 −0 packages/plugin/webpack/src/Config.ts
  2. +15 −4 packages/plugin/webpack/src/WebpackPlugin.ts
@@ -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;
}
@@ -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';
@@ -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) {
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);
}
@@ -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`;
@@ -348,7 +359,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 () => {

0 comments on commit 4da0230

Please sign in to comment.
You can’t perform that action at this time.