From 81bfef03898d366ff0f31792af41e8e99b0176ac Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Tue, 7 Nov 2017 07:31:39 -0800 Subject: [PATCH] metro-bundler: allow different run-before-main-module depending on entry point Reviewed By: davidaurelio Differential Revision: D6248242 fbshipit-source-id: 9471820fce926e676170e3024bd48c9d7335c1a7 --- packages/metro-bundler/src/Config.js | 4 ++-- .../src/Server/__tests__/Server-test.js | 2 +- packages/metro-bundler/src/Server/index.js | 12 +++++++----- packages/metro-bundler/src/index.js | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/metro-bundler/src/Config.js b/packages/metro-bundler/src/Config.js index e5d6f29b..1f13ff75 100644 --- a/packages/metro-bundler/src/Config.js +++ b/packages/metro-bundler/src/Config.js @@ -135,7 +135,7 @@ export type ConfigT = { * An array of modules to be required before the entry point. It should * contain the absolute path of each module. */ - runBeforeMainModule: Array, + getModulesRunBeforeMainModule: (entryFilePath: string) => Array, }; const DEFAULT = ({ @@ -158,7 +158,7 @@ const DEFAULT = ({ postProcessModules: modules => modules, postProcessModulesForBuck: modules => modules, postProcessBundleSourcemap: ({code, map, outFileName}) => ({code, map}), - runBeforeMainModule: [], + getModulesRunBeforeMainModule: () => [], transformVariants: () => ({default: {}}), getWorkerPath: () => null, }: ConfigT); diff --git a/packages/metro-bundler/src/Server/__tests__/Server-test.js b/packages/metro-bundler/src/Server/__tests__/Server-test.js index 9e96c6a9..e149eea9 100644 --- a/packages/metro-bundler/src/Server/__tests__/Server-test.js +++ b/packages/metro-bundler/src/Server/__tests__/Server-test.js @@ -55,7 +55,7 @@ describe('processRequest', () => { cacheVersion: null, polyfillModuleNames: null, reporter: require('../../lib/reporting').nullReporter, - runBeforeMainModule: ['InitializeCore'], + getModulesRunBeforeMainModule: () => ['InitializeCore'], }; const makeRequest = (reqHandler, requrl, reqOptions) => diff --git a/packages/metro-bundler/src/Server/index.js b/packages/metro-bundler/src/Server/index.js index 8622288a..52700c66 100644 --- a/packages/metro-bundler/src/Server/index.js +++ b/packages/metro-bundler/src/Server/index.js @@ -93,7 +93,7 @@ export type Options = {| providesModuleNodeModules?: Array, reporter?: Reporter, resetCache?: boolean, - +runBeforeMainModule: Array, + +getModulesRunBeforeMainModule: (entryPoint: string) => Array, silent?: boolean, +sourceExts: ?Array, +transformCache: TransformCache, @@ -167,7 +167,7 @@ class Server { providesModuleNodeModules?: Array, reporter: Reporter, resetCache: boolean, - +runBeforeMainModule: Array, + +getModulesRunBeforeMainModule: (entryFilePath: string) => Array, silent: boolean, +sourceExts: Array, +transformCache: TransformCache, @@ -207,6 +207,7 @@ class Server { ? options.enableBabelRCLookup : true, extraNodeModules: options.extraNodeModules || {}, + getModulesRunBeforeMainModule: options.getModulesRunBeforeMainModule, getPolyfills: options.getPolyfills, getTransformOptions: options.getTransformOptions, globalTransformCache: options.globalTransformCache, @@ -223,7 +224,6 @@ class Server { providesModuleNodeModules: options.providesModuleNodeModules, reporter, resetCache: options.resetCache || false, - runBeforeMainModule: options.runBeforeMainModule, silent: options.silent || false, sourceExts: options.sourceExts || defaults.sourceExts, transformCache: options.transformCache, @@ -325,7 +325,9 @@ class Server { async buildBundle(options: BundleOptions): Promise { const bundle = await this._bundler.bundle({ ...options, - runBeforeMainModule: this._opts.runBeforeMainModule, + runBeforeMainModule: this._opts.getModulesRunBeforeMainModule( + options.entryFile, + ), }); const modules = bundle.getModules(); const nonVirtual = modules.filter(m => !m.virtual); @@ -1306,7 +1308,7 @@ class Server { minify, excludeSource, hot: true, - runBeforeMainModule: this._opts.runBeforeMainModule, + runBeforeMainModule: this._opts.getModulesRunBeforeMainModule(entryFile), runModule: this._getBoolOptionFromQuery(urlObj.query, 'runModule', true), inlineSourceMap: this._getBoolOptionFromQuery( urlObj.query, diff --git a/packages/metro-bundler/src/index.js b/packages/metro-bundler/src/index.js index d0f7e737..3c801be1 100644 --- a/packages/metro-bundler/src/index.js +++ b/packages/metro-bundler/src/index.js @@ -178,6 +178,7 @@ function toServerOptions(options: Options): ServerOptions { cacheVersion: options.cacheVersion, enableBabelRCLookup: options.enableBabelRCLookup, extraNodeModules: options.extraNodeModules, + getModulesRunBeforeMainModule: options.getModulesRunBeforeMainModule, getPolyfills: options.getPolyfills, getTransformOptions: options.getTransformOptions, globalTransformCache: options.globalTransformCache, @@ -193,7 +194,6 @@ function toServerOptions(options: Options): ServerOptions { providesModuleNodeModules: options.providesModuleNodeModules, reporter: options.reporter, resetCache: options.resetCache, - runBeforeMainModule: options.runBeforeMainModule, silent: options.silent, sourceExts: options.sourceExts, transformCache: options.transformCache || TransformCaching.useTempDir(),