1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = dev;
|
7 |
|
8 | var _openBrowser = _interopRequireDefault(require("react-dev-utils/openBrowser"));
|
9 |
|
10 | var _webpack = _interopRequireDefault(require("webpack"));
|
11 |
|
12 | var _webpackDevServer = _interopRequireDefault(require("webpack-dev-server"));
|
13 |
|
14 | var _chalk = _interopRequireDefault(require("chalk"));
|
15 |
|
16 | var _WebpackDevServerUtils = require("./WebpackDevServerUtils");
|
17 |
|
18 | var _clearConsole = _interopRequireDefault(require("./clearConsole"));
|
19 |
|
20 | var _errorOverlayMiddleware = _interopRequireDefault(require("./errorOverlayMiddleware"));
|
21 |
|
22 | var _send = _interopRequireWildcard(require("./send"));
|
23 |
|
24 | var _choosePort = _interopRequireDefault(require("./choosePort"));
|
25 |
|
26 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
27 |
|
28 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
29 |
|
30 | const isInteractive = process.stdout.isTTY;
|
31 | const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 8000;
|
32 | const HOST = process.env.HOST || '0.0.0.0';
|
33 | const PROTOCOL = process.env.HTTPS ? 'https' : 'http';
|
34 |
|
35 | const noop = () => {};
|
36 |
|
37 | process.env.NODE_ENV = 'development';
|
38 |
|
39 | function dev({
|
40 | cwd,
|
41 | webpackConfig,
|
42 | extraMiddlewares,
|
43 | beforeServerWithApp,
|
44 | beforeServer,
|
45 | afterServer,
|
46 | contentBase,
|
47 | onCompileDone = noop,
|
48 | onCompileInvalid = noop,
|
49 | proxy,
|
50 | openBrowser: openBrowserOpts,
|
51 | historyApiFallback = {
|
52 | disableDotRule: true
|
53 | }
|
54 | }) {
|
55 | if (!webpackConfig) {
|
56 | throw new Error('必须提供 webpackConfig 配置项');
|
57 | }
|
58 |
|
59 | (0, _choosePort.default)(DEFAULT_PORT).then(port => {
|
60 | if (port === null) {
|
61 | return;
|
62 | }
|
63 |
|
64 | const urls = (0, _WebpackDevServerUtils.prepareUrls)(PROTOCOL, HOST, port);
|
65 | const compiler = (0, _WebpackDevServerUtils.createCompiler)(_webpack.default, webpackConfig, 'Your App', urls);
|
66 |
|
67 |
|
68 |
|
69 | const timefix = 11000;
|
70 | compiler.plugin('watch-run', (watching, callback) => {
|
71 | watching.startTime += timefix;
|
72 | callback();
|
73 | });
|
74 | compiler.plugin('done', stats => {
|
75 | (0, _send.default)({
|
76 | type: _send.DONE
|
77 | });
|
78 | stats.startTime -= timefix;
|
79 | onCompileDone();
|
80 | });
|
81 | compiler.plugin('invalid', () => {
|
82 | (0, _send.default)({
|
83 | type: _send.COMPILING
|
84 | });
|
85 | onCompileInvalid();
|
86 | });
|
87 | const serverConfig = {
|
88 | disableHostCheck: true,
|
89 | compress: true,
|
90 | clientLogLevel: 'none',
|
91 | hot: true,
|
92 | quiet: true,
|
93 | headers: {
|
94 | 'access-control-allow-origin': '*'
|
95 | },
|
96 | publicPath: webpackConfig.output.publicPath,
|
97 | watchOptions: {
|
98 | ignored: /node_modules/
|
99 | },
|
100 | historyApiFallback,
|
101 | overlay: false,
|
102 | host: HOST,
|
103 | proxy,
|
104 | https: !!process.env.HTTPS,
|
105 | contentBase: contentBase || process.env.CONTENT_BASE,
|
106 |
|
107 | before(app) {
|
108 | if (beforeServerWithApp) {
|
109 | beforeServerWithApp(app);
|
110 | }
|
111 |
|
112 | app.use((0, _errorOverlayMiddleware.default)());
|
113 | },
|
114 |
|
115 | after(app) {
|
116 | if (extraMiddlewares) {
|
117 | extraMiddlewares.forEach(middleware => {
|
118 | app.use(middleware);
|
119 | });
|
120 | }
|
121 | }
|
122 |
|
123 | };
|
124 | const devServer = new _webpackDevServer.default(compiler, serverConfig);
|
125 |
|
126 | if (beforeServer) {
|
127 | beforeServer(devServer);
|
128 | }
|
129 |
|
130 | devServer.listen(port, HOST, err => {
|
131 | if (err) {
|
132 | console.log(err);
|
133 | return;
|
134 | }
|
135 |
|
136 | if (isInteractive) {
|
137 | (0, _clearConsole.default)();
|
138 | }
|
139 |
|
140 | console.log(_chalk.default.cyan('\nStarting the development server...\n'));
|
141 |
|
142 | if (openBrowserOpts) {
|
143 | (0, _openBrowser.default)(urls.localUrlForBrowser);
|
144 | }
|
145 |
|
146 | (0, _send.default)({
|
147 | type: _send.STARTING
|
148 | });
|
149 |
|
150 | if (afterServer) {
|
151 | afterServer(devServer);
|
152 | }
|
153 | });
|
154 | }).catch(err => {
|
155 | console.log(err);
|
156 | });
|
157 | } |
\ | No newline at end of file |