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

View File

@ -49,6 +49,7 @@ module.exports = class Transformer {
if (maxWorkers > 1) {
this._worker = this._makeFarm(
workerPath,
this._computeWorkerKey,
['minify', 'transform'],
maxWorkers,
);
@ -92,10 +93,10 @@ module.exports = class Transformer {
debug('Started ransforming file', filename);
const data = await this._worker.transform(
this._transformModulePath,
filename,
localPath,
code,
this._transformModulePath,
isScript,
options,
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
// 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
@ -132,12 +133,24 @@ module.exports = class Transformer {
);
return new Worker(workerPath, {
computeWorkerKey,
exposedMethods,
forkOptions: {execArgv},
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) {
const error = new TransformError(`${filename}: ${err.message}`);

View File

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

View File

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