Skip to content

Commit

Permalink
fix(plugin-webpack): allow port to be configurable (#693)
Browse files Browse the repository at this point in the history
* 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 4da0230
Show file tree
Hide file tree
Showing 2 changed files with 19 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 @@ -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) {
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 @@ -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 () => {
Expand Down

0 comments on commit 4da0230

Please sign in to comment.