|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +const net = require('net'); |
3 | 4 | const path = require('path');
|
4 | 5 | const fs = require('fs');
|
5 | 6 | const url = require('url');
|
@@ -759,94 +760,105 @@ class Server {
|
759 | 760 | };
|
760 | 761 |
|
761 | 762 | const useColor = getColorsOption(getCompilerConfigArray(this.compiler));
|
762 |
| - const protocol = this.options.https ? 'https' : 'http'; |
763 |
| - const { address, port } = this.server.address(); |
764 |
| - const prettyPrintUrl = (newHostname) => |
765 |
| - url.format({ protocol, hostname: newHostname, port, pathname: '/' }); |
766 |
| - |
767 |
| - let server; |
768 |
| - let localhost; |
769 |
| - let loopbackIPv4; |
770 |
| - let loopbackIPv6; |
771 |
| - let networkUrlIPv4; |
772 |
| - let networkUrlIPv6; |
773 |
| - |
774 |
| - if (this.options.host) { |
775 |
| - if (this.options.host === 'localhost') { |
776 |
| - localhost = prettyPrintUrl('localhost'); |
777 |
| - } else { |
778 |
| - let isIP; |
779 | 763 |
|
780 |
| - try { |
781 |
| - isIP = ipaddr.parse(this.options.host); |
782 |
| - } catch (error) { |
783 |
| - // Ignore |
784 |
| - } |
| 764 | + if (this.options.ipc) { |
| 765 | + this.logger.info(`Project is running at: "${this.server.address()}"`); |
| 766 | + } else { |
| 767 | + const protocol = this.options.https ? 'https' : 'http'; |
| 768 | + const { address, port } = this.server.address(); |
| 769 | + const prettyPrintUrl = (newHostname) => |
| 770 | + url.format({ protocol, hostname: newHostname, port, pathname: '/' }); |
| 771 | + |
| 772 | + let server; |
| 773 | + let localhost; |
| 774 | + let loopbackIPv4; |
| 775 | + let loopbackIPv6; |
| 776 | + let networkUrlIPv4; |
| 777 | + let networkUrlIPv6; |
| 778 | + |
| 779 | + if (this.options.host) { |
| 780 | + if (this.options.host === 'localhost') { |
| 781 | + localhost = prettyPrintUrl('localhost'); |
| 782 | + } else { |
| 783 | + let isIP; |
785 | 784 |
|
786 |
| - if (!isIP) { |
787 |
| - server = prettyPrintUrl(this.options.host); |
| 785 | + try { |
| 786 | + isIP = ipaddr.parse(this.options.host); |
| 787 | + } catch (error) { |
| 788 | + // Ignore |
| 789 | + } |
| 790 | + |
| 791 | + if (!isIP) { |
| 792 | + server = prettyPrintUrl(this.options.host); |
| 793 | + } |
788 | 794 | }
|
789 | 795 | }
|
790 |
| - } |
791 | 796 |
|
792 |
| - const parsedIP = ipaddr.parse(address); |
| 797 | + const parsedIP = ipaddr.parse(address); |
793 | 798 |
|
794 |
| - if (parsedIP.range() === 'unspecified') { |
795 |
| - localhost = prettyPrintUrl('localhost'); |
| 799 | + if (parsedIP.range() === 'unspecified') { |
| 800 | + localhost = prettyPrintUrl('localhost'); |
796 | 801 |
|
797 |
| - const networkIPv4 = internalIp.v4.sync(); |
| 802 | + const networkIPv4 = internalIp.v4.sync(); |
798 | 803 |
|
799 |
| - if (networkIPv4) { |
800 |
| - networkUrlIPv4 = prettyPrintUrl(networkIPv4); |
801 |
| - } |
| 804 | + if (networkIPv4) { |
| 805 | + networkUrlIPv4 = prettyPrintUrl(networkIPv4); |
| 806 | + } |
802 | 807 |
|
803 |
| - const networkIPv6 = internalIp.v6.sync(); |
| 808 | + const networkIPv6 = internalIp.v6.sync(); |
804 | 809 |
|
805 |
| - if (networkIPv6) { |
806 |
| - networkUrlIPv6 = prettyPrintUrl(networkIPv6); |
807 |
| - } |
808 |
| - } else if (parsedIP.range() === 'loopback') { |
809 |
| - if (parsedIP.kind() === 'ipv4') { |
810 |
| - loopbackIPv4 = prettyPrintUrl(parsedIP.toString()); |
811 |
| - } else if (parsedIP.kind() === 'ipv6') { |
812 |
| - loopbackIPv6 = prettyPrintUrl(parsedIP.toString()); |
| 810 | + if (networkIPv6) { |
| 811 | + networkUrlIPv6 = prettyPrintUrl(networkIPv6); |
| 812 | + } |
| 813 | + } else if (parsedIP.range() === 'loopback') { |
| 814 | + if (parsedIP.kind() === 'ipv4') { |
| 815 | + loopbackIPv4 = prettyPrintUrl(parsedIP.toString()); |
| 816 | + } else if (parsedIP.kind() === 'ipv6') { |
| 817 | + loopbackIPv6 = prettyPrintUrl(parsedIP.toString()); |
| 818 | + } |
| 819 | + } else { |
| 820 | + networkUrlIPv4 = |
| 821 | + parsedIP.kind() === 'ipv6' && parsedIP.isIPv4MappedAddress() |
| 822 | + ? prettyPrintUrl(parsedIP.toIPv4Address().toString()) |
| 823 | + : prettyPrintUrl(address); |
| 824 | + |
| 825 | + if (parsedIP.kind() === 'ipv6') { |
| 826 | + networkUrlIPv6 = prettyPrintUrl(address); |
| 827 | + } |
813 | 828 | }
|
814 |
| - } else { |
815 |
| - networkUrlIPv4 = |
816 |
| - parsedIP.kind() === 'ipv6' && parsedIP.isIPv4MappedAddress() |
817 |
| - ? prettyPrintUrl(parsedIP.toIPv4Address().toString()) |
818 |
| - : prettyPrintUrl(address); |
819 | 829 |
|
820 |
| - if (parsedIP.kind() === 'ipv6') { |
821 |
| - networkUrlIPv6 = prettyPrintUrl(address); |
| 830 | + this.logger.info('Project is running at:'); |
| 831 | + |
| 832 | + if (server) { |
| 833 | + this.logger.info(`Server: ${colors.info(useColor, server)}`); |
822 | 834 | }
|
823 |
| - } |
824 | 835 |
|
825 |
| - this.logger.info('Project is running at:'); |
| 836 | + if (localhost || loopbackIPv4 || loopbackIPv6) { |
| 837 | + const loopbacks = [] |
| 838 | + .concat(localhost ? [colors.info(useColor, localhost)] : []) |
| 839 | + .concat(loopbackIPv4 ? [colors.info(useColor, loopbackIPv4)] : []) |
| 840 | + .concat(loopbackIPv6 ? [colors.info(useColor, loopbackIPv6)] : []); |
826 | 841 |
|
827 |
| - if (server) { |
828 |
| - this.logger.info(`Server: ${colors.info(useColor, server)}`); |
829 |
| - } |
| 842 | + this.logger.info(`Loopback: ${loopbacks.join(', ')}`); |
| 843 | + } |
830 | 844 |
|
831 |
| - if (localhost || loopbackIPv4 || loopbackIPv6) { |
832 |
| - const loopbacks = [] |
833 |
| - .concat(localhost ? [colors.info(useColor, localhost)] : []) |
834 |
| - .concat(loopbackIPv4 ? [colors.info(useColor, loopbackIPv4)] : []) |
835 |
| - .concat(loopbackIPv6 ? [colors.info(useColor, loopbackIPv6)] : []); |
| 845 | + if (networkUrlIPv4) { |
| 846 | + this.logger.info( |
| 847 | + `On Your Network (IPv4): ${colors.info(useColor, networkUrlIPv4)}` |
| 848 | + ); |
| 849 | + } |
836 | 850 |
|
837 |
| - this.logger.info(`Loopback: ${loopbacks.join(', ')}`); |
838 |
| - } |
| 851 | + if (networkUrlIPv6) { |
| 852 | + this.logger.info( |
| 853 | + `On Your Network (IPv6): ${colors.info(useColor, networkUrlIPv6)}` |
| 854 | + ); |
| 855 | + } |
839 | 856 |
|
840 |
| - if (networkUrlIPv4) { |
841 |
| - this.logger.info( |
842 |
| - `On Your Network (IPv4): ${colors.info(useColor, networkUrlIPv4)}` |
843 |
| - ); |
844 |
| - } |
| 857 | + if (this.options.open) { |
| 858 | + const openTarget = prettyPrintUrl(this.options.host || 'localhost'); |
845 | 859 |
|
846 |
| - if (networkUrlIPv6) { |
847 |
| - this.logger.info( |
848 |
| - `On Your Network (IPv6): ${colors.info(useColor, networkUrlIPv6)}` |
849 |
| - ); |
| 860 | + this.openBrowser(openTarget); |
| 861 | + } |
850 | 862 | }
|
851 | 863 |
|
852 | 864 | if (this.options.static && this.options.static.length > 0) {
|
@@ -877,15 +889,13 @@ class Server {
|
877 | 889 | `Broadcasting "${bonjourProtocol}" with subtype of "webpack" via ZeroConf DNS (Bonjour)`
|
878 | 890 | );
|
879 | 891 | }
|
880 |
| - |
881 |
| - if (this.options.open) { |
882 |
| - const openTarget = prettyPrintUrl(this.options.host || 'localhost'); |
883 |
| - |
884 |
| - this.openBrowser(openTarget); |
885 |
| - } |
886 | 892 | }
|
887 | 893 |
|
888 | 894 | listen(port, hostname, fn) {
|
| 895 | + if (typeof port === 'function') { |
| 896 | + fn = port; |
| 897 | + } |
| 898 | + |
889 | 899 | if (
|
890 | 900 | typeof port !== 'undefined' &&
|
891 | 901 | typeof this.options.port !== 'undefined' &&
|
@@ -920,35 +930,75 @@ class Server {
|
920 | 930 |
|
921 | 931 | this.options.host = Server.getHostname(this.options.host);
|
922 | 932 |
|
923 |
| - return Server.getFreePort(this.options.port) |
924 |
| - .then((foundPort) => { |
| 933 | + const resolveFreePortOrIPC = () => { |
| 934 | + if (this.options.ipc) { |
| 935 | + return new Promise((resolve, reject) => { |
| 936 | + const socket = new net.Socket(); |
| 937 | + |
| 938 | + socket.on('error', (error) => { |
| 939 | + if (error.code === 'ECONNREFUSED') { |
| 940 | + fs.unlinkSync(this.options.ipc); |
| 941 | + |
| 942 | + resolve(this.options.ipc); |
| 943 | + |
| 944 | + return; |
| 945 | + } else if (error.code === 'ENOENT') { |
| 946 | + resolve(this.options.ipc); |
| 947 | + |
| 948 | + return; |
| 949 | + } |
| 950 | + |
| 951 | + reject(error); |
| 952 | + }); |
| 953 | + |
| 954 | + socket.connect({ path: this.options.ipc }, () => { |
| 955 | + throw new Error(`IPC "${this.options.ipc}" is already used`); |
| 956 | + }); |
| 957 | + }); |
| 958 | + } |
| 959 | + |
| 960 | + return Server.getFreePort(this.options.port).then((foundPort) => { |
925 | 961 | this.options.port = foundPort;
|
| 962 | + }); |
| 963 | + }; |
926 | 964 |
|
| 965 | + return resolveFreePortOrIPC() |
| 966 | + .then(() => { |
927 | 967 | this.initialize();
|
928 | 968 |
|
929 |
| - return this.server.listen( |
930 |
| - this.options.port, |
931 |
| - this.options.host, |
932 |
| - (error) => { |
933 |
| - if (Boolean(this.options.hot) || this.options.liveReload) { |
934 |
| - this.createWebSocketServer(); |
935 |
| - } |
| 969 | + const listenOptions = this.options.ipc |
| 970 | + ? { path: this.options.ipc } |
| 971 | + : { |
| 972 | + host: this.options.host, |
| 973 | + port: this.options.port, |
| 974 | + }; |
936 | 975 |
|
937 |
| - if (this.options.bonjour) { |
938 |
| - this.runBonjour(); |
939 |
| - } |
| 976 | + return this.server.listen(listenOptions, (error) => { |
| 977 | + if (this.options.ipc) { |
| 978 | + // chmod 666 (rw rw rw) |
| 979 | + const READ_WRITE = 438; |
940 | 980 |
|
941 |
| - this.logStatus(); |
| 981 | + fs.chmodSync(this.options.ipc, READ_WRITE); |
| 982 | + } |
942 | 983 |
|
943 |
| - if (fn) { |
944 |
| - fn.call(this.server, error); |
945 |
| - } |
| 984 | + if (Boolean(this.options.hot) || this.options.liveReload) { |
| 985 | + this.createWebSocketServer(); |
| 986 | + } |
946 | 987 |
|
947 |
| - if (typeof this.options.onListening === 'function') { |
948 |
| - this.options.onListening(this); |
949 |
| - } |
| 988 | + if (this.options.bonjour) { |
| 989 | + this.runBonjour(); |
950 | 990 | }
|
951 |
| - ); |
| 991 | + |
| 992 | + this.logStatus(); |
| 993 | + |
| 994 | + if (fn) { |
| 995 | + fn.call(this.server, error); |
| 996 | + } |
| 997 | + |
| 998 | + if (typeof this.options.onListening === 'function') { |
| 999 | + this.options.onListening(this); |
| 1000 | + } |
| 1001 | + }); |
952 | 1002 | })
|
953 | 1003 | .catch((error) => {
|
954 | 1004 | if (fn) {
|
|
0 commit comments