runServer.js
7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @format
*/
'use strict';
require('../../setupBabel')();
const Metro = require('metro');
const HmrServer = require('metro/src/HmrServer');
const {Terminal} = require('metro-core');
const attachWebsocketServer = require('./util/attachWebsocketServer');
const compression = require('compression');
const connect = require('connect');
const copyToClipBoardMiddleware = require('./middleware/copyToClipBoardMiddleware');
const defaultAssetExts = Metro.defaults.assetExts;
const defaultSourceExts = Metro.defaults.sourceExts;
const defaultPlatforms = Metro.defaults.platforms;
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
const defaultProvidesModuleNodeModules =
Metro.defaults.providesModuleNodeModules;
const errorhandler = require('errorhandler');
const fs = require('fs');
const getDevToolsMiddleware = require('./middleware/getDevToolsMiddleware');
const http = require('http');
const https = require('https');
const indexPageMiddleware = require('./middleware/indexPage');
const loadRawBodyMiddleware = require('./middleware/loadRawBodyMiddleware');
const messageSocket = require('./util/messageSocket.js');
const morgan = require('morgan');
const openStackFrameInEditorMiddleware = require('./middleware/openStackFrameInEditorMiddleware');
const path = require('path');
const serveStatic = require('serve-static');
const statusPageMiddleware = require('./middleware/statusPageMiddleware.js');
const systraceProfileMiddleware = require('./middleware/systraceProfileMiddleware.js');
const webSocketProxy = require('./util/webSocketProxy.js');
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
const TransformCaching = require('metro/src/lib/TransformCaching');
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
import type {ConfigT} from 'metro';
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
import type {Reporter} from 'metro/src/lib/reporting';
export type Args = {|
+assetExts: $ReadOnlyArray<string>,
+host: string,
+maxWorkers: number,
+nonPersistent: boolean,
+platforms: $ReadOnlyArray<string>,
+port: number,
+projectRoots: $ReadOnlyArray<string>,
+resetCache: boolean,
+sourceExts: $ReadOnlyArray<string>,
+verbose: boolean,
|};
function runServer(
args: Args,
config: ConfigT,
// FIXME: this is weird design. The top-level should pass down a custom
// reporter rather than passing it up as argument to an event.
startedCallback: (reporter: Reporter) => mixed,
readyCallback: (reporter: Reporter) => mixed,
) {
var wsProxy = null;
var ms = null;
const terminal = new Terminal(process.stdout);
const ReporterImpl = getReporterImpl(args.customLogReporterPath || null);
const reporter = new ReporterImpl(terminal);
const packagerServer = getPackagerServer(args, config, reporter);
startedCallback(reporter);
const app = connect()
.use(loadRawBodyMiddleware)
.use(compression())
.use(
'/debugger-ui',
serveStatic(path.join(__dirname, 'util', 'debugger-ui')),
)
.use(
getDevToolsMiddleware(args, () => wsProxy && wsProxy.isChromeConnected()),
)
.use(getDevToolsMiddleware(args, () => ms && ms.isChromeConnected()))
.use(openStackFrameInEditorMiddleware(args))
.use(copyToClipBoardMiddleware)
.use(statusPageMiddleware)
.use(systraceProfileMiddleware)
.use(indexPageMiddleware)
.use(packagerServer.processRequest.bind(packagerServer));
args.projectRoots.forEach(root => app.use(serveStatic(root)));
app.use(morgan('combined')).use(errorhandler());
if (args.https && (!args.key || !args.cert)) {
throw new Error('Cannot use https without specifying key and cert options');
}
const serverInstance = args.https
? https.createServer(
{
key: fs.readFileSync(args.key),
cert: fs.readFileSync(args.cert),
},
app,
)
: http.createServer(app);
serverInstance.listen(args.port, args.host, 511, function() {
attachWebsocketServer({
httpServer: serverInstance,
path: '/hot',
websocketServer: new HmrServer(packagerServer),
});
wsProxy = webSocketProxy.attachToServer(serverInstance, '/debugger-proxy');
ms = messageSocket.attachToServer(serverInstance, '/message');
readyCallback(reporter);
});
// Disable any kind of automatic timeout behavior for incoming
// requests in case it takes the packager more than the default
// timeout of 120 seconds to respond to a request.
serverInstance.timeout = 0;
}
function getReporterImpl(customLogReporterPath: ?string) {
if (customLogReporterPath == null) {
return require('metro/src/lib/TerminalReporter');
}
try {
// First we let require resolve it, so we can require packages in node_modules
// as expected. eg: require('my-package/reporter');
/* $FlowFixMe: can't type dynamic require */
return require(customLogReporterPath);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
// If that doesn't work, then we next try relative to the cwd, eg:
// require('./reporter');
/* $FlowFixMe: can't type dynamic require */
return require(path.resolve(customLogReporterPath));
}
}
function getPackagerServer(args, config, reporter) {
const transformModulePath = args.transformer
? path.resolve(args.transformer)
: config.getTransformModulePath();
const providesModuleNodeModules =
args.providesModuleNodeModules || defaultProvidesModuleNodeModules;
return Metro.createServer({
assetExts: defaultAssetExts.concat(args.assetExts),
assetRegistryPath: ASSET_REGISTRY_PATH,
blacklistRE: config.getBlacklistRE(),
cacheVersion: '3',
enableBabelRCLookup: config.getEnableBabelRCLookup(),
extraNodeModules: config.extraNodeModules,
dynamicDepsInPackages: config.dynamicDepsInPackages,
getModulesRunBeforeMainModule: config.getModulesRunBeforeMainModule,
getPolyfills: config.getPolyfills,
getTransformOptions: config.getTransformOptions,
globalTransformCache: null,
hasteImplModulePath: config.hasteImplModulePath,
maxWorkers: args.maxWorkers,
platforms: defaultPlatforms.concat(args.platforms),
polyfillModuleNames: config.getPolyfillModuleNames(),
postMinifyProcess: config.postMinifyProcess,
postProcessBundleSourcemap: config.postProcessBundleSourcemap,
postProcessModules: config.postProcessModules,
projectRoots: args.projectRoots,
providesModuleNodeModules: providesModuleNodeModules,
reporter,
resetCache: args.resetCache,
sourceExts: defaultSourceExts.concat(args.sourceExts),
transformModulePath: transformModulePath,
transformCache: TransformCaching.useTempDir(),
verbose: args.verbose,
watch: !args.nonPersistent,
workerPath: config.getWorkerPath(),
});
}
module.exports = runServer;