Skip to content

feat: <url> pattern for open #3496

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 8 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 20 additions & 18 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ class Server {
});
}

openBrowser(uri) {
openBrowser(defaultOpenTarget) {
const isAbsoluteUrl = require('is-absolute-url');
const open = require('open');

Expand All @@ -653,7 +653,7 @@ class Server {

const getOpenTask = (item) => {
if (typeof item === 'boolean') {
return [{ target: uri, options: defaultOpenOptions }];
return [{ target: defaultOpenTarget, options: defaultOpenOptions }];
}

if (typeof item === 'string') {
Expand All @@ -665,7 +665,7 @@ class Server {
if (item.target) {
targets = Array.isArray(item.target) ? item.target : [item.target];
} else {
targets = [uri];
targets = [defaultOpenTarget];
}

return targets.map((target) => {
Expand Down Expand Up @@ -697,14 +697,16 @@ class Server {

if (openTask.target) {
if (typeof openTask.target === 'boolean') {
openTarget = uri;
openTarget = defaultOpenTarget;
} else if (openTask.target === '<url>') {
openTarget = defaultOpenTarget;
} else {
openTarget = isAbsoluteUrl(openTask.target)
? openTask.target
: new URL(openTask.target, uri).toString();
: new URL(openTask.target, defaultOpenTarget).toString();
}
} else {
openTarget = uri;
openTarget = defaultOpenTarget;
}

return open(openTarget, openTask.options).catch(() => {
Expand Down Expand Up @@ -766,7 +768,7 @@ class Server {
} else {
const protocol = this.options.https ? 'https' : 'http';
const { address, port } = this.server.address();
const prettyPrintUrl = (newHostname) =>
const prettyPrintURL = (newHostname) =>
url.format({ protocol, hostname: newHostname, port, pathname: '/' });

let server;
Expand All @@ -778,7 +780,7 @@ class Server {

if (this.options.host) {
if (this.options.host === 'localhost') {
localhost = prettyPrintUrl('localhost');
localhost = prettyPrintURL('localhost');
} else {
let isIP;

Expand All @@ -789,41 +791,41 @@ class Server {
}

if (!isIP) {
server = prettyPrintUrl(this.options.host);
server = prettyPrintURL(this.options.host);
}
}
}

const parsedIP = ipaddr.parse(address);

if (parsedIP.range() === 'unspecified') {
localhost = prettyPrintUrl('localhost');
localhost = prettyPrintURL('localhost');

const networkIPv4 = internalIp.v4.sync();

if (networkIPv4) {
networkUrlIPv4 = prettyPrintUrl(networkIPv4);
networkUrlIPv4 = prettyPrintURL(networkIPv4);
}

const networkIPv6 = internalIp.v6.sync();

if (networkIPv6) {
networkUrlIPv6 = prettyPrintUrl(networkIPv6);
networkUrlIPv6 = prettyPrintURL(networkIPv6);
}
} else if (parsedIP.range() === 'loopback') {
if (parsedIP.kind() === 'ipv4') {
loopbackIPv4 = prettyPrintUrl(parsedIP.toString());
loopbackIPv4 = prettyPrintURL(parsedIP.toString());
} else if (parsedIP.kind() === 'ipv6') {
loopbackIPv6 = prettyPrintUrl(parsedIP.toString());
loopbackIPv6 = prettyPrintURL(parsedIP.toString());
}
} else {
networkUrlIPv4 =
parsedIP.kind() === 'ipv6' && parsedIP.isIPv4MappedAddress()
? prettyPrintUrl(parsedIP.toIPv4Address().toString())
: prettyPrintUrl(address);
? prettyPrintURL(parsedIP.toIPv4Address().toString())
: prettyPrintURL(address);

if (parsedIP.kind() === 'ipv6') {
networkUrlIPv6 = prettyPrintUrl(address);
networkUrlIPv6 = prettyPrintURL(address);
}
}

Expand Down Expand Up @@ -855,7 +857,7 @@ class Server {
}

if (this.options.open) {
const openTarget = prettyPrintUrl(this.options.host || 'localhost');
const openTarget = prettyPrintURL(this.options.host || 'localhost');

this.openBrowser(openTarget);
}
Expand Down
122 changes: 122 additions & 0 deletions test/server/open-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,30 @@ describe('"open" option', () => {
server.listen(port, host);
});

it('should work with "<url>" pattern', (done) => {
const host = 'localhost';
server = new Server(
{
host,
port,
open: '<url>',
},
compiler
);

compiler.hooks.done.tap('webpack-dev-server', () => {
server.close(() => {
expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, {
wait: false,
});

done();
});
});

server.listen(port, host);
});

it('should work with relative string starting with "/"', (done) => {
const host = 'localhost';
server = new Server(
Expand Down Expand Up @@ -414,6 +438,37 @@ describe('"open" option', () => {
server.listen(port, host);
});

it('should work with "<url>" pattern in multiple strings', (done) => {
const host = 'localhost';
server = new Server(
{
host: 'localhost',
port,
open: ['<url>', 'second.html'],
},
compiler
);

compiler.hooks.done.tap('webpack-dev-server', () => {
server.close(() => {
expect(open).toHaveBeenNthCalledWith(1, `http://${host}:${port}/`, {
wait: false,
});
expect(open).toHaveBeenNthCalledWith(
2,
`http://${host}:${port}/second.html`,
{
wait: false,
}
);

done();
});
});

server.listen(port, host);
});

it('should work with empty object', (done) => {
const host = 'localhost';
server = new Server(
Expand Down Expand Up @@ -609,6 +664,34 @@ describe('"open" option', () => {
server.listen(port, host);
});

it('should work with <url> pattern in "target" and "app" options', (done) => {
const host = 'localhost';
server = new Server(
{
host,
port,
open: {
target: '<url>',
app: 'google-chrome',
},
},
compiler
);

compiler.hooks.done.tap('webpack-dev-server', () => {
server.close(() => {
expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, {
app: { name: 'google-chrome' },
wait: false,
});

done();
});
});

server.listen(port, host);
});

it("should work with object, with multiple value of the 'target' option and with the 'app' and 'arguments' options", (done) => {
const host = 'localhost';
server = new Server(
Expand Down Expand Up @@ -689,6 +772,45 @@ describe('"open" option', () => {
server.listen(port, host);
});

it.only('should work with <url> pattern in multiple open options', (done) => {
const host = 'localhost';
server = new Server(
{
host,
port,
open: [
{
target: '<url>',
app: 'google-chrome',
},
{
target: '<url>',
app: 'firefox',
},
],
},
compiler
);

compiler.hooks.done.tap('webpack-dev-server', () => {
server.close(() => {
expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, {
app: { name: 'google-chrome' },
wait: false,
});

expect(open).toHaveBeenCalledWith(`http://${host}:${port}/`, {
app: { name: 'firefox' },
wait: false,
});

done();
});
});

server.listen(port, host);
});

it("should log warning when can't open", (done) => {
open.mockImplementation(() => Promise.reject());

Expand Down