Updates from Sun Feb 15

- [react-packager][streamline oss] Injectable transformer | Amjad Masad
This commit is contained in:
Spencer Ahrens 2015-02-18 17:43:36 -08:00
parent ef842c285b
commit eeb0bf145d
5 changed files with 34 additions and 23 deletions

View File

@ -1,6 +1,6 @@
'use strict';
var transformer = require('../packager/react-packager/src/JSTransformer/transformer.js');
var transformer = require('../packager/transformer.js');
function transformSource(src) {
return transformer.transform(null, src).code;

View File

@ -84,10 +84,7 @@ function getAppMiddleware(options) {
projectRoots: options.projectRoots,
blacklistRE: blacklist(false),
cacheVersion: '2',
polyfillModuleNames: [
path.resolve(__dirname, 'polyfill/console.js'),
path.resolve(__dirname, 'polyfill/error-guard.js'),
]
transformModulePath: require.resolve('./transformer.js'),
});
}

View File

@ -8,9 +8,6 @@ var Cache = require('./Cache');
var _ = require('underscore');
var workerFarm = require('worker-farm');
var workers = workerFarm(require.resolve('./worker'));
warmupWorkers();
var readFile = q.nfbind(fs.readFile);
module.exports = Transformer;
@ -18,10 +15,14 @@ Transformer.TransformError = TransformError;
function Transformer(projectConfig) {
this._cache = new Cache(projectConfig);
this._workers = workerFarm(
{autoStart: true},
projectConfig.transformModulePath
);
}
Transformer.prototype.kill = function() {
workerFarm.end(workers);
workerFarm.end(this._workers);
return this._cache.end();
};
@ -36,6 +37,7 @@ Transformer.prototype.loadFileAndTransform = function(
filePath,
options
) {
var workers = this._workers;
return this._cache.get(filePath, function() {
return readFile(filePath)
.then(function(buffer) {
@ -62,19 +64,6 @@ Transformer.prototype.loadFileAndTransform = function(
});
};
// worker-farm module starts workers lazily. But we want them to take time
// to initialize so we send a dummy request.
// see https://github.com/rvagg/node-worker-farm/issues/23
function warmupWorkers() {
os.cpus().forEach(function() {
workers({
transformSets: ['es6'],
sourceCode: '\n',
options: {}
}, function() {});
});
}
function TransformError() {}
TransformError.__proto__ = SyntaxError.prototype;

View File

@ -18,6 +18,7 @@ function Server(options) {
cacheVersion: options.cacheVersion,
resetCache: options.resetCache,
dev: options.dev,
transformModulePath: options.transformModulePath
});
this._fileWatcher = new FileWatcher(options.projectRoots);

View File

@ -30,4 +30,28 @@ function transform(transformSets, srcTxt, options) {
return jstransform(visitorList, staticTypeSyntaxResult.code);
}
exports.transform = transform;
module.exports = function(data, callback) {
var result;
try {
result = transform(
data.transformSets,
data.sourceCode,
data.options
);
} catch (e) {
return callback(null, {
error: {
lineNumber: e.lineNumber,
column: e.column,
message: e.message,
stack: e.stack,
description: e.description
}
});
}
callback(null, result);
};
// export for use in jest
module.exports.transform = transform;