From 4460ed5307e60f3dbb7f204ae8f741a3bc373939 Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Mon, 12 Jun 2017 02:18:54 -0700 Subject: [PATCH] Unify invocations to `uglify.minify` Summary: Puts all invocations of `uglify.minify` into one place to facilitate the upgrade to Uglify 3 Reviewed By: cpojer Differential Revision: D5218415 fbshipit-source-id: 8085255205f80bfda06e0092c9e268a85947763b --- .../JSTransformer/worker/JsMinification.js | 20 --------- .../worker/__tests__/minify-test.js | 18 ++++++-- .../src/JSTransformer/worker/index.js | 2 +- .../src/JSTransformer/worker/minify.js | 43 ++++++++++++++----- .../src/ModuleGraph/worker/optimize-module.js | 4 +- 5 files changed, 50 insertions(+), 37 deletions(-) delete mode 100644 packages/metro-bundler/src/JSTransformer/worker/JsMinification.js diff --git a/packages/metro-bundler/src/JSTransformer/worker/JsMinification.js b/packages/metro-bundler/src/JSTransformer/worker/JsMinification.js deleted file mode 100644 index b9f6d702..00000000 --- a/packages/metro-bundler/src/JSTransformer/worker/JsMinification.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Copyright (c) 2016-present, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - * @format - */ - -'use strict'; - -const UGLIFY_JS_OUTPUT_OPTIONS = { - ascii_only: true, - screw_ie8: true, -}; - -module.exports = {UGLIFY_JS_OUTPUT_OPTIONS}; diff --git a/packages/metro-bundler/src/JSTransformer/worker/__tests__/minify-test.js b/packages/metro-bundler/src/JSTransformer/worker/__tests__/minify-test.js index 6a5ab01f..927fe39f 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/__tests__/minify-test.js +++ b/packages/metro-bundler/src/JSTransformer/worker/__tests__/minify-test.js @@ -36,7 +36,7 @@ describe('Minification:', () => { }); it('passes file name, code, and source map to `uglify`', () => { - minify(filename, code, map); + minify.withSourceMap(code, map, filename); expect(uglify.minify).toBeCalledWith( code, objectContaining({ @@ -47,15 +47,27 @@ describe('Minification:', () => { ); }); + it('passes code to `uglify` when minifying without source map', () => { + minify.noSourceMap(code); + expect(uglify.minify).toBeCalledWith( + code, + objectContaining({ + fromString: true, + outSourceMap: true, + }), + ); + }); + it('returns the code provided by uglify', () => { uglify.minify.mockReturnValue({code, map: '{}'}); - const result = minify('', '', {}); + const result = minify.withSourceMap('', {}, ''); expect(result.code).toBe(code); + expect(minify.noSourceMap('')).toBe(code); }); it('parses the source map object provided by uglify and sets the sources property', () => { uglify.minify.mockReturnValue({map: JSON.stringify(map), code: ''}); - const result = minify(filename, '', {}); + const result = minify.withSourceMap('', {}, filename); expect(result.map).toEqual({...map, sources: [filename]}); }); }); diff --git a/packages/metro-bundler/src/JSTransformer/worker/index.js b/packages/metro-bundler/src/JSTransformer/worker/index.js index c9ac4310..7ef9da0e 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/index.js +++ b/packages/metro-bundler/src/JSTransformer/worker/index.js @@ -185,7 +185,7 @@ exports.minify = ( ) => { var result; try { - result = minify(filename, code, sourceMap); + result = minify.withSourceMap(code, sourceMap, filename); } catch (error) { callback(error); } diff --git a/packages/metro-bundler/src/JSTransformer/worker/minify.js b/packages/metro-bundler/src/JSTransformer/worker/minify.js index 67e3b1c5..c4d40f7f 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/minify.js +++ b/packages/metro-bundler/src/JSTransformer/worker/minify.js @@ -14,22 +14,43 @@ const uglify = require('uglify-js'); -const {UGLIFY_JS_OUTPUT_OPTIONS} = require('./JsMinification'); - import type {MappingsMap} from '../../lib/SourceMap'; +type ResultWithMap = { + code: string, + map: MappingsMap, +}; -function minify(filename: string, inputCode: string, sourceMap: ?MappingsMap) { - const result = uglify.minify(inputCode, { +const UGLIFY_JS_OUTPUT_OPTIONS = { + ascii_only: true, + screw_ie8: true, +}; + +function noSourceMap(code: string): string { + return minify(code).code; +} + +function withSourceMap( + code: string, + sourceMap: ?MappingsMap, + filename: string, +): ResultWithMap { + const result = minify(code, sourceMap); + + const map: MappingsMap = JSON.parse(result.map); + map.sources = [filename]; + return {code: result.code, map}; +} + +function minify(inputCode: string, inputMap: ?MappingsMap) { + return uglify.minify(inputCode, { fromString: true, - inSourceMap: sourceMap, + inSourceMap: inputMap, outSourceMap: true, output: UGLIFY_JS_OUTPUT_OPTIONS, }); - - const code = result.code; - const map = JSON.parse(result.map); - map.sources = [filename]; - return {code, map}; } -module.exports = minify; +module.exports = { + noSourceMap, + withSourceMap, +}; diff --git a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js index e77ad12d..7c31b3aa 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js @@ -75,10 +75,10 @@ function optimize(transformed, file, originalCode, options) { const inputMap = transformed.map; const gen = generate(optimized.ast, file, originalCode); - const min = minify( - file, + const min = minify.withSourceMap( gen.code, inputMap && mergeSourceMaps(file, inputMap, gen.map), + file, ); return {code: min.code, map: min.map, dependencies}; }