Skip to content

Commit

Permalink
Add --host option (#2181)
Browse files Browse the repository at this point in the history
Closes #1482.
  • Loading branch information
devongovett committed Oct 22, 2018
1 parent 05437f8 commit 2c14995
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/core/parcel-bundler/src/Bundler.js
Expand Up @@ -131,6 +131,7 @@ class Bundler extends EventEmitter {
!scopeHoist,
hmrHostname:
options.hmrHostname ||
options.host ||
(options.target === 'electron' ? 'localhost' : ''),
detailedReport: options.detailedReport || false,
global: options.global,
Expand Down Expand Up @@ -776,8 +777,8 @@ class Bundler extends EventEmitter {
return Server.middleware(this);
}

async serve(port = 1234, https = false) {
this.server = await Server.serve(this, port, https);
async serve(port = 1234, https = false, host) {
this.server = await Server.serve(this, port, host, https);
try {
await this.bundle();
} catch (e) {
Expand Down
9 changes: 6 additions & 3 deletions packages/core/parcel-bundler/src/Server.js
Expand Up @@ -110,7 +110,7 @@ function middleware(bundler) {
};
}

async function serve(bundler, port, useHTTPS = false) {
async function serve(bundler, port, host, useHTTPS = false) {
let handler = middleware(bundler);
let server;
if (!useHTTPS) {
Expand All @@ -122,10 +122,11 @@ async function serve(bundler, port, useHTTPS = false) {
}

let freePort = await getPort({port});
server.listen(freePort);
server.listen(freePort, host);

return new Promise((resolve, reject) => {
server.on('error', err => {
console.log(err);
logger.error(new Error(serverErrors(err, server.address().port)));
reject(err);
});
Expand All @@ -140,7 +141,9 @@ async function serve(bundler, port, useHTTPS = false) {

logger.persistent(
`Server running at ${logger.chalk.cyan(
`${useHTTPS ? 'https' : 'http'}://localhost:${server.address().port}`
`${useHTTPS ? 'https' : 'http'}://${host || 'localhost'}:${
server.address().port
}`
)} ${addon}`
);

Expand Down
10 changes: 9 additions & 1 deletion packages/core/parcel-bundler/src/cli.js
Expand Up @@ -13,6 +13,10 @@ program
'set the port to serve on. defaults to 1234',
parseInt
)
.option(
'--host <host>',
'set the host to listen on, defaults to listening on all interfaces'
)
.option(
'--hmr-port <port>',
'set the port to serve HMR websockets, defaults to random',
Expand Down Expand Up @@ -214,7 +218,11 @@ async function bundle(main, command) {

command.target = command.target || 'browser';
if (command.name() === 'serve' && command.target === 'browser') {
const server = await bundler.serve(command.port || 1234, command.https);
const server = await bundler.serve(
command.port || 1234,
command.https,
command.host
);
if (server && command.open) {
await require('./utils/openInBrowser')(
`${command.https ? 'https' : 'http'}://localhost:${
Expand Down
Expand Up @@ -20,7 +20,7 @@ function generateCertificate(options = {}) {
}
}

logger.log('Generating SSL Certificate...');
logger.progress('Generating SSL Certificate...');

const pki = forge.pki;
const keys = pki.rsa.generateKeyPair(2048);
Expand Down

0 comments on commit 2c14995

Please sign in to comment.