Skip to content

Commit

Permalink
Updates from Sun Feb 15
Browse files Browse the repository at this point in the history
- [react-packager][streamline oss] Injectable transformer | Amjad Masad
  • Loading branch information
sahrens committed Feb 19, 2015
1 parent ef842c2 commit eeb0bf1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion jestSupport/scriptPreprocess.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
5 changes: 1 addition & 4 deletions packager/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
});
}

Expand Down
23 changes: 6 additions & 17 deletions packager/react-packager/src/JSTransformer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ 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;
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();
};

Expand All @@ -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) {
Expand All @@ -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;

Expand Down
1 change: 1 addition & 0 deletions packager/react-packager/src/Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit eeb0bf1

Please sign in to comment.