diff --git a/packages/metro-bundler/src/ModuleGraph/worker.js b/packages/metro-bundler/src/ModuleGraph/worker.js index 16ee3cf6..906f39fd 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker.js +++ b/packages/metro-bundler/src/ModuleGraph/worker.js @@ -10,20 +10,15 @@ */ 'use strict'; -const asyncify: Asyncify = require('async/asyncify'); const optimizeModule = require('./worker/optimize-module'); const transformModule = require('./worker/transform-module'); const wrapWorkerFn = require('./worker/wrap-worker-fn'); -import type {Callback} from './types.flow'; import type {OptimizationOptions} from './worker/optimize-module'; import type {TransformOptions} from './worker/transform-module'; import type {WorkerFnWithIO} from './worker/wrap-worker-fn'; -type Asyncify = ((A, B) => C) => (A, B, Callback) => void; - - exports.optimizeModule = - (wrapWorkerFn(asyncify(optimizeModule)): WorkerFnWithIO); + (wrapWorkerFn(optimizeModule): WorkerFnWithIO); exports.transformModule = - (wrapWorkerFn(asyncify(transformModule)): WorkerFnWithIO); + (wrapWorkerFn(transformModule): WorkerFnWithIO); diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/wrap-worker-fn-test.js b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/wrap-worker-fn-test.js index 1989e630..a0329ce3 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/wrap-worker-fn-test.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/wrap-worker-fn-test.js @@ -27,72 +27,71 @@ describe('wrapWorkerFn:', () => { let workerFn, wrapped; beforeEach(() => { workerFn = fn(); - workerFn.stub.yields(); wrapped = wrapWorkerFn(workerFn); }); const fs = require('fs'); const mkdirp = require('mkdirp'); - it('reads the passed-in file synchronously as buffer', done => { - wrapped(infile, outfile, {}, () => { - expect(fs.readFileSync).toBeCalledWith(infile); - done(); - }); + it('reads the passed-in file synchronously as buffer', () => { + wrapped(infile, outfile, {}); + expect(fs.readFileSync).toBeCalledWith(infile); }); - it('calls the worker function with file contents and options', done => { + it('calls the worker function with file contents and options', () => { const contents = 'arbitrary(contents);'; const options = {arbitrary: 'options'}; fs.readFileSync.mockReturnValue(contents); - wrapped(infile, outfile, options, () => { - expect(workerFn).toBeCalledWith(contents, options, any(Function)); - done(); - }); + wrapped(infile, outfile, options); + expect(workerFn).toBeCalledWith(contents, options); }); - it('passes through any error that the worker function calls back with', done => { + it('passes through any error that the worker function calls back with', () => { const error = new Error(); - workerFn.stub.yields(error); - wrapped(infile, outfile, {}, e => { + workerFn.stub.throws(error); + try { + wrapped(infile, outfile, {}); + throw new Error('should not reach'); + } catch (e) { expect(e).toBe(error); - done(); - }); + } }); - it('writes the result to disk', done => { + it('writes the result to disk', () => { const result = {arbitrary: 'result'}; - workerFn.stub.yields(null, result); - wrapped(infile, outfile, {}, () => { - expect(mkdirp.sync).toBeCalledWith(dirname(outfile)); - expect(fs.writeFileSync).toBeCalledWith( - outfile, - JSON.stringify(result), - 'utf8', - ); - done(); - }); + workerFn.stub.returns(result); + wrapped(infile, outfile, {}); + expect(mkdirp.sync).toBeCalledWith(dirname(outfile)); + expect(fs.writeFileSync).toBeCalledWith( + outfile, + JSON.stringify(result), + 'utf8', + ); }); - it('calls back with any error thrown by `mkdirp.sync`', done => { + it('calls back with any error thrown by `mkdirp.sync`', () => { const error = new Error(); mkdirp.sync.mockImplementationOnce(() => { throw error; }); - wrapped(infile, outfile, {}, e => { + try { + wrapped(infile, outfile, {}); + throw new Error('should not reach'); + } catch (e) { expect(e).toBe(error); - done(); - }); + } }); - it('calls back with any error thrown by `fs.writeFileSync`', done => { + it('calls back with any error thrown by `fs.writeFileSync`', () => { const error = new Error(); fs.writeFileSync.mockImplementationOnce(() => { throw error; }); - wrapped(infile, outfile, {}, e => { + try { + wrapped(infile, outfile, {}); + throw new Error('should not reach'); + } catch (e) { expect(e).toBe(error); - done(); - }); + } }); }); diff --git a/packages/metro-bundler/src/ModuleGraph/worker/wrap-worker-fn.js b/packages/metro-bundler/src/ModuleGraph/worker/wrap-worker-fn.js index 0b43d779..732bb774 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/wrap-worker-fn.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/wrap-worker-fn.js @@ -17,47 +17,22 @@ const mkdirp = require('mkdirp'); const {dirname} = require('path'); -import type {Callback} from '../types.flow'; - type Path = string; -type WorkerFn = ( - fileContents: Buffer, - options: Options, - callback: Callback, -) => void; +type WorkerFn = (fileContents: Buffer, options: Options) => mixed; export type WorkerFnWithIO = ( infile: Path, outfile: Path, options: Options, - callback: Callback<>, ) => void; function wrapWorkerFn( workerFunction: WorkerFn, ): WorkerFnWithIO { - return ( - infile: Path, - outfile: Path, - options: Options, - callback: Callback<>, - ) => { + return (infile: Path, outfile: Path, options: Options) => { const contents = fs.readFileSync(infile); - workerFunction(contents, options, (error, result) => { - if (error) { - callback(error); - return; - } - - try { - mkdirp.sync(dirname(outfile)); - fs.writeFileSync(outfile, JSON.stringify(result), 'utf8'); - } catch (writeError) { - callback(writeError); - return; - } - - callback(null); - }); + const result = workerFunction(contents, options); + mkdirp.sync(dirname(outfile)); + fs.writeFileSync(outfile, JSON.stringify(result), 'utf8'); }; }