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

Added new --host option and updated relevant tests #1482

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 2 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,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, host = 'localhost', https = false) {
this.server = await Server.serve(this, port, host, https);
this.bundle();
return this.server;
}
Expand Down
9 changes: 6 additions & 3 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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 @@ -100,7 +100,7 @@ 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 => {
Expand All @@ -115,10 +115,13 @@ async function serve(bundler, port, useHTTPS = false) {
`configured port ${port} could not be used.`
)}`
: '';
const hostAddress = host || 'localhost';

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

Expand Down
10 changes: 8 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ program
'set the port to serve on. defaults to 1234',
parseInt
)
.option('--host <host>', 'set the host to listen to. defaults to localhost')
.option(
'--hmr-port <port>',
'set the port to serve HMR websockets, defaults to random',
Expand Down Expand Up @@ -186,10 +187,15 @@ 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 hostAddress = command.host || 'localhost';
const server = await bundler.serve(
command.port || 1234,
hostAddress,
command.https
);
if (server && command.open) {
await require('./utils/openInBrowser')(
`${command.https ? 'https' : 'http'}://localhost:${
`${command.https ? 'https' : 'http'}://${hostAddress}:${
server.address().port
}`,
command.open
Expand Down
4 changes: 2 additions & 2 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ describe('server', function() {

it('should support HTTPS', async function() {
let b = bundler(__dirname + '/integration/commonjs/index.js');
server = await b.serve(0, true);
server = await b.serve(0, undefined, true);

let data = await get('/index.js', https);
assert.equal(data, await fs.readFile(__dirname + '/dist/index.js', 'utf8'));
});

it('should support HTTPS via custom certificate', async function() {
let b = bundler(__dirname + '/integration/commonjs/index.js');
server = await b.serve(0, {
server = await b.serve(0, undefined, {
key: __dirname + '/integration/https/private.pem',
cert: __dirname + '/integration/https/primary.crt'
});
Expand Down