From 5aa19c7626cf8cb3bac58e17d8276f06f451081f Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Tue, 20 Feb 2018 03:56:19 -0800 Subject: [PATCH] Add minifier-path option to Metro Summary: This exposes the `minifierPath` externally and allows it to be adjusted through the `--minifier-path` argument. This works for both the server (when starting the server) and running a single build through the CLI. In server mode, this can not be used per file request yet, though that should also be doable if need be. Reviewed By: rafeca Differential Revision: D6998562 fbshipit-source-id: 669b876c24fe117ec88b2200d48aa82658f568b4 --- packages/metro/src/Bundler/index.js | 4 ++-- .../src/DeltaBundler/__tests__/DeltaTransformer-test.js | 1 + packages/metro/src/Server/index.js | 8 ++++++++ packages/metro/src/index.js | 5 +++++ packages/metro/src/legacy.js | 1 + packages/metro/src/shared/types.flow.js | 1 + 6 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/metro/src/Bundler/index.js b/packages/metro/src/Bundler/index.js index 185ac224..0959d54e 100644 --- a/packages/metro/src/Bundler/index.js +++ b/packages/metro/src/Bundler/index.js @@ -89,6 +89,7 @@ export type Options = {| +globalTransformCache: ?GlobalTransformCache, +hasteImplModulePath?: string, +maxWorkers: number, + +minifierPath: string, +platforms: Array, +polyfillModuleNames: Array, +postMinifyProcess: PostMinifyProcess, @@ -120,8 +121,7 @@ class Bundler { this._transformer = new Transformer({ asyncRequireModulePath: opts.asyncRequireModulePath, maxWorkers: opts.maxWorkers, - // TODO t26063242 make this an option - minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH, + minifierPath: opts.minifierPath, reporters: { stdoutChunk: chunk => opts.reporter.update({type: 'worker_stdout_chunk', chunk}), diff --git a/packages/metro/src/DeltaBundler/__tests__/DeltaTransformer-test.js b/packages/metro/src/DeltaBundler/__tests__/DeltaTransformer-test.js index fcb11807..f6a1c807 100644 --- a/packages/metro/src/DeltaBundler/__tests__/DeltaTransformer-test.js +++ b/packages/metro/src/DeltaBundler/__tests__/DeltaTransformer-test.js @@ -33,6 +33,7 @@ const bundlerOptions = { cacheVersion: 'smth', enableBabelRCLookup: true, extraNodeModules: {}, + minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH, platforms: defaults.platforms, resetCache: false, sourceExts: defaults.sourceExts, diff --git a/packages/metro/src/Server/index.js b/packages/metro/src/Server/index.js index 895db5b6..da660d88 100644 --- a/packages/metro/src/Server/index.js +++ b/packages/metro/src/Server/index.js @@ -28,6 +28,7 @@ const symbolicate = require('./symbolicate'); const url = require('url'); const {getAsset} = require('../Assets'); +const resolveSync: ResolveSync = require('resolve').sync; import type {CustomError} from '../lib/formatBundlingError'; import type {IncomingMessage, ServerResponse} from 'http'; @@ -49,6 +50,8 @@ const { Logger: {createActionStartEntry, createActionEndEntry, log}, } = require('metro-core'); +type ResolveSync = (path: string, opts: ?{baseDir?: string}) => string; + function debounceAndBatch(fn, delay) { let timeout; return () => { @@ -71,6 +74,7 @@ class Server { getTransformOptions?: GetTransformOptions, hasteImplModulePath?: string, maxWorkers: number, + minifierPath: string, moduleFormat: string, platforms: Array, polyfillModuleNames: Array, @@ -127,6 +131,10 @@ class Server { globalTransformCache: options.globalTransformCache, hasteImplModulePath: options.hasteImplModulePath, maxWorkers, + minifierPath: + options.minifierPath == null + ? defaults.DEFAULT_METRO_MINIFIER_PATH + : resolveSync(options.minifierPath, {basedir: process.cwd()}), moduleFormat: options.moduleFormat != null ? options.moduleFormat : 'haste', platforms: options.platforms || defaults.platforms, diff --git a/packages/metro/src/index.js b/packages/metro/src/index.js index f87d0e00..596a4f2b 100644 --- a/packages/metro/src/index.js +++ b/packages/metro/src/index.js @@ -49,6 +49,7 @@ type PublicMetroOptions = {| config?: ConfigT, globalTransformCache?: ?GlobalTransformCache, maxWorkers?: number, + minifierPath?: string, port?: ?number, reporter?: Reporter, transformCache?: TransformCache, @@ -86,6 +87,7 @@ async function runMetro({ globalTransformCache, resetCache = false, maxWorkers = getMaxWorkers(), + minifierPath, // $FlowFixMe TODO t0 https://github.com/facebook/flow/issues/183 port = null, reporter = new TerminalReporter(new Terminal(process.stdout)), @@ -133,6 +135,7 @@ async function runMetro({ globalTransformCache, hasteImplModulePath: normalizedConfig.hasteImplModulePath, maxWorkers, + minifierPath, platforms: defaults.platforms.concat(platforms), postMinifyProcess: normalizedConfig.postMinifyProcess, postProcessModules: normalizedConfig.postProcessModules, @@ -208,6 +211,7 @@ type RunServerOptions = {| exports.runServer = async ({ host, onReady, + minifierPath, // $FlowFixMe Flow messes up when using "destructuring"+"default value"+"spread typing"+"stricter field typing" together port = 8080, reporter = new TerminalReporter(new Terminal(process.stdout)), @@ -231,6 +235,7 @@ exports.runServer = async ({ ...rest, port, reporter, + minifierPath, }); serverApp.use(middleware); diff --git a/packages/metro/src/legacy.js b/packages/metro/src/legacy.js index 1105f35b..dc2d42b6 100644 --- a/packages/metro/src/legacy.js +++ b/packages/metro/src/legacy.js @@ -183,6 +183,7 @@ function toServerOptions(options: Options): ServerOptions { globalTransformCache: options.globalTransformCache, hasteImplModulePath: options.hasteImplModulePath, maxWorkers: options.maxWorkers, + minifierPath: options.minifierPath, moduleFormat: options.moduleFormat, platforms: options.platforms, polyfillModuleNames: options.polyfillModuleNames, diff --git a/packages/metro/src/shared/types.flow.js b/packages/metro/src/shared/types.flow.js index 1087c27a..2051dfa3 100644 --- a/packages/metro/src/shared/types.flow.js +++ b/packages/metro/src/shared/types.flow.js @@ -83,6 +83,7 @@ export type Options = {| globalTransformCache: ?GlobalTransformCache, hasteImplModulePath?: string, maxWorkers?: number, + minifierPath?: string, moduleFormat?: string, platforms?: Array, polyfillModuleNames?: Array,