Bind files to workers

Reviewed By: davidaurelio

Differential Revision: D6284120

fbshipit-source-id: f674214fbbc2f2463e0180c5dae45ace7f9c1563
This commit is contained in:
Miguel Jimenez Esun 2018-01-16 03:16:12 -08:00 committed by Facebook Github Bot
parent 69ae428091
commit cc7ceebaff
4 changed files with 22 additions and 9 deletions

View File

@ -66,10 +66,10 @@ describe('Transformer', function() {
); );
expect(api.transform).toBeCalledWith( expect(api.transform).toBeCalledWith(
transformModulePath,
fileName, fileName,
localPath, localPath,
code, code,
transformModulePath,
false, false,
transformOptions, transformOptions,
[], [],
@ -83,7 +83,7 @@ describe('Transformer', function() {
const snippet = 'snippet'; const snippet = 'snippet';
api.transform.mockImplementation( api.transform.mockImplementation(
(transformPath, filename, localPth, code, opts) => { (filename, localPth, code, transformPath, opts) => {
const babelError = new SyntaxError(message); const babelError = new SyntaxError(message);
babelError.type = 'SyntaxError'; babelError.type = 'SyntaxError';

View File

@ -49,6 +49,7 @@ module.exports = class Transformer {
if (maxWorkers > 1) { if (maxWorkers > 1) {
this._worker = this._makeFarm( this._worker = this._makeFarm(
workerPath, workerPath,
this._computeWorkerKey,
['minify', 'transform'], ['minify', 'transform'],
maxWorkers, maxWorkers,
); );
@ -92,10 +93,10 @@ module.exports = class Transformer {
debug('Started ransforming file', filename); debug('Started ransforming file', filename);
const data = await this._worker.transform( const data = await this._worker.transform(
this._transformModulePath,
filename, filename,
localPath, localPath,
code, code,
this._transformModulePath,
isScript, isScript,
options, options,
assetExts, assetExts,
@ -119,7 +120,7 @@ module.exports = class Transformer {
} }
} }
_makeFarm(workerPath, exposedMethods, maxWorkers) { _makeFarm(workerPath, computeWorkerKey, exposedMethods, maxWorkers) {
// We whitelist only what would work. For example `--inspect` doesn't work // We whitelist only what would work. For example `--inspect` doesn't work
// in the workers because it tries to open the same debugging port. Feel // in the workers because it tries to open the same debugging port. Feel
// free to add more cases to the RegExp. A whitelist is preferred, to // free to add more cases to the RegExp. A whitelist is preferred, to
@ -132,12 +133,24 @@ module.exports = class Transformer {
); );
return new Worker(workerPath, { return new Worker(workerPath, {
computeWorkerKey,
exposedMethods, exposedMethods,
forkOptions: {execArgv}, forkOptions: {execArgv},
maxWorkers, maxWorkers,
}); });
} }
_computeWorkerKey(method: string, filename: string): ?string {
// Only when transforming a file we want to stick to the same worker; and
// we'll shard by file path. If not; we return null, which tells the worker
// to pick the first available one.
if (method === 'transform') {
return filename;
}
return null;
}
_formatGenericError(err, filename) { _formatGenericError(err, filename) {
const error = new TransformError(`${filename}: ${err.message}`); const error = new TransformError(`${filename}: ${err.message}`);

View File

@ -23,10 +23,10 @@ const {InvalidRequireCallError} = require('..');
describe('code transformation worker:', () => { describe('code transformation worker:', () => {
it('transforms a simple script', async () => { it('transforms a simple script', async () => {
const {result} = await transformCode( const {result} = await transformCode(
path.join(__dirname, '../../../transformer.js'),
'arbitrary/file.js', 'arbitrary/file.js',
`local/file.js`, `local/file.js`,
'someReallyArbitrary(code)', 'someReallyArbitrary(code)',
path.join(__dirname, '../../../transformer.js'),
true, true,
{ {
dev: true, dev: true,
@ -49,10 +49,10 @@ describe('code transformation worker:', () => {
it('transforms a simple module', async () => { it('transforms a simple module', async () => {
const {result} = await transformCode( const {result} = await transformCode(
path.join(__dirname, '../../../transformer.js'),
'arbitrary/file.js', 'arbitrary/file.js',
`local/file.js`, `local/file.js`,
'arbitrary(code)', 'arbitrary(code)',
path.join(__dirname, '../../../transformer.js'),
false, false,
{ {
dev: true, dev: true,
@ -75,7 +75,6 @@ describe('code transformation worker:', () => {
it('transforms a module with dependencies', async () => { it('transforms a module with dependencies', async () => {
const {result} = await transformCode( const {result} = await transformCode(
path.join(__dirname, '../../../transformer.js'),
'arbitrary/file.js', 'arbitrary/file.js',
`local/file.js`, `local/file.js`,
[ [
@ -84,6 +83,7 @@ describe('code transformation worker:', () => {
'const b = require("b");', 'const b = require("b");',
'import c from "./c";', 'import c from "./c";',
].join('\n'), ].join('\n'),
path.join(__dirname, '../../../transformer.js'),
false, false,
{ {
dev: true, dev: true,
@ -115,7 +115,6 @@ describe('code transformation worker:', () => {
it('reports filename when encountering unsupported dynamic dependency', async () => { it('reports filename when encountering unsupported dynamic dependency', async () => {
try { try {
await transformCode( await transformCode(
path.join(__dirname, '../../../transformer.js'),
'arbitrary/file.js', 'arbitrary/file.js',
`local/file.js`, `local/file.js`,
[ [
@ -123,6 +122,7 @@ describe('code transformation worker:', () => {
'let a = arbitrary(code);', 'let a = arbitrary(code);',
'const b = require(a);', 'const b = require(a);',
].join('\n'), ].join('\n'),
path.join(__dirname, '../../../transformer.js'),
false, false,
{ {
dev: true, dev: true,

View File

@ -163,10 +163,10 @@ function postTransform(
} }
function transformCode( function transformCode(
transformerPath: string,
filename: string, filename: string,
localPath: LocalPath, localPath: LocalPath,
sourceCode: string, sourceCode: string,
transformerPath: string,
isScript: boolean, isScript: boolean,
options: Options, options: Options,
assetExts: $ReadOnlyArray<string>, assetExts: $ReadOnlyArray<string>,