2015-03-23 18:48:02 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-08-13 02:04:54 +00:00
|
|
|
const Promise = require('promise');
|
|
|
|
const declareOpts = require('../lib/declareOpts');
|
2016-03-02 15:31:11 +00:00
|
|
|
const os = require('os');
|
2015-08-13 02:04:54 +00:00
|
|
|
const util = require('util');
|
|
|
|
const workerFarm = require('worker-farm');
|
2015-09-29 23:06:42 +00:00
|
|
|
const debug = require('debug')('ReactNativePackager:JStransformer');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-08-19 23:35:52 +00:00
|
|
|
// Avoid memory leaks caused in workers. This number seems to be a good enough number
|
|
|
|
// to avoid any memory leak while not slowing down initial builds.
|
|
|
|
// TODO(amasad): Once we get bundle splitting, we can drive this down a bit more.
|
|
|
|
const MAX_CALLS_PER_WORKER = 600;
|
|
|
|
|
|
|
|
// Worker will timeout if one of the callers timeout.
|
2016-01-08 21:38:30 +00:00
|
|
|
const DEFAULT_MAX_CALL_TIME = 301000;
|
2015-08-19 23:35:52 +00:00
|
|
|
|
2015-08-31 16:40:46 +00:00
|
|
|
// How may times can we tolerate failures from the worker.
|
2016-01-06 15:14:20 +00:00
|
|
|
const MAX_RETRIES = 2;
|
2015-08-31 16:40:46 +00:00
|
|
|
|
2015-08-13 02:04:54 +00:00
|
|
|
const validateOpts = declareOpts({
|
2015-02-24 22:22:13 +00:00
|
|
|
transformModulePath: {
|
|
|
|
type:'string',
|
2015-02-27 18:23:54 +00:00
|
|
|
required: false,
|
2015-02-24 22:22:13 +00:00
|
|
|
},
|
2015-08-19 23:35:52 +00:00
|
|
|
transformTimeoutInterval: {
|
|
|
|
type: 'number',
|
|
|
|
default: DEFAULT_MAX_CALL_TIME,
|
2016-01-04 19:31:48 +00:00
|
|
|
},
|
2015-02-24 22:22:13 +00:00
|
|
|
});
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-03-02 15:31:11 +00:00
|
|
|
const maxConcurrentWorkers = ((cores, override) => {
|
|
|
|
if (override) {
|
|
|
|
return Math.min(cores, override);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cores < 3) {
|
|
|
|
return cores;
|
|
|
|
}
|
|
|
|
if (cores < 8) {
|
|
|
|
return Math.floor(cores * 0.75);
|
|
|
|
}
|
|
|
|
if (cores < 24) {
|
|
|
|
return Math.floor(3/8 * cores + 3); // between cores *.75 and cores / 2
|
|
|
|
}
|
|
|
|
return cores / 2;
|
|
|
|
})(os.cpus().length, process.env.REACT_NATIVE_MAX_WORKERS);
|
|
|
|
|
2015-08-13 02:04:54 +00:00
|
|
|
class Transformer {
|
|
|
|
constructor(options) {
|
2015-08-19 23:35:52 +00:00
|
|
|
const opts = this._opts = validateOpts(options);
|
2015-02-24 22:22:13 +00:00
|
|
|
|
2016-03-08 17:50:14 +00:00
|
|
|
const {transformModulePath} = opts;
|
|
|
|
|
|
|
|
if (transformModulePath) {
|
|
|
|
this._transformModulePath = require.resolve(transformModulePath);
|
|
|
|
|
|
|
|
this._workers = workerFarm(
|
|
|
|
{
|
|
|
|
autoStart: true,
|
|
|
|
maxConcurrentCallsPerWorker: 1,
|
|
|
|
maxConcurrentWorkers: maxConcurrentWorkers,
|
|
|
|
maxCallsPerWorker: MAX_CALLS_PER_WORKER,
|
|
|
|
maxCallTime: opts.transformTimeoutInterval,
|
|
|
|
maxRetries: MAX_RETRIES,
|
|
|
|
},
|
|
|
|
require.resolve('./worker'),
|
|
|
|
['minify', 'transformAndExtractDependencies']
|
|
|
|
);
|
|
|
|
|
|
|
|
this._transform = Promise.denodeify(this._workers.transformAndExtractDependencies);
|
|
|
|
this.minify = Promise.denodeify(this._workers.minify);
|
2015-08-13 02:04:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-08-13 02:04:54 +00:00
|
|
|
kill() {
|
|
|
|
this._workers && workerFarm.end(this._workers);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 17:50:14 +00:00
|
|
|
transformFile(fileName, code, options) {
|
|
|
|
if (!this._transform) {
|
2015-08-13 02:04:54 +00:00
|
|
|
return Promise.reject(new Error('No transfrom module'));
|
|
|
|
}
|
2016-03-08 17:50:14 +00:00
|
|
|
debug('transforming file', fileName);
|
|
|
|
return this
|
|
|
|
._transform(this._transformModulePath, fileName, code, options)
|
|
|
|
.then(result => {
|
|
|
|
debug('done transforming file', fileName);
|
|
|
|
return result;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
if (error.type === 'TimeoutError') {
|
|
|
|
const timeoutErr = new Error(
|
|
|
|
`TimeoutError: transforming ${fileName} took longer than ` +
|
|
|
|
`${this._opts.transformTimeoutInterval / 1000} seconds.\n` +
|
|
|
|
`You can adjust timeout via the 'transformTimeoutInterval' option`
|
|
|
|
);
|
|
|
|
timeoutErr.type = 'TimeoutError';
|
|
|
|
throw timeoutErr;
|
|
|
|
} else if (error.type === 'ProcessTerminatedError') {
|
|
|
|
const uncaughtError = new Error(
|
|
|
|
'Uncaught error in the transformer worker: ' +
|
|
|
|
this._opts.transformModulePath
|
|
|
|
);
|
|
|
|
uncaughtError.type = 'ProcessTerminatedError';
|
|
|
|
throw uncaughtError;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw formatError(error, fileName);
|
|
|
|
});
|
2015-08-13 02:04:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Transformer;
|
|
|
|
|
|
|
|
Transformer.TransformError = TransformError;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-08-05 19:31:51 +00:00
|
|
|
function TransformError() {
|
|
|
|
Error.captureStackTrace && Error.captureStackTrace(this, TransformError);
|
|
|
|
}
|
2015-02-26 04:29:14 +00:00
|
|
|
util.inherits(TransformError, SyntaxError);
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-01 01:13:18 +00:00
|
|
|
function formatError(err, filename, source) {
|
2015-05-22 17:17:43 +00:00
|
|
|
if (err.loc) {
|
|
|
|
return formatBabelError(err, filename, source);
|
2015-03-01 01:13:18 +00:00
|
|
|
} else {
|
|
|
|
return formatGenericError(err, filename, source);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-03-01 01:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatGenericError(err, filename) {
|
|
|
|
var msg = 'TransformError: ' + filename + ': ' + err.message;
|
|
|
|
var error = new TransformError();
|
2015-04-08 21:28:44 +00:00
|
|
|
var stack = (err.stack || '').split('\n').slice(0, -1);
|
2015-03-01 01:13:18 +00:00
|
|
|
stack.push(msg);
|
|
|
|
error.stack = stack.join('\n');
|
|
|
|
error.message = msg;
|
|
|
|
error.type = 'TransformError';
|
|
|
|
return error;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-05-22 17:17:43 +00:00
|
|
|
function formatBabelError(err, filename) {
|
2015-02-20 04:10:52 +00:00
|
|
|
var error = new TransformError();
|
|
|
|
error.type = 'TransformError';
|
2015-05-22 17:17:43 +00:00
|
|
|
error.message = (err.type || error.type) + ' ' + err.message;
|
|
|
|
error.stack = err.stack;
|
|
|
|
error.snippet = err.codeFrame;
|
|
|
|
error.lineNumber = err.loc.line;
|
|
|
|
error.column = err.loc.column;
|
2015-02-20 04:10:52 +00:00
|
|
|
error.filename = filename;
|
2015-05-22 17:17:43 +00:00
|
|
|
error.description = err.message;
|
2015-02-20 04:10:52 +00:00
|
|
|
return error;
|
|
|
|
}
|