diff --git a/packages/metro-bundler/src/JSTransformer/worker/minify.js b/packages/metro-bundler/src/JSTransformer/worker/minify.js index deceb4cb..d6459ab4 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/minify.js +++ b/packages/metro-bundler/src/JSTransformer/worker/minify.js @@ -15,17 +15,19 @@ const uglify = require('uglify-js'); const {UGLIFY_JS_OUTPUT_OPTIONS} = require('./JsMinification'); -function minify(filename: string, code: string, sourceMap: ?string) { - const minifyResult = uglify.minify(code, { +import type {MappingsMap} from '../../lib/SourceMap'; + +function minify(filename: string, inputCode: string, sourceMap: ?MappingsMap) { + let {code, map} = uglify.minify(inputCode, { fromString: true, inSourceMap: sourceMap, outSourceMap: true, output: UGLIFY_JS_OUTPUT_OPTIONS, }); - minifyResult.map = JSON.parse(minifyResult.map); - minifyResult.map.sources = [filename]; - return minifyResult; + map = JSON.parse(map); + map.sources = [filename]; + return {code, map}; } module.exports = minify; diff --git a/packages/metro-bundler/src/JSTransformer/worker/worker.js b/packages/metro-bundler/src/JSTransformer/worker/worker.js index 011489a7..c674a1be 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/worker.js +++ b/packages/metro-bundler/src/JSTransformer/worker/worker.js @@ -166,7 +166,7 @@ exports.transformAndExtractDependencies = ( exports.minify = ( filename: string, code: string, - sourceMap: string, + sourceMap: MappingsMap, callback: (error: ?Error, result: mixed) => mixed, ) => { var result; diff --git a/packages/metro-bundler/src/ModuleGraph/types.flow.js b/packages/metro-bundler/src/ModuleGraph/types.flow.js index 1352a927..8809cfbd 100644 --- a/packages/metro-bundler/src/ModuleGraph/types.flow.js +++ b/packages/metro-bundler/src/ModuleGraph/types.flow.js @@ -110,7 +110,7 @@ export type TransformResult = {| code: string, dependencies: Array, dependencyMapName?: string, - map: ?Object, + map: ?MappingsMap, |}; export type TransformResults = {[string]: TransformResult}; diff --git a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js index 89885163..4324d02a 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js @@ -19,6 +19,7 @@ const minify = require('../../JSTransformer/worker/minify'); const sourceMap = require('source-map'); import type {TransformedSourceFile, TransformResult} from '../types.flow'; +import type {MappingsMap, SourceMap} from '../../lib/SourceMap'; export type OptimizationOptions = {| dev: boolean, @@ -71,7 +72,7 @@ function optimize(transformed, file, originalCode, options): TransformResult { gen.code, inputMap && mergeSourceMaps(file, inputMap, gen.map), ); - return {code: min.code, map: inputMap && min.map, dependencies}; + return {code: min.code, map: min.map, dependencies}; } function optimizeCode(code, map, filename, inliningOptions) { @@ -86,7 +87,11 @@ function optimizeCode(code, map, filename, inliningOptions) { }); } -function mergeSourceMaps(file, originalMap, secondMap) { +function mergeSourceMaps( + file: string, + originalMap: SourceMap, + secondMap: SourceMap, +): MappingsMap { const merged = new sourceMap.SourceMapGenerator(); const inputMap = new sourceMap.SourceMapConsumer(originalMap); new sourceMap.SourceMapConsumer(secondMap)