Skip to content

migrate Server.listen from bin #1863

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

Merged
merged 2 commits into from
May 13, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 9 additions & 42 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ const Server = require('../lib/Server');

const colors = require('../lib/utils/colors');
const createConfig = require('../lib/utils/createConfig');
const createDomain = require('../lib/utils/createDomain');
const createLogger = require('../lib/utils/createLogger');
const defaultTo = require('../lib/utils/defaultTo');
const findPort = require('../lib/utils/findPort');
const getVersions = require('../lib/utils/getVersions');
const runBonjour = require('../lib/utils/runBonjour');
const status = require('../lib/utils/status');
const tryParseInt = require('../lib/utils/tryParseInt');

let server;
Expand Down Expand Up @@ -159,11 +156,6 @@ function startDevServer(config, options) {
}).apply(compiler);
}

const suffix =
options.inline !== false || options.lazy === true
? '/'
: '/webpack-dev-server/';

try {
server = new Server(compiler, options, log);
} catch (err) {
Expand Down Expand Up @@ -200,52 +192,27 @@ function startDevServer(config, options) {
}
});

server.listen(options.socket, options.host, (err) => {
runServer();
} else if (options.port) {
runServer();
} else {
// only run port finder if no port as been specified
findPort(server, DEFAULT_PORT, defaultPortRetry, (err, port) => {
if (err) {
throw err;
}
// chmod 666 (rw rw rw)
const READ_WRITE = 438;

fs.chmod(options.socket, READ_WRITE, (err) => {
if (err) {
throw err;
}

const uri = createDomain(options, server.listeningApp) + suffix;

status(uri, options, log, argv.color);
});
options.port = port;
runServer();
});
return;
}

const startServer = () => {
function runServer() {
server.listen(options.port, options.host, (err) => {
if (err) {
throw err;
}
if (options.bonjour) {
runBonjour(options);
}
const uri = createDomain(options, server.listeningApp) + suffix;
status(uri, options, log, argv.color);
});
};

if (options.port) {
startServer();
return;
}

// only run port finder if no port as been specified
findPort(server, DEFAULT_PORT, defaultPortRetry, (err, port) => {
if (err) {
throw err;
}
options.port = port;
startServer();
});
}

processOptions(config);
1 change: 1 addition & 0 deletions examples/api/simple/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const webpackConfig = require('./webpack.config');

const compiler = Webpack(webpackConfig);
const devServerOptions = Object.assign({}, webpackConfig.devServer, {
open: true,
stats: {
colors: true,
},
Expand Down
38 changes: 38 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const validateOptions = require('schema-utils');
const updateCompiler = require('./utils/updateCompiler');
const createLogger = require('./utils/createLogger');
const getCertificate = require('./utils/getCertificate');
const status = require('./utils/status');
const createDomain = require('./utils/createDomain');
const runBonjour = require('./utils/runBonjour');
const routes = require('./utils/routes');
const schema = require('./options.json');

Expand Down Expand Up @@ -809,6 +812,41 @@ class Server {
prefix: this.sockPath,
});

if (this.options.bonjour) {
runBonjour(this.options);
}

const showStatus = () => {
const suffix =
this.options.inline !== false || this.options.lazy === true
? '/'
: '/webpack-dev-server/';

const uri = `${createDomain(this.options, this.listeningApp)}${suffix}`;

status(
uri,
this.options,
this.log,
this.options.stats && this.options.stats.colors
);
};

if (this.options.socket) {
// chmod 666 (rw rw rw)
const READ_WRITE = 438;

fs.chmod(this.options.socket, READ_WRITE, (err) => {
if (err) {
throw err;
}

showStatus();
});
} else {
showStatus();
}

if (fn) {
fn.call(this.listeningApp, err);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const open = require('opn');
const colors = require('./colors');

// TODO: don't emit logs when webpack-dev-server is used via Node.js API
function status(uri, options, log, useColor) {
const contentBase = Array.isArray(options.contentBase)
? options.contentBase.join(', ')
Expand Down Expand Up @@ -51,7 +52,7 @@ function status(uri, options, log, useColor) {
openMessage += `: ${options.open}`;
}

open(uri + (options.openPage || ''), openOptions).catch(() => {
open(`${uri}${options.openPage || ''}`, openOptions).catch(() => {
log.warn(
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
);
Expand Down
29 changes: 29 additions & 0 deletions test/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
const { relative, sep } = require('path');
const webpack = require('webpack');
const request = require('supertest');
// Mock opn before loading Server
jest.mock('opn');
// eslint-disable-next-line import/newline-after-import
const opn = require('opn');
opn.mockImplementation(() => {
return {
catch: jest.fn(),
};
});
const Server = require('../lib/Server');
const config = require('./fixtures/simple-config/webpack.config');
const helper = require('./helper');
Expand Down Expand Up @@ -189,6 +198,26 @@ describe('Server', () => {
server.listen(8080, 'localhost');
});
});

it('should open', () => {
return new Promise((res) => {
const compiler = webpack(config);
const server = new Server(compiler, {
open: true,
});

compiler.hooks.done.tap('webpack-dev-server', () => {
expect(opn.mock.calls[0]).toEqual(['http://localhost:8080/', {}]);
expect(opn.mock.invocationCallOrder[0]).toEqual(1);
server.close(() => {
res();
});
});

compiler.run(() => {});
server.listen(8080, 'localhost');
});
});
});

describe('host', () => {
Expand Down