Skip to content

Commit 700b19d

Browse files
mathieumgSpaceK33z
authored andcommitted
Now accepting config as Promise. (Fixes #419) (#698)
1 parent 5f49fde commit 700b19d

File tree

1 file changed

+126
-112
lines changed

1 file changed

+126
-112
lines changed

bin/webpack-dev-server.js

Lines changed: 126 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -68,147 +68,161 @@ var argv = optimist.argv;
6868
var wpOpt = require("webpack/bin/convert-argv")(optimist, argv, {
6969
outputFilename: "/bundle.js"
7070
});
71-
var firstWpOpt = Array.isArray(wpOpt) ? wpOpt[0] : wpOpt;
7271

73-
var options = wpOpt.devServer || firstWpOpt.devServer || {};
72+
function processOptions(wpOpt) {
73+
//process Promise
74+
if(typeof wpOpt.then === "function") {
75+
wpOpt.then(processOptions).catch(function(err) {
76+
console.error(err.stack || err);
77+
process.exit(); // eslint-disable-line
78+
});
79+
return;
80+
}
7481

75-
if(argv.host !== "localhost" || !options.host)
76-
options.host = argv.host;
82+
var firstWpOpt = Array.isArray(wpOpt) ? wpOpt[0] : wpOpt;
7783

78-
if(argv.public)
79-
options.public = argv.public;
84+
var options = wpOpt.devServer || firstWpOpt.devServer || {};
8085

81-
if(argv.port !== 8080 || !options.port)
82-
options.port = argv.port;
86+
if(argv.host !== "localhost" || !options.host)
87+
options.host = argv.host;
8388

84-
if(!options.publicPath) {
85-
options.publicPath = firstWpOpt.output && firstWpOpt.output.publicPath || "";
86-
if(!/^(https?:)?\/\//.test(options.publicPath) && options.publicPath[0] !== "/")
87-
options.publicPath = "/" + options.publicPath;
88-
}
89+
if(argv.public)
90+
options.public = argv.public;
8991

90-
if(!options.outputPath)
91-
options.outputPath = "/";
92-
if(!options.filename)
93-
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
94-
[].concat(wpOpt).forEach(function(wpOpt) {
95-
wpOpt.output.path = "/";
96-
});
92+
if(argv.port !== 8080 || !options.port)
93+
options.port = argv.port;
9794

98-
if(!options.watchOptions)
99-
options.watchOptions = firstWpOpt.watchOptions;
95+
if(!options.publicPath) {
96+
options.publicPath = firstWpOpt.output && firstWpOpt.output.publicPath || "";
97+
if(!/^(https?:)?\/\//.test(options.publicPath) && options.publicPath[0] !== "/")
98+
options.publicPath = "/" + options.publicPath;
99+
}
100100

101-
if(argv["stdin"]) {
102-
process.stdin.on('end', function() {
103-
process.exit(0); // eslint-disable-line no-process-exit
101+
if(!options.outputPath)
102+
options.outputPath = "/";
103+
if(!options.filename)
104+
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
105+
[].concat(wpOpt).forEach(function(wpOpt) {
106+
wpOpt.output.path = "/";
104107
});
105-
process.stdin.resume();
106-
}
107108

108-
if(!options.watchDelay && !options.watchOptions) // TODO remove in next major version
109-
options.watchDelay = firstWpOpt.watchDelay;
110-
111-
if(!options.hot)
112-
options.hot = argv["hot"];
113-
114-
if(argv["content-base"]) {
115-
options.contentBase = argv["content-base"];
116-
if(/^[0-9]$/.test(options.contentBase))
117-
options.contentBase = +options.contentBase;
118-
else if(!/^(https?:)?\/\//.test(options.contentBase))
119-
options.contentBase = path.resolve(options.contentBase);
120-
} else if(argv["content-base-target"]) {
121-
options.contentBase = {
122-
target: argv["content-base-target"]
123-
};
124-
} else if(!options.contentBase) {
125-
options.contentBase = process.cwd();
126-
}
109+
if(!options.watchOptions)
110+
options.watchOptions = firstWpOpt.watchOptions;
127111

128-
if(!options.stats) {
129-
options.stats = {
130-
cached: false,
131-
cachedAssets: false
132-
};
133-
}
112+
if(argv["stdin"]) {
113+
process.stdin.on('end', function() {
114+
process.exit(0); // eslint-disable-line no-process-exit
115+
});
116+
process.stdin.resume();
117+
}
134118

135-
if(typeof options.stats === "object" && typeof options.stats.colors === "undefined")
136-
options.stats.colors = require("supports-color");
119+
if(!options.watchDelay && !options.watchOptions) // TODO remove in next major version
120+
options.watchDelay = firstWpOpt.watchDelay;
121+
122+
if(!options.hot)
123+
options.hot = argv["hot"];
124+
125+
if(argv["content-base"]) {
126+
options.contentBase = argv["content-base"];
127+
if(/^[0-9]$/.test(options.contentBase))
128+
options.contentBase = +options.contentBase;
129+
else if(!/^(https?:)?\/\//.test(options.contentBase))
130+
options.contentBase = path.resolve(options.contentBase);
131+
} else if(argv["content-base-target"]) {
132+
options.contentBase = {
133+
target: argv["content-base-target"]
134+
};
135+
} else if(!options.contentBase) {
136+
options.contentBase = process.cwd();
137+
}
138+
139+
if(!options.stats) {
140+
options.stats = {
141+
cached: false,
142+
cachedAssets: false
143+
};
144+
}
137145

138-
if(argv["lazy"])
139-
options.lazy = true;
146+
if(typeof options.stats === "object" && typeof options.stats.colors === "undefined")
147+
options.stats.colors = require("supports-color");
140148

141-
if(!argv["info"])
142-
options.noInfo = true;
149+
if(argv["lazy"])
150+
options.lazy = true;
143151

144-
if(argv["quiet"])
145-
options.quiet = true;
152+
if(!argv["info"])
153+
options.noInfo = true;
146154

147-
if(argv["https"])
148-
options.https = true;
155+
if(argv["quiet"])
156+
options.quiet = true;
149157

150-
if(argv["cert"])
151-
options.cert = fs.readFileSync(path.resolve(argv["cert"]));
158+
if(argv["https"])
159+
options.https = true;
152160

153-
if(argv["key"])
154-
options.key = fs.readFileSync(path.resolve(argv["key"]));
161+
if(argv["cert"])
162+
options.cert = fs.readFileSync(path.resolve(argv["cert"]));
155163

156-
if(argv["cacert"])
157-
options.ca = fs.readFileSync(path.resolve(argv["cacert"]));
164+
if(argv["key"])
165+
options.key = fs.readFileSync(path.resolve(argv["key"]));
158166

159-
if(argv["pfx"])
160-
options.pfx = fs.readFileSync(path.resolve(argv["pfx"]));
167+
if(argv["cacert"])
168+
options.ca = fs.readFileSync(path.resolve(argv["cacert"]));
161169

162-
if(argv["pfx-passphrase"])
163-
options.pfxPassphrase = argv["pfx-passphrase"];
170+
if(argv["pfx"])
171+
options.pfx = fs.readFileSync(path.resolve(argv["pfx"]));
164172

165-
if(argv["inline"])
166-
options.inline = true;
173+
if(argv["pfx-passphrase"])
174+
options.pfxPassphrase = argv["pfx-passphrase"];
167175

168-
if(argv["history-api-fallback"])
169-
options.historyApiFallback = true;
176+
if(argv["inline"])
177+
options.inline = true;
170178

171-
if(argv["client-log-level"])
172-
options.clientLogLevel = argv["client-log-level"];
179+
if(argv["history-api-fallback"])
180+
options.historyApiFallback = true;
173181

174-
if(argv["compress"])
175-
options.compress = true;
182+
if(argv["client-log-level"])
183+
options.clientLogLevel = argv["client-log-level"];
176184

177-
if(argv["open"])
178-
options.open = true;
185+
if(argv["compress"])
186+
options.compress = true;
179187

180-
var protocol = options.https ? "https" : "http";
188+
if(argv["open"])
189+
options.open = true;
181190

182-
if(options.inline) {
183-
var devClient = [require.resolve("../client/") + "?" + protocol + "://" + (options.public || (options.host + ":" + options.port))];
191+
var protocol = options.https ? "https" : "http";
184192

185-
if(options.hot)
186-
devClient.push("webpack/hot/dev-server");
187-
[].concat(wpOpt).forEach(function(wpOpt) {
188-
if(typeof wpOpt.entry === "object" && !Array.isArray(wpOpt.entry)) {
189-
Object.keys(wpOpt.entry).forEach(function(key) {
190-
wpOpt.entry[key] = devClient.concat(wpOpt.entry[key]);
191-
});
192-
} else {
193-
wpOpt.entry = devClient.concat(wpOpt.entry);
194-
}
193+
if(options.inline) {
194+
var devClient = [require.resolve("../client/") + "?" + protocol + "://" + (options.public || (options.host + ":" + options.port))];
195+
196+
if(options.hot)
197+
devClient.push("webpack/hot/dev-server");
198+
[].concat(wpOpt).forEach(function(wpOpt) {
199+
if(typeof wpOpt.entry === "object" && !Array.isArray(wpOpt.entry)) {
200+
Object.keys(wpOpt.entry).forEach(function(key) {
201+
wpOpt.entry[key] = devClient.concat(wpOpt.entry[key]);
202+
});
203+
} else {
204+
wpOpt.entry = devClient.concat(wpOpt.entry);
205+
}
206+
});
207+
}
208+
209+
new Server(webpack(wpOpt), options).listen(options.port, options.host, function(err) {
210+
var uri = protocol + "://" + options.host + ":" + options.port + "/";
211+
if(!options.inline)
212+
uri += "webpack-dev-server/";
213+
214+
if(err) throw err;
215+
console.log(" " + uri);
216+
console.log("webpack result is served from " + options.publicPath);
217+
if(typeof options.contentBase === "object")
218+
console.log("requests are proxied to " + options.contentBase.target);
219+
else
220+
console.log("content is served from " + options.contentBase);
221+
if(options.historyApiFallback)
222+
console.log("404s will fallback to %s", options.historyApiFallback.index || "/index.html");
223+
if(options.open)
224+
open(uri);
195225
});
196226
}
197227

198-
new Server(webpack(wpOpt), options).listen(options.port, options.host, function(err) {
199-
var uri = protocol + "://" + options.host + ":" + options.port + "/";
200-
if(!options.inline)
201-
uri += "webpack-dev-server/";
202-
203-
if(err) throw err;
204-
console.log(" " + uri);
205-
console.log("webpack result is served from " + options.publicPath);
206-
if(typeof options.contentBase === "object")
207-
console.log("requests are proxied to " + options.contentBase.target);
208-
else
209-
console.log("content is served from " + options.contentBase);
210-
if(options.historyApiFallback)
211-
console.log("404s will fallback to %s", options.historyApiFallback.index || "/index.html");
212-
if(options.open)
213-
open(uri);
214-
});
228+
processOptions(wpOpt);

0 commit comments

Comments
 (0)