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
This commit is contained in:
David Aurelio 2017-06-12 02:18:54 -07:00 committed by Facebook Github Bot
parent bc654954a7
commit 4460ed5307
5 changed files with 50 additions and 37 deletions

View File

@ -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};

View File

@ -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]});
});
});

View File

@ -185,7 +185,7 @@ exports.minify = (
) => {
var result;
try {
result = minify(filename, code, sourceMap);
result = minify.withSourceMap(code, sourceMap, filename);
} catch (error) {
callback(error);
}

View File

@ -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,
};

View File

@ -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};
}