mirror of https://github.com/status-im/metro.git
Updates from Sun Feb 15
- [react-packager][streamline oss] Injectable transformer | Amjad Masad
This commit is contained in:
parent
0bbd32514e
commit
6cbd5fb941
|
@ -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'),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
Loading…
Reference in New Issue