From f1a220b3cfae2ddb5ab458a90fd754bc5b4b8bcb Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Wed, 10 May 2017 05:15:18 -0700 Subject: [PATCH] Fixes issues with uglify.js usage Summary: Adds flow type defs for uglify, and fixes the two issues found Reviewed By: jeanlauliac Differential Revision: D5029190 fbshipit-source-id: eb5947b051844938da241e002b727edc1384e3a6 --- flow/uglify.js | 71 +++++++++++++++++++ packager/src/JSTransformer/worker/minify.js | 12 ++-- packager/src/JSTransformer/worker/worker.js | 2 +- packager/src/ModuleGraph/types.flow.js | 2 +- .../src/ModuleGraph/worker/optimize-module.js | 9 ++- 5 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 flow/uglify.js diff --git a/flow/uglify.js b/flow/uglify.js new file mode 100644 index 000000000..29a3e85ac --- /dev/null +++ b/flow/uglify.js @@ -0,0 +1,71 @@ +/** + * Copyright (c) 2013-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. + */ + +'use strict'; + +type _SourceMap = { + file?: string, + mappings: string, + names: Array, + sourceRoot?: string, + sources: Array, + sourcesContent?: Array, + version: number, +}; + +type _Result = { + code: string, + map: MapT, +}; + +type _Options = { + compress?: false | {||}, + fromString?: boolean, + inSourceMap?: string | ?_SourceMap, + mangle?: boolean | {| + except?: Array, + toplevel?: boolean, + eval?: boolean, + keep_fnames?: boolean, + |}, + mangleProperties?: boolean | {| + regex?: RegExp, + ignore_quoted?: boolean, + debug?: false | string, + |}, + outFileName?: string, + output?: {| + ascii_only?: boolean, + screw_ie8?: boolean, + |}, + parse?: {| + strict?: boolean, + bare_returns?: boolean, + filename?: string, + |}, + sourceMapUrl?: string, + sourceRoot?: string, + warnings?: boolean, +}; + +type _Input = + | string // code or file name + | Array // array of file names + | {[filename: string]: string}; // file names and corresponding code + +declare module 'uglify-js' { + declare function minify( + fileOrFilesOrCode: _Input, + options?: _Options & {outSourceMap?: ?false | ''}, + ): _Result; + declare function minify( + fileOrFilesOrCode: _Input, + options?: _Options & {outSourceMap: true | string}, + ): _Result; +} diff --git a/packager/src/JSTransformer/worker/minify.js b/packager/src/JSTransformer/worker/minify.js index deceb4cbe..d6459ab46 100644 --- a/packager/src/JSTransformer/worker/minify.js +++ b/packager/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/packager/src/JSTransformer/worker/worker.js b/packager/src/JSTransformer/worker/worker.js index 011489a77..c674a1bed 100644 --- a/packager/src/JSTransformer/worker/worker.js +++ b/packager/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/packager/src/ModuleGraph/types.flow.js b/packager/src/ModuleGraph/types.flow.js index 1352a927a..8809cfbda 100644 --- a/packager/src/ModuleGraph/types.flow.js +++ b/packager/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/packager/src/ModuleGraph/worker/optimize-module.js b/packager/src/ModuleGraph/worker/optimize-module.js index 89885163b..4324d02a0 100644 --- a/packager/src/ModuleGraph/worker/optimize-module.js +++ b/packager/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)