Integrate the Source Map generator from Delta Bundler into the metro server

Reviewed By: mjesun

Differential Revision: D5793423

fbshipit-source-id: a26b6d67405d6ec0d59479cfe5091159a29018d1
This commit is contained in:
Rafael Oleza 2017-09-11 08:22:32 -07:00 committed by Facebook Github Bot
parent e57e0002d1
commit bf64ba58d3
1 changed files with 87 additions and 17 deletions

View File

@ -787,9 +787,14 @@ class Server {
return; return;
} }
if (this._opts.useDeltaBundler && requestType === 'bundle') { if (this._opts.useDeltaBundler) {
await this._processRequestUsingDeltaBundler(req, res); if (requestType === 'bundle') {
return; await this._processBundleUsingDeltaBundler(req, res);
return;
} else if (requestType === 'map') {
await this._processSourceMapUsingDeltaBundler(req, res);
return;
}
} }
const options = this._getOptionsFromUrl(req.url); const options = this._getOptionsFromUrl(req.url);
@ -877,18 +882,10 @@ class Server {
}); });
} }
async _processRequestUsingDeltaBundler( _prepareDeltaBundler(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, ): {options: BundleOptions, buildID: string} {
) {
const options = this._getOptionsFromUrl(req.url); const options = this._getOptionsFromUrl(req.url);
const requestingBundleLogEntry = log(
createActionStartEntry({
action_name: 'Requesting bundle',
bundle_url: req.url,
entry_point: options.entryFile,
}),
);
const buildID = this.getNewBuildID(); const buildID = this.getNewBuildID();
@ -909,10 +906,40 @@ class Server {
type: 'bundle_build_started', type: 'bundle_build_started',
}); });
const bundle = await this._deltaBundler.buildFullBundle({ return {options, buildID};
...options, }
deltaBundleId: this.optionsHash(options),
}); async _processBundleUsingDeltaBundler(
req: IncomingMessage,
res: ServerResponse,
) {
const {options, buildID} = this._prepareDeltaBundler(req);
const requestingBundleLogEntry = log(
createActionStartEntry({
action_name: 'Requesting bundle',
bundle_url: req.url,
entry_point: options.entryFile,
}),
);
let bundle;
try {
bundle = await this._deltaBundler.buildFullBundle({
...options,
deltaBundleId: this.optionsHash(options),
});
} catch (error) {
this._handleError(res, this.optionsHash(options), error);
this._reporter.update({
buildID,
type: 'bundle_build_failed',
});
return;
}
const etag = crypto.createHash('md5').update(bundle).digest('hex'); const etag = crypto.createHash('md5').update(bundle).digest('hex');
@ -938,6 +965,49 @@ class Server {
log(createActionEndEntry(requestingBundleLogEntry)); log(createActionEndEntry(requestingBundleLogEntry));
} }
async _processSourceMapUsingDeltaBundler(
req: IncomingMessage,
res: ServerResponse,
) {
const {options, buildID} = this._prepareDeltaBundler(req);
const requestingBundleLogEntry = log(
createActionStartEntry({
action_name: 'Requesting sourcemap',
bundle_url: req.url,
entry_point: options.entryFile,
}),
);
let sourceMap;
try {
sourceMap = await this._deltaBundler.buildFullSourceMap({
...options,
deltaBundleId: this.optionsHash(options),
});
} catch (error) {
this._handleError(res, this.optionsHash(options), error);
this._reporter.update({
buildID,
type: 'bundle_build_failed',
});
return;
}
res.setHeader('Content-Type', 'application/json');
res.end(sourceMap.toString());
this._reporter.update({
buildID,
type: 'bundle_build_done',
});
log(createActionEndEntry(requestingBundleLogEntry));
}
_symbolicate(req: IncomingMessage, res: ServerResponse) { _symbolicate(req: IncomingMessage, res: ServerResponse) {
const symbolicatingLogEntry = log(createActionStartEntry('Symbolicating')); const symbolicatingLogEntry = log(createActionStartEntry('Symbolicating'));