From 797a07ccc227f186c35ba9604fc661d87577f303 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Thu, 11 May 2017 08:32:04 -0700 Subject: [PATCH] BREAKING: Pass parameters to `transformer.transform` as object Reviewed By: jeanlauliac Differential Revision: D5037155 fbshipit-source-id: fbd18bd62254d7e1522f659ea62055e66748a951 --- packages/metro-bundler/package.json | 2 +- .../worker/__tests__/worker-test.js | 18 +++++++++--------- .../src/JSTransformer/worker/worker.js | 14 +++++++++----- .../worker/__tests__/transform-module-test.js | 14 ++++++++++---- .../src/ModuleGraph/worker/transform-module.js | 8 ++++---- packages/metro-bundler/transformer.js | 13 ++++++++----- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/packages/metro-bundler/package.json b/packages/metro-bundler/package.json index e6386cc9..e04a905a 100644 --- a/packages/metro-bundler/package.json +++ b/packages/metro-bundler/package.json @@ -1,5 +1,5 @@ { - "version": "0.5.1", + "version": "0.6.0", "name": "react-native-packager", "description": "Build native apps with React!", "repository": { diff --git a/packages/metro-bundler/src/JSTransformer/worker/__tests__/worker-test.js b/packages/metro-bundler/src/JSTransformer/worker/__tests__/worker-test.js index d3512e43..262deeac 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/__tests__/worker-test.js +++ b/packages/metro-bundler/src/JSTransformer/worker/__tests__/worker-test.js @@ -26,7 +26,7 @@ describe('code transformation worker:', () => { extractDependencies = require('../extract-dependencies').mockReturnValue({}); transformer = { - transform: jest.fn((src, filename, options) => ({ + transform: jest.fn(({filename, options, src}) => ({ code: src, map: {}, })), @@ -38,22 +38,22 @@ describe('code transformation worker:', () => { const sourceCode = 'arbitrary(code)'; const transformOptions = {arbitrary: 'options'}; transformCode(transformer, filename, sourceCode, {transform: transformOptions}, () => {}); - expect(transformer.transform).toBeCalledWith( - sourceCode, + expect(transformer.transform).toBeCalledWith({ filename, - transformOptions, - ); + options: transformOptions, + src: sourceCode, + }); }); it('prefixes JSON files with an assignment to module.exports to make the code valid', function() { const filename = 'arbitrary/file.json'; const sourceCode = '{"arbitrary":"property"}'; transformCode(transformer, filename, sourceCode, {}, () => {}); - expect(transformer.transform).toBeCalledWith( - `module.exports=${sourceCode}`, + expect(transformer.transform).toBeCalledWith({ filename, - undefined, - ); + options: undefined, + src: `module.exports=${sourceCode}`, + }); }); it('calls back with the result of the transform in the cache', done => { diff --git a/packages/metro-bundler/src/JSTransformer/worker/worker.js b/packages/metro-bundler/src/JSTransformer/worker/worker.js index c674a1be..f1890c95 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/worker.js +++ b/packages/metro-bundler/src/JSTransformer/worker/worker.js @@ -29,13 +29,13 @@ export type TransformedCode = { }; export type Transformer = { - transform: ( + transform: ({| filename: string, - sourceCode: string, options: ExtraOptions & TransformOptions, plugins?: BabelPlugins, - ) => {ast: ?Ast, code: string, map: ?MappingsMap}, - getCacheKey: TransformOptionsStrict => string, + src: string, + |}) => {ast: ?Ast, code: string, map: ?MappingsMap}, + getCacheKey: () => string, }; @@ -102,7 +102,11 @@ function transformCode( let transformed; try { - transformed = transformer.transform(sourceCode, filename, options.transform); + transformed = transformer.transform({ + filename, + options: options.transform, + src: sourceCode, + }); } catch (error) { callback(error); return; diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js index bb4af0f7..ef45c173 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js @@ -95,10 +95,16 @@ describe('transforming JS modules:', () => { const variants = {dev: {dev: true}, prod: {dev: false}}; transformModule(sourceCode, options(variants), () => { - expect(transformer.transform) - .toBeCalledWith(sourceCode, filename, {...defaults, ...variants.dev}); - expect(transformer.transform) - .toBeCalledWith(sourceCode, filename, {...defaults, ...variants.prod}); + expect(transformer.transform).toBeCalledWith({ + filename, + options: {...defaults, ...variants.dev}, + src: sourceCode, + }); + expect(transformer.transform).toBeCalledWith({ + filename, + options: {...defaults, ...variants.prod}, + src: sourceCode, + }); done(); }); }, diff --git a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js index 47dcd0a2..38e5a600 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js @@ -70,11 +70,11 @@ function transformModule( const {filename, transformer, variants = defaultVariants} = options; const tasks = {}; Object.keys(variants).forEach(name => { - tasks[name] = asyncify(() => transformer.transform( - code, + tasks[name] = asyncify(() => transformer.transform({ filename, - {...defaultTransformOptions, ...variants[name]}, - ) + options: {...defaultTransformOptions, ...variants[name]}, + src: code, + }) ); }); diff --git a/packages/metro-bundler/transformer.js b/packages/metro-bundler/transformer.js index 55447ee4..1aefd50e 100644 --- a/packages/metro-bundler/transformer.js +++ b/packages/metro-bundler/transformer.js @@ -112,11 +112,14 @@ function buildBabelConfig(filename, options) { return Object.assign({}, babelRC, config); } -function transform( - src: string, +type Params = { filename: string, - options, -) { + options: TransformOptions, + plugins?: BabelPlugins, + src: string, +}; + +function transform({filename, options, src}: Params) { options = options || {}; const OLD_BABEL_ENV = process.env.BABEL_ENV; @@ -154,7 +157,7 @@ function transform( } } -function getCacheKey(options: TransformOptions) { +function getCacheKey() { var key = crypto.createHash('md5'); cacheKeyParts.forEach(part => key.update(part)); return key.digest('hex');