From ca24ab1da6f6fad3e0db45c0962c19c296a0c309 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Fri, 21 Apr 2017 08:40:38 -0700 Subject: [PATCH] Stronger typing for transform options / remove duplication Reviewed By: jeanlauliac Differential Revision: D4929276 fbshipit-source-id: 0b23435a1502c72377425cae775e258106a5bf14 --- packages/metro-bundler/src/Bundler/Bundle.js | 4 +- packages/metro-bundler/src/Bundler/index.js | 43 +++++++++++-------- .../src/JSTransformer/worker/worker.js | 2 +- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/packages/metro-bundler/src/Bundler/Bundle.js b/packages/metro-bundler/src/Bundler/Bundle.js index 2823e7ed..582853ec 100644 --- a/packages/metro-bundler/src/Bundler/Bundle.js +++ b/packages/metro-bundler/src/Bundler/Bundle.js @@ -41,7 +41,7 @@ class Bundle extends BundleBase { _minify: boolean | void; _numRequireCalls: number; _ramBundle: Unbundle | null; - _ramGroups: Array | void; + _ramGroups: ?Array; _sourceMap: string | null; _sourceMapFormat: SourceMapFormat; _sourceMapUrl: ?string; @@ -309,7 +309,7 @@ class Bundle extends BundleBase { ].join('\n'); } - setRamGroups(ramGroups: Array) { + setRamGroups(ramGroups: ?Array) { this._ramGroups = ramGroups; } } diff --git a/packages/metro-bundler/src/Bundler/index.js b/packages/metro-bundler/src/Bundler/index.js index 98a42654..b195496c 100644 --- a/packages/metro-bundler/src/Bundler/index.js +++ b/packages/metro-bundler/src/Bundler/index.js @@ -47,16 +47,22 @@ import type { import type {Reporter} from '../lib/reporting'; import type {GlobalTransformCache} from '../lib/GlobalTransformCache'; -export type ExtraTransformOptions = { +export type ExtraTransformOptions = {| +inlineRequires?: {+blacklist: {[string]: true}} | boolean, - +preloadedModules?: Array | false, + +preloadedModules?: {[path: string]: true} | false, +ramGroups?: Array, -}; +|}; + +export type GetTransformOptionsOpts = {| + dev: boolean, + hot: boolean, + platform: string, +|}; export type GetTransformOptions = ( mainModuleName: string, - options: {}, - getDependencies: string => Promise>, + options: GetTransformOptionsOpts, + getDependenciesOf: string => Promise>, ) => Promise; type Asset = {| @@ -749,7 +755,7 @@ class Bundler { }); } - getTransformOptions( + async getTransformOptions( mainModuleName: string, options: {| dev: boolean, @@ -758,24 +764,25 @@ class Bundler { platform: string, projectRoots: Array, |}, - ): Promise { + ): Promise { const getDependencies = (entryFile: string) => this.getDependencies({...options, entryFile}) .then(r => r.dependencies.map(d => d.path)); - const extraOptions: Promise = this._getTransformOptions - ? this._getTransformOptions(mainModuleName, options, getDependencies) - : Promise.resolve({}); - return extraOptions.then(extraOpts => ({ - dev: options.dev, + const {dev, hot, platform} = options; + const extraOptions = this._getTransformOptions + ? await this._getTransformOptions(mainModuleName, {dev, hot, platform}, getDependencies) + : {}; + return { + dev, generateSourceMaps: options.generateSourceMaps, - hot: options.hot, - inlineRequires: extraOpts.inlineRequires || false, - platform: options.platform, - preloadedModules: extraOpts.preloadedModules, + hot, + inlineRequires: extraOptions.inlineRequires || false, + platform, + preloadedModules: extraOptions.preloadedModules, projectRoots: options.projectRoots, - ramGroups: extraOpts.ramGroups, - })); + ramGroups: extraOptions.ramGroups, + }; } getResolver(): Promise { diff --git a/packages/metro-bundler/src/JSTransformer/worker/worker.js b/packages/metro-bundler/src/JSTransformer/worker/worker.js index 0e075651..d7b1b588 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/worker.js +++ b/packages/metro-bundler/src/JSTransformer/worker/worker.js @@ -41,7 +41,7 @@ export type TransformOptions = {| +hot: boolean, +inlineRequires: {+blacklist: {[string]: true}} | boolean, +platform: string, - +preloadedModules: ?Array | false, + +preloadedModules: ?{[string]: true} | false, +projectRoots: Array, +ramGroups: ?Array, |};