UNPKG

4.69 kBJavaScriptView Raw
1'use strict';
2
3// Do this as the first thing so that any code reading it knows the right env.
4process.env.BABEL_ENV = 'development';
5process.env.NODE_ENV = 'development';
6
7// Makes the script crash on unhandled rejections instead of silently
8// ignoring them. In the future, promise rejections that are not handled will
9// terminate the Node.js process with a non-zero exit code.
10process.on('unhandledRejection', err => {
11 throw err;
12});
13
14// Ensure environment variables are read.
15require('../config/env');
16
17const fs = require('fs');
18const chalk = require('chalk');
19const spawn = require('cross-spawn');
20const webpack = require('webpack');
21const exec = require('child_process').exec;
22const WebpackDevServer = require('webpack-dev-server');
23const clearConsole = require('react-dev-utils/clearConsole');
24const {
25 choosePort,
26 createCompiler,
27 prepareProxy,
28 prepareUrls,
29} = require('react-dev-utils/WebpackDevServerUtils');
30const openBrowser = require('react-dev-utils/openBrowser');
31const paths = require('../config/paths');
32const config = require('../config/webpack.config.dev');
33const createDevServerConfig = require('../config/webpackDevServer.config');
34const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
35const _ = require('lodash');
36
37const checkPagesRequired = require('../tools').checkPagesRequired;
38const resolveApp = require('../tools').resolveApp;
39const localMockPort = require('../tools').getLocalMockPort(require(paths.appPackageJson).proxy);
40
41const useYarn = fs.existsSync(paths.yarnLockFile);
42const isInteractive = process.stdout.isTTY;
43
44// Warn and crash if required files are missing
45if (!checkPagesRequired(paths.allPages)) {
46 process.exit(1);
47}
48
49// console.log('earth-scripts')
50
51// Tools like Cloud9 rely on this.
52const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
53const HOST = process.env.HOST || '0.0.0.0';
54
55// start mock server depends `npm run start -- stopmock`
56const isStopmock = process.argv.slice(2).find((v) => v === 'stopmock' );
57if (!isStopmock) {
58 const customerMock = require(paths.appPackageJson).mockRoot;
59 if (customerMock) {
60 const customerMockPath = resolveApp(`mock/${customerMock}`);
61 if (checkRequiredFiles([customerMockPath])) {
62 console.log(chalk.green('\n custom mock is running! \n'));
63 require('./mock').start(customerMockPath);
64 } else {
65 console.log(chalk.yellow(`\n mock warning: \n missing mock/${customerMock}, start default mockServer\n\n`));
66 require('./mock').start();
67 }
68 } else {
69 if (localMockPort) {
70 console.log(chalk.green('\n default mock is running! \n'));
71 require('./mock').start();
72 }
73 }
74}
75
76//todo 优化
77// if (process.platform === 'win32') {
78//
79// exec('mock/test.bat')
80//
81// } else if (process.platform === 'darwin') {
82//
83// exec('/bin/sh mock/test.sh');
84//
85// }
86
87// We attempt to use the default port but if it is busy, we offer the user to
88// run on a different port. `detect()` Promise resolves to the next free port.
89choosePort(HOST, DEFAULT_PORT)
90 .then(port => {
91 if (port == null) {
92 // We have not found a port.
93 return;
94 }
95 const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
96 const appName = require(paths.appPackageJson).name;
97 const urls = prepareUrls(protocol, HOST, port);
98 // Create a webpack compiler that is configured with custom messages.
99 const compiler = createCompiler(webpack, config, appName, urls, useYarn);
100 // Load proxy config
101 const proxySetting = require(paths.appPackageJson).proxy;
102 const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
103 // Serve webpack assets generated by the compiler over a web sever.
104 const serverConfig = createDevServerConfig(
105 proxyConfig,
106 urls.lanUrlForConfig
107 );
108 const devServer = new WebpackDevServer(compiler, serverConfig);
109 const publicPath = _.get(config, ['output', 'publicPath']);
110 // Launch WebpackDevServer.
111 devServer.listen(port, HOST, err => {
112 if (err) {
113 return console.log(err);
114 }
115 if (isInteractive) {
116 // clearConsole();
117 }
118 console.log(chalk.cyan('Starting the development server...\n'));
119 // 默认取第一个html打开
120 openBrowser(
121 urls.localUrlForBrowser +
122 publicPath.substring(1) +
123 `${paths.allPages[0]}.html`
124 )
125 });
126
127 ['SIGINT', 'SIGTERM'].forEach(function(sig) {
128 process.on(sig, function() {
129 devServer.close();
130 process.exit();
131 });
132 });
133 })
134 .catch(err => {
135 if (err && err.message) {
136 console.log(err.message);
137 }
138 process.exit(1);
139 });