diff --git a/packages/metro/src/Bundler/__tests__/Bundler-test.js b/packages/metro/src/Bundler/__tests__/Bundler-test.js index fa9104a1..3f0756c4 100644 --- a/packages/metro/src/Bundler/__tests__/Bundler-test.js +++ b/packages/metro/src/Bundler/__tests__/Bundler-test.js @@ -37,6 +37,7 @@ var commonOptions = { cacheVersion: 'smth', enableBabelRCLookup: true, extraNodeModules: {}, + minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH, platforms: defaults.platforms, postMinifyProcess: e => e, resetCache: false, diff --git a/packages/metro/src/Bundler/index.js b/packages/metro/src/Bundler/index.js index aab42cea..185ac224 100644 --- a/packages/metro/src/Bundler/index.js +++ b/packages/metro/src/Bundler/index.js @@ -120,6 +120,8 @@ class Bundler { this._transformer = new Transformer({ asyncRequireModulePath: opts.asyncRequireModulePath, maxWorkers: opts.maxWorkers, + // TODO t26063242 make this an option + minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH, reporters: { stdoutChunk: chunk => opts.reporter.update({type: 'worker_stdout_chunk', chunk}), diff --git a/packages/metro/src/JSTransformer/__tests__/Transformer-test.js b/packages/metro/src/JSTransformer/__tests__/Transformer-test.js index a6e2a664..30898622 100644 --- a/packages/metro/src/JSTransformer/__tests__/Transformer-test.js +++ b/packages/metro/src/JSTransformer/__tests__/Transformer-test.js @@ -15,6 +15,7 @@ jest .mock('jest-worker', () => ({__esModule: true, default: jest.fn()})); const Transformer = require('../'); +const defaults = require('../../defaults'); const {Readable} = require('stream'); @@ -27,6 +28,7 @@ describe('Transformer', function() { const opts = { asyncRequireModulePath: 'asyncRequire', maxWorkers: 4, + minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH, reporters: {}, transformModulePath, dynamicDepsInPackages: 'reject', diff --git a/packages/metro/src/JSTransformer/index.js b/packages/metro/src/JSTransformer/index.js index f5f4244f..09e27dcf 100644 --- a/packages/metro/src/JSTransformer/index.js +++ b/packages/metro/src/JSTransformer/index.js @@ -39,9 +39,11 @@ module.exports = class Transformer { _transformModulePath: string; _asyncRequireModulePath: string; _dynamicDepsInPackages: DynamicRequiresBehavior; + _minifierPath: string; constructor(options: {| +maxWorkers: number, + +minifierPath: string, +reporters: Reporters, +transformModulePath: string, +asyncRequireModulePath: string, @@ -51,6 +53,7 @@ module.exports = class Transformer { this._transformModulePath = options.transformModulePath; this._asyncRequireModulePath = options.asyncRequireModulePath; this._dynamicDepsInPackages = options.dynamicDepsInPackages; + this._minifierPath = options.minifierPath; const {workerPath = require.resolve('./worker')} = options; if (options.maxWorkers > 1) { @@ -86,7 +89,12 @@ module.exports = class Transformer { code: string, sourceMap: BabelSourceMap, ): Promise { - return await this._worker.minify(filename, code, sourceMap); + return await this._worker.minify( + filename, + code, + sourceMap, + this._minifierPath, + ); } async transform( diff --git a/packages/metro/src/JSTransformer/worker/index.js b/packages/metro/src/JSTransformer/worker/index.js index 1e130eaa..8ff2bb3a 100644 --- a/packages/metro/src/JSTransformer/worker/index.js +++ b/packages/metro/src/JSTransformer/worker/index.js @@ -15,8 +15,8 @@ const JsFileWrapping = require('../../ModuleGraph/worker/JsFileWrapping'); const assetTransformer = require('../../assetTransformer'); const collectDependencies = require('../../ModuleGraph/worker/collectDependencies'); const constantFolding = require('./constant-folding'); +const getMinifier = require('../../lib/getMinifier'); const inline = require('./inline'); -const minify = require('metro-minify-uglify'); const optimizeDependencies = require('../../ModuleGraph/worker/optimizeDependencies'); const path = require('path'); @@ -272,7 +272,9 @@ function minifyCode( filename: string, code: string, sourceMap: BabelSourceMap, + minifierPath: string, ): ResultWithMap | Promise { + const minify = getMinifier(minifierPath); try { return minify.withSourceMap(code, sourceMap, filename); } catch (error) { diff --git a/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js b/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js index bfbff605..bf3409de 100644 --- a/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js +++ b/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js @@ -11,6 +11,7 @@ 'use strict'; +const defaults = require('../../../defaults'); const invariant = require('fbjs/lib/invariant'); const nullthrows = require('fbjs/lib/nullthrows'); const optimizeModule = require('../optimize-module'); @@ -22,15 +23,13 @@ const {SourceMapConsumer} = require('source-map'); const {objectContaining} = jasmine; -const METRO_MINIFIER = 'metro-minify-uglify'; - describe('optimizing JS modules', () => { const filename = 'arbitrary/file.js'; const sourceExts = new Set(['js', 'json']); const asyncRequireModulePath = 'asyncRequire'; const optimizationOptions = { dev: false, - minifierPath: METRO_MINIFIER, + minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH, platform: 'android', postMinifyProcess: x => x, }; @@ -147,7 +146,7 @@ describe('optimizing JS modules', () => { optimizeModule(new Buffer(JSON.stringify(data), 'utf8'), { dev: true, platform: '', - minifierPath: METRO_MINIFIER, + minifierPath: defaults.DEFAULT_METRO_MINIFIER_PATH, postMinifyProcess: ({code, map}) => ({code, map}), }), ).toEqual(data); diff --git a/packages/metro/src/defaults.js b/packages/metro/src/defaults.js index b5e06e97..d0fa3cc1 100644 --- a/packages/metro/src/defaults.js +++ b/packages/metro/src/defaults.js @@ -50,3 +50,5 @@ exports.platforms = ['ios', 'android', 'windows', 'web']; exports.providesModuleNodeModules = ['react-native', 'react-native-windows']; exports.transformModulePath = require.resolve('./defaultTransform.js'); + +exports.DEFAULT_METRO_MINIFIER_PATH = 'metro-minify-uglify';