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 3f2a18ba..bfbff605 100644 --- a/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js +++ b/packages/metro/src/ModuleGraph/worker/__tests__/optimize-module-test.js @@ -22,12 +22,15 @@ 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, platform: 'android', postMinifyProcess: x => x, }; @@ -144,6 +147,7 @@ describe('optimizing JS modules', () => { optimizeModule(new Buffer(JSON.stringify(data), 'utf8'), { dev: true, platform: '', + minifierPath: METRO_MINIFIER, postMinifyProcess: ({code, map}) => ({code, map}), }), ).toEqual(data); diff --git a/packages/metro/src/ModuleGraph/worker/optimize-module.js b/packages/metro/src/ModuleGraph/worker/optimize-module.js index 00999c07..5be365de 100644 --- a/packages/metro/src/ModuleGraph/worker/optimize-module.js +++ b/packages/metro/src/ModuleGraph/worker/optimize-module.js @@ -13,9 +13,9 @@ const constantFolding = require('../../JSTransformer/worker/constant-folding') .plugin; const generate = require('./generate'); +const getMinifier = require('../../lib/getMinifier'); const inline = require('../../JSTransformer/worker/inline').plugin; const invariant = require('fbjs/lib/invariant'); -const minify = require('metro-minify-uglify'); const optimizeDependencies = require('./optimizeDependencies'); const sourceMap = require('source-map'); @@ -25,10 +25,12 @@ import type {TransformedSourceFile, TransformResult} from '../types.flow'; import type {BabelSourceMap} from '@babel/core'; import type {MetroSourceMap} from 'metro-source-map'; import type {PostMinifyProcess} from '../../Bundler/index.js'; +import type {TransformResult as BabelTransformResult} from '@babel/core'; export type OptimizationOptions = {| dev: boolean, isPolyfill?: boolean, + minifierPath: string, platform: string, postMinifyProcess: PostMinifyProcess, |}; @@ -93,6 +95,7 @@ function optimize( const inputMap = transformed.map; const gen = generate(optimized.ast, file, '', true); + const minify = getMinifier(options.minifierPath); const min = minify.withSourceMap( gen.code, inputMap && gen.map && mergeSourceMaps(file, inputMap, gen.map), @@ -106,7 +109,12 @@ function optimize( }; } -function optimizeCode(code, map, filename, inliningOptions) { +function optimizeCode( + code, + map, + filename, + inliningOptions, +): BabelTransformResult { return transformSync(code, { plugins: [ [constantFolding], diff --git a/packages/metro/src/lib/getMinifier.js b/packages/metro/src/lib/getMinifier.js new file mode 100644 index 00000000..df59ae2e --- /dev/null +++ b/packages/metro/src/lib/getMinifier.js @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2018-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +'use strict'; + +import type {MetroMinifier} from 'metro-minify-uglify'; + +function getMinifier(minifierPath: string): MetroMinifier { + // Note: minifierPath should be an absolute path OR a module name here! + // The options allow relative paths but they HAVE to be normalized at + // any entry point that accepts them... + try { + // $FlowFixMe TODO t0 cannot do require with literal + return require(minifierPath); + } catch (e) { + throw new Error( + 'A problem occurred while trying to fetch the minifier. Path: "' + + minifierPath + + '", error message: ' + + e.message, + ); + } +} + +module.exports = getMinifier;