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

Add --host option #2181

Merged
merged 1 commit into from Oct 22, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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