Skip to content

Commit 5269e76

Browse files
committed
Merge pull request #419 from crohde7707/master
Update webpack-dev-server to accept a promise
2 parents e5df542 + 35286d8 commit 5269e76

File tree

1 file changed

+123
-109
lines changed

1 file changed

+123
-109
lines changed

bin/webpack-dev-server.js

Lines changed: 123 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -115,141 +115,155 @@ var argv = yargs.argv;
115115
var wpOpt = require("webpack/bin/convert-argv")(yargs, argv, {
116116
outputFilename: "/bundle.js"
117117
});
118-
var firstWpOpt = Array.isArray(wpOpt) ? wpOpt[0] : wpOpt;
119118

120-
var options = wpOpt.devServer || firstWpOpt.devServer || {};
119+
function processOptions(wpOpt) {
120+
//process Promise
121+
if(typeof wpOpt.then === "function") {
122+
wpOpt.then(processOptions).catch(function(err) {
123+
console.error(err.stack || err);
124+
process.exit(); // eslint-disable-line
125+
});
126+
return;
127+
}
121128

122-
if(argv.host !== "localhost" || !options.host)
123-
options.host = argv.host;
129+
var firstWpOpt = Array.isArray(wpOpt) ? wpOpt[0] : wpOpt;
124130

125-
if(argv.public)
126-
options.public = argv.public;
131+
var options = wpOpt.devServer || firstWpOpt.devServer || {};
127132

128-
if(argv.port !== 8080 || !options.port)
129-
options.port = argv.port;
133+
if(argv.host !== "localhost" || !options.host)
134+
options.host = argv.host;
130135

131-
if(!options.publicPath) {
132-
options.publicPath = firstWpOpt.output && firstWpOpt.output.publicPath || "";
133-
if(!/^(https?:)?\/\//.test(options.publicPath) && options.publicPath[0] !== "/")
134-
options.publicPath = "/" + options.publicPath;
135-
}
136+
if(argv.public)
137+
options.public = argv.public;
136138

137-
if(!options.outputPath)
138-
options.outputPath = "/";
139-
if(!options.filename)
140-
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
141-
[].concat(wpOpt).forEach(function(wpOpt) {
142-
wpOpt.output.path = "/";
143-
});
139+
if(argv.port !== 8080 || !options.port)
140+
options.port = argv.port;
144141

145-
if(!options.watchOptions)
146-
options.watchOptions = firstWpOpt.watchOptions;
142+
if(!options.publicPath) {
143+
options.publicPath = firstWpOpt.output && firstWpOpt.output.publicPath || "";
144+
if(!/^(https?:)?\/\//.test(options.publicPath) && options.publicPath[0] !== "/")
145+
options.publicPath = "/" + options.publicPath;
146+
}
147147

148-
if(argv["stdin"]) {
149-
process.stdin.on('end', function() {
150-
process.exit(0);
148+
if(!options.outputPath)
149+
options.outputPath = "/";
150+
if(!options.filename)
151+
options.filename = firstWpOpt.output && firstWpOpt.output.filename;
152+
[].concat(wpOpt).forEach(function(wpOpt) {
153+
wpOpt.output.path = "/";
151154
});
152-
process.stdin.resume();
153-
}
154155

155-
if(!options.watchDelay && !options.watchOptions) // TODO remove in next major version
156-
options.watchDelay = firstWpOpt.watchDelay;
157-
158-
if(!options.hot)
159-
options.hot = argv["hot"];
160-
161-
if(argv["content-base"]) {
162-
options.contentBase = argv["content-base"];
163-
if(/^[0-9]$/.test(options.contentBase))
164-
options.contentBase = +options.contentBase;
165-
else if(!/^(https?:)?\/\//.test(options.contentBase))
166-
options.contentBase = path.resolve(options.contentBase);
167-
} else if(argv["content-base-target"]) {
168-
options.contentBase = {
169-
target: argv["content-base-target"]
170-
};
171-
} else if(!options.contentBase) {
172-
options.contentBase = process.cwd();
173-
}
156+
if(!options.watchOptions)
157+
options.watchOptions = firstWpOpt.watchOptions;
174158

175-
if(!options.stats) {
176-
options.stats = {
177-
cached: false,
178-
cachedAssets: false
179-
};
180-
}
159+
if(argv["stdin"]) {
160+
process.stdin.on('end', function() {
161+
process.exit(0);
162+
});
163+
process.stdin.resume();
164+
}
181165

182-
if(typeof options.stats === "object" && typeof options.stats.colors === "undefined")
183-
options.stats.colors = require("supports-color");
166+
if(!options.watchDelay && !options.watchOptions) // TODO remove in next major version
167+
options.watchDelay = firstWpOpt.watchDelay;
168+
169+
if(!options.hot)
170+
options.hot = argv["hot"];
171+
172+
if(argv["content-base"]) {
173+
options.contentBase = argv["content-base"];
174+
if(/^[0-9]$/.test(options.contentBase))
175+
options.contentBase = +options.contentBase;
176+
else if(!/^(https?:)?\/\//.test(options.contentBase))
177+
options.contentBase = path.resolve(options.contentBase);
178+
} else if(argv["content-base-target"]) {
179+
options.contentBase = {
180+
target: argv["content-base-target"]
181+
};
182+
} else if(!options.contentBase) {
183+
options.contentBase = process.cwd();
184+
}
185+
186+
if(!options.stats) {
187+
options.stats = {
188+
cached: false,
189+
cachedAssets: false
190+
};
191+
}
184192

185-
if(argv["lazy"])
186-
options.lazy = true;
193+
if(typeof options.stats === "object" && typeof options.stats.colors === "undefined")
194+
options.stats.colors = require("supports-color");
187195

188-
if(!argv["info"])
189-
options.noInfo = true;
196+
if(argv["lazy"])
197+
options.lazy = true;
190198

191-
if(argv["quiet"])
192-
options.quiet = true;
199+
if(!argv["info"])
200+
options.noInfo = true;
193201

194-
if(argv["https"])
195-
options.https = true;
202+
if(argv["quiet"])
203+
options.quiet = true;
196204

197-
if(argv["cert"])
198-
options.cert = fs.readFileSync(path.resolve(argv["cert"]));
205+
if(argv["https"])
206+
options.https = true;
199207

200-
if(argv["key"])
201-
options.key = fs.readFileSync(path.resolve(argv["key"]));
208+
if(argv["cert"])
209+
options.cert = fs.readFileSync(path.resolve(argv["cert"]));
202210

203-
if(argv["cacert"])
204-
options.cacert = fs.readFileSync(path.resolve(argv["cacert"]));
211+
if(argv["key"])
212+
options.key = fs.readFileSync(path.resolve(argv["key"]));
205213

206-
if(argv["inline"] === false)
207-
options.inline = false;
214+
if(argv["cacert"])
215+
options.cacert = fs.readFileSync(path.resolve(argv["cacert"]));
208216

209-
if(argv["history-api-fallback"])
210-
options.historyApiFallback = true;
217+
if(argv["inline"] === false)
218+
options.inline = false;
211219

212-
if(argv["compress"])
213-
options.compress = true;
220+
if(argv["history-api-fallback"])
221+
options.historyApiFallback = true;
214222

215-
if(argv["open"])
216-
options.open = true;
223+
if(argv["compress"])
224+
options.compress = true;
217225

218-
var protocol = options.https ? "https" : "http";
226+
if(argv["open"])
227+
options.open = true;
219228

220-
if(options.inline !== false) {
221-
var devClient = [require.resolve("../client/") + "?" + protocol + "://" + (options.public || (options.host + ":" + options.port))];
229+
var protocol = options.https ? "https" : "http";
222230

223-
if(options.hot)
224-
devClient.push("webpack/hot/dev-server");
225-
[].concat(wpOpt).forEach(function(wpOpt) {
226-
if(typeof wpOpt.entry === "object" && !Array.isArray(wpOpt.entry)) {
227-
Object.keys(wpOpt.entry).forEach(function(key) {
228-
wpOpt.entry[key] = devClient.concat(wpOpt.entry[key]);
229-
});
230-
} else {
231-
wpOpt.entry = devClient.concat(wpOpt.entry);
232-
}
231+
if(options.inline !== false) {
232+
var devClient = [require.resolve("../client/") + "?" + protocol + "://" + (options.public || (options.host + ":" + options.port))];
233+
234+
if(options.hot)
235+
devClient.push("webpack/hot/dev-server");
236+
[].concat(wpOpt).forEach(function(wpOpt) {
237+
if(typeof wpOpt.entry === "object" && !Array.isArray(wpOpt.entry)) {
238+
Object.keys(wpOpt.entry).forEach(function(key) {
239+
wpOpt.entry[key] = devClient.concat(wpOpt.entry[key]);
240+
});
241+
} else {
242+
wpOpt.entry = devClient.concat(wpOpt.entry);
243+
}
244+
});
245+
}
246+
247+
new Server(webpack(wpOpt), options).listen(options.port, options.host, function(err) {
248+
if(err) throw err;
249+
250+
var uri = protocol + "://" + options.host + ":" + options.port + "/";
251+
if(options.inline === false)
252+
uri += "webpack-dev-server/";
253+
console.log(uri);
254+
255+
console.log("webpack result is served from " + options.publicPath);
256+
if(Array.isArray(options.contentBase))
257+
console.log("content is served from " + options.contentBase.join(", "));
258+
else if(typeof options.contentBase === "object")
259+
console.log("requests are proxied to " + options.contentBase.target);
260+
else
261+
console.log("content is served from " + options.contentBase);
262+
if(options.historyApiFallback)
263+
console.log("404s will fallback to %s", options.historyApiFallback.index || "/index.html");
264+
if(options.open)
265+
open(uri);
233266
});
234267
}
235268

236-
new Server(webpack(wpOpt), options).listen(options.port, options.host, function(err) {
237-
if(err) throw err;
238-
239-
var uri = protocol + "://" + options.host + ":" + options.port + "/";
240-
if(options.inline === false)
241-
uri += "webpack-dev-server/";
242-
console.log(uri);
243-
244-
console.log("webpack result is served from " + options.publicPath);
245-
if(Array.isArray(options.contentBase))
246-
console.log("content is served from " + options.contentBase.join(", "));
247-
else if(typeof options.contentBase === "object")
248-
console.log("requests are proxied to " + options.contentBase.target);
249-
else
250-
console.log("content is served from " + options.contentBase);
251-
if(options.historyApiFallback)
252-
console.log("404s will fallback to %s", options.historyApiFallback.index || "/index.html");
253-
if(options.open)
254-
open(uri);
255-
});
269+
processOptions(wpOpt);

0 commit comments

Comments
 (0)