1 | import configuration from "../config";
|
2 | import { getUserConfig, isBackEndConfig, rootPath } from "../utils";
|
3 | import * as webpack from "webpack";
|
4 | import { join } from "path";
|
5 | import chalk from "chalk";
|
6 | import * as WebpackDevServer from "webpack-dev-server";
|
7 | import { once } from "ramda";
|
8 | import * as nodemon from "nodemon";
|
9 | import * as opn from "opn";
|
10 |
|
11 | const log = console.log;
|
12 |
|
13 | module.exports = opts => {
|
14 | const userConfig = getUserConfig();
|
15 | const config = configuration("development", userConfig, opts.d, opts.port);
|
16 | config.mode = "development";
|
17 | if (isBackEndConfig(userConfig.mode)) {
|
18 | webpack(config).watch(
|
19 | {},
|
20 | once(error => {
|
21 | if (error) {
|
22 | console.log(chalk.red(error));
|
23 | process.exit(1);
|
24 | return;
|
25 | }
|
26 | nodemon({
|
27 | script: join(rootPath, "./dist/index.js"),
|
28 | watch: join(rootPath, "./dist/**/*")
|
29 | }).on("quit", process.exit);
|
30 | })
|
31 | );
|
32 | } else {
|
33 | const port = opts.port;
|
34 | const open = opts.open;
|
35 | const hostname = "0.0.0.0";
|
36 |
|
37 | const options = {
|
38 | contentBase: join(rootPath, "./assets"),
|
39 | open,
|
40 | openPage: "",
|
41 | overlay: true,
|
42 | inline: true,
|
43 | host: hostname,
|
44 | quiet: true,
|
45 | port: opts.port,
|
46 | hot: true,
|
47 | historyApiFallback: true,
|
48 | proxy: userConfig && userConfig.devServer && userConfig.devServer.proxy
|
49 | };
|
50 | config.devServer = options;
|
51 |
|
52 | const server = new WebpackDevServer(webpack(config), options);
|
53 |
|
54 | server.listen(port, hostname, error => {
|
55 | if (error) {
|
56 | server.close();
|
57 | log(chalk.red(error.message));
|
58 | process.exit(1);
|
59 | }
|
60 | if (open) {
|
61 | const url = `http://localhost:${port}`;
|
62 | opn(url);
|
63 | }
|
64 | });
|
65 | }
|
66 | };
|