diff --git a/packages/metro/src/JSTransformer/__tests__/Transformer-test.js b/packages/metro/src/JSTransformer/__tests__/Transformer-test.js index 0395ba6e..654a3364 100644 --- a/packages/metro/src/JSTransformer/__tests__/Transformer-test.js +++ b/packages/metro/src/JSTransformer/__tests__/Transformer-test.js @@ -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'; diff --git a/packages/metro/src/JSTransformer/index.js b/packages/metro/src/JSTransformer/index.js index c3f03454..737d77e1 100644 --- a/packages/metro/src/JSTransformer/index.js +++ b/packages/metro/src/JSTransformer/index.js @@ -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}`); diff --git a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js index 4255306d..92958d82 100644 --- a/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js +++ b/packages/metro/src/JSTransformer/worker/__tests__/worker-test.js @@ -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, diff --git a/packages/metro/src/JSTransformer/worker/index.js b/packages/metro/src/JSTransformer/worker/index.js index 6eabc3bf..0e0adabd 100644 --- a/packages/metro/src/JSTransformer/worker/index.js +++ b/packages/metro/src/JSTransformer/worker/index.js @@ -163,10 +163,10 @@ function postTransform( } function transformCode( - transformerPath: string, filename: string, localPath: LocalPath, sourceCode: string, + transformerPath: string, isScript: boolean, options: Options, assetExts: $ReadOnlyArray,