Skip to content

Commit 2a0fd29

Browse files
committed
refactor: inline runOpen util
1 parent 5a6146a commit 2a0fd29

File tree

2 files changed

+85
-90
lines changed

2 files changed

+85
-90
lines changed

lib/Server.js

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,90 @@ class Server {
558558
});
559559
}
560560

561+
static runOpen(uri, options, logger) {
562+
const isAbsoluteUrl = require('is-absolute-url');
563+
const open = require('open');
564+
565+
// https://github.com/webpack/webpack-dev-server/issues/1990
566+
const defaultOpenOptions = { wait: false };
567+
const openTasks = [];
568+
569+
const getOpenTask = (item) => {
570+
if (typeof item === 'boolean') {
571+
return [{ target: uri, options: defaultOpenOptions }];
572+
}
573+
574+
if (typeof item === 'string') {
575+
return [{ target: item, options: defaultOpenOptions }];
576+
}
577+
578+
let targets;
579+
580+
if (item.target) {
581+
targets = Array.isArray(item.target) ? item.target : [item.target];
582+
} else {
583+
targets = [uri];
584+
}
585+
586+
return targets.map((target) => {
587+
const openOptions = defaultOpenOptions;
588+
589+
if (item.app) {
590+
if (typeof item.app === 'string') {
591+
openOptions.app = { name: item.app };
592+
} else {
593+
openOptions.app = item.app;
594+
}
595+
}
596+
597+
return { target, options: openOptions };
598+
});
599+
};
600+
601+
if (Array.isArray(options)) {
602+
options.forEach((item) => {
603+
openTasks.push(...getOpenTask(item));
604+
});
605+
} else {
606+
openTasks.push(...getOpenTask(options));
607+
}
608+
609+
return Promise.all(
610+
openTasks.map((openTask) => {
611+
let openTarget;
612+
613+
if (openTask.target) {
614+
if (typeof openTask.target === 'boolean') {
615+
openTarget = uri;
616+
} else {
617+
openTarget = isAbsoluteUrl(openTask.target)
618+
? openTask.target
619+
: new URL(openTask.target, uri).toString();
620+
}
621+
} else {
622+
openTarget = uri;
623+
}
624+
625+
return open(openTarget, openTask.options).catch(() => {
626+
logger.warn(
627+
`Unable to open "${openTarget}" page${
628+
// eslint-disable-next-line no-nested-ternary
629+
openTask.options.app
630+
? ` in "${openTask.options.app.name}" app${
631+
openTask.options.app.arguments
632+
? ` with "${openTask.options.app.arguments.join(
633+
' '
634+
)}" arguments`
635+
: ''
636+
}`
637+
: ''
638+
}. If you are running in a headless environment, please do not use the "open" option or related flags like "--open", "--open-target", and "--open-app".`
639+
);
640+
});
641+
})
642+
);
643+
}
644+
561645
logStatus() {
562646
const useColor = getColorsOption(getCompilerConfigArray(this.compiler));
563647
const protocol = this.options.https ? 'https' : 'http';
@@ -680,11 +764,9 @@ class Server {
680764
}
681765

682766
if (this.options.open) {
683-
const runOpen = require('./utils/runOpen');
684-
685767
const openTarget = prettyPrintUrl(this.options.host || 'localhost');
686768

687-
runOpen(openTarget, this.options.open, this.logger);
769+
Server.runOpen(openTarget, this.options.open, this.logger);
688770
}
689771
}
690772

lib/utils/runOpen.js

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)