From 8ad2ab3b5eabdc55e9bdf462bb2fdcc16d16ea14 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Mon, 28 Nov 2016 07:27:09 -0800 Subject: [PATCH] BREAKING: expose `getTransformOptions` directly in configuration Summary: Instead of exposing a `getTransformOptionsModulePath` function in configurations, we can simply expose a `getTransformOptions` *function*. The necessity of exposing a path comes from the olden days, where we had a server listening on a socket, and a client, talking to that server. Since that architectural gem no longer exists, we can use functions directly, rather than passing paths to modules around. Reviewed By: cpojer Differential Revision: D4233551 fbshipit-source-id: ec1acef8e6495a2f1fd0911a5613c144e8ffd7c3 --- local-cli/bundle/buildBundle.js | 2 +- local-cli/dependencies/dependencies.js | 2 +- local-cli/server/runServer.js | 2 +- local-cli/util/Config.js | 4 +++- packager/README.md | 5 ++--- packager/react-packager/src/Bundler/index.js | 16 ++++++---------- packager/react-packager/src/Server/index.js | 4 ++-- 7 files changed, 16 insertions(+), 19 deletions(-) diff --git a/local-cli/bundle/buildBundle.js b/local-cli/bundle/buildBundle.js index 859943f99ec..a2ed179290d 100644 --- a/local-cli/bundle/buildBundle.js +++ b/local-cli/bundle/buildBundle.js @@ -52,7 +52,7 @@ function buildBundle(args, config, output = outputBundle, packagerInstance) { projectRoots: config.getProjectRoots(), assetExts: defaultAssetExts.concat(assetExts), blacklistRE: config.getBlacklistRE(), - getTransformOptionsModulePath: config.getTransformOptionsModulePath, + getTransformOptions: config.getTransformOptions, transformModulePath: transformModulePath, extraNodeModules: config.extraNodeModules, resetCache: args.resetCache, diff --git a/local-cli/dependencies/dependencies.js b/local-cli/dependencies/dependencies.js index f0ebc1dd279..45ff7a9508e 100644 --- a/local-cli/dependencies/dependencies.js +++ b/local-cli/dependencies/dependencies.js @@ -28,7 +28,7 @@ function dependencies(argv, config, args, packagerInstance) { const packageOpts = { projectRoots: config.getProjectRoots(), blacklistRE: config.getBlacklistRE(), - getTransformOptionsModulePath: config.getTransformOptionsModulePath, + getTransformOptions: config.getTransformOptions, transformModulePath: transformModulePath, extraNodeModules: config.extraNodeModules, verbose: config.verbose, diff --git a/local-cli/server/runServer.js b/local-cli/server/runServer.js index c2423e69891..1bdac4ab0eb 100644 --- a/local-cli/server/runServer.js +++ b/local-cli/server/runServer.js @@ -90,7 +90,7 @@ function getPackagerServer(args, config) { blacklistRE: config.getBlacklistRE(), cacheVersion: '3', extraNodeModules: config.extraNodeModules, - getTransformOptionsModulePath: config.getTransformOptionsModulePath, + getTransformOptions: config.getTransformOptions, projectRoots: args.projectRoots, resetCache: args.resetCache, transformModulePath: transformModulePath, diff --git a/local-cli/util/Config.js b/local-cli/util/Config.js index 2f7b0e0141d..db4e5b18e29 100644 --- a/local-cli/util/Config.js +++ b/local-cli/util/Config.js @@ -14,13 +14,15 @@ const assert = require('assert'); const fs = require('fs'); const path = require('path'); +import type {GetTransformOptions} from '../../packager/react-packager/src/Bundler/index.js'; + const RN_CLI_CONFIG = 'rn-cli.config.js'; export type ConfigT = { extraNodeModules?: {[id: string]: string}, getAssetExts?: () => Array, getTransformModulePath?: () => string, - getTransformOptionsModulePath?: () => string, + getTransformOptions?: GetTransformOptions<*>, transformVariants?: () => {[name: string]: Object}, getBlacklistRE(): RegExp, diff --git a/packager/README.md b/packager/README.md index 2bf09ebfbb4..79bf3b7f578 100644 --- a/packager/README.md +++ b/packager/README.md @@ -110,9 +110,8 @@ Builds a bundle according to the provided options. * `nonPersistent` boolean, defaults to false: Whether the server should be used as a persistent deamon to watch files and update itself -* `getTransformOptionsModulePath` string: Path to module that exports a function - that acts as a middleware for generating options to pass to the transformer - based on the bundle being built. +* `getTransformOptions` function: A function that acts as a middleware for + generating options to pass to the transformer based on the bundle being built. #### `bundleOptions` diff --git a/packager/react-packager/src/Bundler/index.js b/packager/react-packager/src/Bundler/index.js index d95a7a1df10..374fbb421d5 100644 --- a/packager/react-packager/src/Bundler/index.js +++ b/packager/react-packager/src/Bundler/index.js @@ -36,7 +36,7 @@ import AssetServer from '../AssetServer'; import Module from '../node-haste/Module'; import ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse'; -export type TransformOptionsModule = ( +export type GetTransformOptions = ( string, Object, string => Promise>, @@ -121,6 +121,7 @@ type Options = { cacheVersion: string, resetCache: boolean, transformModulePath: string, + getTransformOptions?: GetTransformOptions<*>, extraNodeModules: {}, assetExts: Array, watch: boolean, @@ -138,7 +139,7 @@ class Bundler { _resolver: Resolver; _projectRoots: Array; _assetServer: AssetServer; - _transformOptionsModule: TransformOptionsModule<*>; + _getTransformOptions: void | GetTransformOptions<*>; constructor(options: Options) { const opts = this._opts = validateOpts(options); @@ -204,12 +205,7 @@ class Bundler { this._projectRoots = opts.projectRoots; this._assetServer = opts.assetServer; - if (opts.getTransformOptionsModulePath) { - /* $FlowFixMe: dynamic requires prevent static typing :'( */ - this._transformOptionsModule = require( - opts.getTransformOptionsModulePath - ); - } + this._getTransformOptions = opts.getTransformOptions; } end() { @@ -744,8 +740,8 @@ class Bundler { const getDependencies = (entryFile: string) => this.getDependencies({...options, entryFile}) .then(r => r.dependencies.map(d => d.path)); - const extraOptions = this._transformOptionsModule - ? this._transformOptionsModule(mainModuleName, options, getDependencies) + const extraOptions = this._getTransformOptions + ? this._getTransformOptions(mainModuleName, options, getDependencies) : null; return Promise.resolve(extraOptions) .then(extraOpts => Object.assign(options, extraOpts)); diff --git a/packager/react-packager/src/Server/index.js b/packager/react-packager/src/Server/index.js index 5532c81274b..2d52c5213d7 100644 --- a/packager/react-packager/src/Server/index.js +++ b/packager/react-packager/src/Server/index.js @@ -87,8 +87,8 @@ const validateOpts = declareOpts({ type: 'number', required: false, }, - getTransformOptionsModulePath: { - type: 'string', + getTransformOptions: { + type: 'function', required: false, }, silent: {