1 | var path = require('path');
|
2 | var chalk = require('chalk');
|
3 | var parse = require('url-parse');
|
4 | var express = require('express');
|
5 | var proxy = require('http-proxy-middleware');
|
6 | var WebpackDevServer = require('webpack-dev-server');
|
7 |
|
8 | var WebpackDevConfig = require('../config/webpack.config.dev');
|
9 |
|
10 |
|
11 | var clearConsole = require('./clearConsole');
|
12 | var setupCompiler = require('./setupCompiler');
|
13 | var applyProxyModdileware = require('./applyProxyModdileware');
|
14 |
|
15 | var isInteractive = process.stdout.isTTY;
|
16 |
|
17 | function runDevServer(config, dllConfig) {
|
18 | var server = config.server;
|
19 | var protocol = server.protocol;
|
20 | var host = server.host;
|
21 | var port = server.port;
|
22 | var publicPath = parse(config.output.publicPath);
|
23 | var webpackConfig = WebpackDevConfig(config, dllConfig);
|
24 | var compiler = setupCompiler(webpackConfig, {
|
25 | host: host,
|
26 | port: port,
|
27 | protocol: protocol,
|
28 | });
|
29 | var devServer = new WebpackDevServer(compiler, Object.assign({
|
30 | disableHostCheck: true,
|
31 | contentBase: [
|
32 | config.context,
|
33 | ],
|
34 | compress: true,
|
35 | inline: true,
|
36 | hot: true,
|
37 | quiet: true,
|
38 | noInfo: true,
|
39 | clientLogLevel: 'none',
|
40 | watchContentBase: false,
|
41 | watchOptions: {
|
42 | ignored: /node_modules/
|
43 | },
|
44 | publicPath: config.output.publicPath,
|
45 | https: protocol === 'https',
|
46 | headers: {
|
47 |
|
48 | 'Access-Control-Allow-Origin': '*',
|
49 | },
|
50 | host: host,
|
51 | port: port,
|
52 | overlay: true,
|
53 | historyApiFallback: false,
|
54 | setup: function (app) {
|
55 |
|
56 |
|
57 | app.use(proxy(function (pathname, req) {
|
58 | var parsedUrl = parse(req.url);
|
59 |
|
60 | if (parsedUrl.origin != 'null') {
|
61 | return true;
|
62 | }
|
63 | return false;
|
64 | }, { target: config.output.publicUrl }));
|
65 |
|
66 | config.chunks.forEach(function (chunk) {
|
67 | var dll = path.resolve(config.output.dll, chunk.name);
|
68 | app.use(publicPath.pathname, express.static(dll, {
|
69 | setHeaders: function (res) {
|
70 |
|
71 | res.header('Access-Control-Allow-Origin', '*');
|
72 | },
|
73 | }));
|
74 | });
|
75 | },
|
76 | }, config.server.original));
|
77 |
|
78 | applyProxyModdileware(devServer, config);
|
79 |
|
80 | devServer.listen(port, function (err) {
|
81 | if (err) {
|
82 | console.log(err);
|
83 | return;
|
84 | }
|
85 |
|
86 | if (isInteractive) {
|
87 | clearConsole();
|
88 | }
|
89 | console.log(chalk.cyan('Starting the development server...'));
|
90 | console.log();
|
91 |
|
92 |
|
93 |
|
94 | });
|
95 | }
|
96 |
|
97 | module.exports = runDevServer;
|