From fb9d114861d27676b42edd16fa74ddf54b3da113 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Mon, 28 Nov 2016 07:27:07 -0800 Subject: [PATCH] Simplify contract of `getTransformOptionsModulePath` Summary: instead of passing the `Bundler` instance, the transform options module now receives a `getDependencies()` function. The idea is to make the contract stricter, to integrate more easily with the new `ModuleGraph` system. Reviewed By: cpojer Differential Revision: D4233529 fbshipit-source-id: 1c6d462c7dae1c61cbf45fd13989ce77f76e7384 --- react-packager/src/Bundler/index.js | 35 +++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index ced2be34..d95a7a1d 100644 --- a/react-packager/src/Bundler/index.js +++ b/react-packager/src/Bundler/index.js @@ -36,6 +36,12 @@ import AssetServer from '../AssetServer'; import Module from '../node-haste/Module'; import ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; +export type TransformOptionsModule = ( + string, + Object, + string => Promise>, +) => T | Promise; + const sizeOf = denodeify(imageSize); const noop = () => {}; @@ -132,7 +138,7 @@ class Bundler { _resolver: Resolver; _projectRoots: Array; _assetServer: AssetServer; - _transformOptionsModule: (path: string, options: {}, bunler: Bundler) => {}; + _transformOptionsModule: TransformOptionsModule<*>; constructor(options: Options) { const opts = this._opts = validateOpts(options); @@ -274,7 +280,7 @@ class Bundler { ); } - hmrBundle(options: {platform: mixed}, host: string, port: number) { + hmrBundle(options: {platform: ?string}, host: string, port: number) { return this._bundle({ ...options, bundle: new HMRBundle({ @@ -466,7 +472,7 @@ class Bundler { generateSourceMaps = false, }: { entryFile: string, - platform: mixed, + platform: ?string, dev?: boolean, minify?: boolean, hot?: boolean, @@ -513,7 +519,7 @@ class Bundler { onProgress, }: { entryFile: string, - platform: mixed, + platform: ?string, dev?: boolean, minify?: boolean, hot?: boolean, @@ -552,7 +558,7 @@ class Bundler { getOrderedDependencyPaths({ entryFile, dev, platform }: { entryFile: string, dev: boolean, - platform: mixed, + platform: ?string, }) { return this.getDependencies({entryFile, dev, platform}).then( ({ dependencies }) => { @@ -630,7 +636,7 @@ class Bundler { }); } - _generateAssetObjAndCode(module, assetPlugins, platform: mixed = null) { + _generateAssetObjAndCode(module, assetPlugins, platform: ?string = null) { const relPath = getPathRelativeToRoot(this._projectRoots, module.path); var assetUrlPath = joinPath('/assets', pathDirname(relPath)); @@ -707,7 +713,7 @@ class Bundler { module, moduleId, assetPlugins: Array = [], - platform: mixed = null, + platform: ?string = null, ) { return Promise.all([ module.getName(), @@ -726,9 +732,20 @@ class Bundler { }); } - getTransformOptions(mainModuleName: string, options: {}) { + getTransformOptions( + mainModuleName: string, + options: { + dev?: boolean, + platform: ?string, + hot?: boolean, + generateSourceMaps?: boolean, + }, + ) { + const getDependencies = (entryFile: string) => + this.getDependencies({...options, entryFile}) + .then(r => r.dependencies.map(d => d.path)); const extraOptions = this._transformOptionsModule - ? this._transformOptionsModule(mainModuleName, options, this) + ? this._transformOptionsModule(mainModuleName, options, getDependencies) : null; return Promise.resolve(extraOptions) .then(extraOpts => Object.assign(options, extraOpts));