Upgrade uglify to v3 + es support

Summary: Upgrades uglify to version 3 with (experimental) ES6 support turned on.

Reviewed By: jeanlauliac

Differential Revision: D5842410

fbshipit-source-id: 1c8ccea15785bc5bb1c68d7a83b75881432d0ce2
This commit is contained in:
David Aurelio 2017-09-19 12:15:31 -07:00 committed by Facebook Github Bot
parent 025e1841d9
commit ad927b93dc
6 changed files with 104 additions and 65 deletions

113
flow-typed/uglify.js vendored
View File

@ -9,6 +9,60 @@
'use strict'; 'use strict';
type _Input =
| string // code or file name
| Array<string> // array of file names
| {[filename: string]: string}; // file names and corresponding code
type _Options = {
// https://github.com/mishoo/UglifyJS2/tree/harmony#compress-options
compress?: false | Object,
ie8?: boolean,
mangle?: boolean | {
eval?: boolean,
keep_fnames?: boolean,
properties?: boolean | {
builtins?: boolean,
debug?: boolean,
keep_quoted?: boolean,
regex?: RegExp,
reserved?: Array<string>,
},
reserved?: Array<string>,
safari10?: boolean,
toplevel?: boolean,
},
output?: {
ascii_only?: boolean,
beautify?: boolean,
bracketize?: boolean,
comments?: boolean | 'all' | 'some' | RegExp | Function,
ecma?: 5 | 6,
indent_level?: number,
indent_start?: number,
inline_script?: number,
keep_quoted_props?: boolean,
max_line_len?: false | number,
preamble?: string,
preserve_line?: boolean,
quote_keys?: boolean,
quote_style?: 0 | 1 | 2 | 3,
semicolons?: boolean,
shebang?: boolean,
width?: number,
wrap_iife?: boolean,
},
parse?: {
bare_returns: boolean,
html5_comments: boolean,
shebang: boolean,
},
sourceMap?: false,
toplevel?: boolean,
warnings?: boolean | 'verbose',
};
type _SourceMap = { type _SourceMap = {
file?: string, file?: string,
mappings: string, mappings: string,
@ -19,53 +73,24 @@ type _SourceMap = {
version: number, version: number,
}; };
type _Result<MapT> = { type _SourceMapOptions = true | {
code: string, filename?: string,
map: MapT, content?: ?string | _SourceMap,
includeSources?: boolean,
root?: string,
url?: string,
}; };
type _Options = { type _Error = {|error: Error|};
compress?: false | {||}, type _Result = {|code: string, warnings?: Array<string>|};
fromString?: boolean,
inSourceMap?: string | ?_SourceMap,
mangle?: boolean | {|
except?: Array<string>,
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 = declare module 'uglify-es' {
| string // code or file name
| Array<string> // array of file names
| {[filename: string]: string}; // file names and corresponding code
declare module 'uglify-js' {
declare function minify( declare function minify(
fileOrFilesOrCode: _Input, code: _Input,
options?: _Options & {outSourceMap?: ?false | ''}, options?: _Options,
): _Result<void>; ): _Error | _Result;
declare function minify( declare function minify(
fileOrFilesOrCode: _Input, code: _Input,
options?: _Options & {outSourceMap: true | string}, options: {..._Options, sourceMap: _SourceMapOptions},
): _Result<string>; ): _Error | {|..._Result, map: string|};
} }

View File

@ -44,7 +44,7 @@
"source-map": "^0.5.6", "source-map": "^0.5.6",
"temp": "0.8.3", "temp": "0.8.3",
"throat": "^4.1.0", "throat": "^4.1.0",
"uglify-js": "2.7.5", "uglify-es": "^3.1.0",
"write-file-atomic": "^1.2.0", "write-file-atomic": "^1.2.0",
"xpipe": "^1.0.5" "xpipe": "^1.0.5"
} }

View File

@ -15,7 +15,7 @@
jest jest
.setMock('worker-farm', () => () => undefined) .setMock('worker-farm', () => () => undefined)
.setMock('../../worker-farm', () => () => undefined) .setMock('../../worker-farm', () => () => undefined)
.setMock('uglify-js') .setMock('../../JSTransformer/worker/minify')
.mock('image-size') .mock('image-size')
.mock('fs') .mock('fs')
.mock('os') .mock('os')

View File

@ -10,7 +10,7 @@
*/ */
'use strict'; 'use strict';
jest.mock('uglify-js', () => ({ jest.mock('uglify-es', () => ({
minify: jest.fn(code => { minify: jest.fn(code => {
return { return {
code: code.replace(/(^|\W)\s+/g, '$1'), code: code.replace(/(^|\W)\s+/g, '$1'),
@ -29,7 +29,7 @@ describe('Minification:', () => {
let uglify; let uglify;
beforeEach(() => { beforeEach(() => {
uglify = require('uglify-js'); uglify = require('uglify-es');
uglify.minify.mockClear(); uglify.minify.mockClear();
uglify.minify.mockReturnValue({code: '', map: '{}'}); uglify.minify.mockReturnValue({code: '', map: '{}'});
map = {version: 3, sources: ['?'], mappings: ''}; map = {version: 3, sources: ['?'], mappings: ''};
@ -40,9 +40,10 @@ describe('Minification:', () => {
expect(uglify.minify).toBeCalledWith( expect(uglify.minify).toBeCalledWith(
code, code,
objectContaining({ objectContaining({
fromString: true, sourceMap: {
inSourceMap: map, content: map,
outSourceMap: true, includeSources: true,
},
}), }),
); );
}); });
@ -52,8 +53,10 @@ describe('Minification:', () => {
expect(uglify.minify).toBeCalledWith( expect(uglify.minify).toBeCalledWith(
code, code,
objectContaining({ objectContaining({
fromString: true, sourceMap: {
outSourceMap: true, content: undefined,
includeSources: true,
},
}), }),
); );
}); });

View File

@ -12,7 +12,7 @@
'use strict'; 'use strict';
const uglify = require('uglify-js'); const uglify = require('uglify-es');
import type {MappingsMap} from '../../lib/SourceMap'; import type {MappingsMap} from '../../lib/SourceMap';
type ResultWithMap = { type ResultWithMap = {
@ -20,11 +20,6 @@ type ResultWithMap = {
map: MappingsMap, map: MappingsMap,
}; };
const UGLIFY_JS_OUTPUT_OPTIONS = {
ascii_only: true,
screw_ie8: true,
};
function noSourceMap(code: string): string { function noSourceMap(code: string): string {
return minify(code).code; return minify(code).code;
} }
@ -42,12 +37,28 @@ function withSourceMap(
} }
function minify(inputCode: string, inputMap: ?MappingsMap) { function minify(inputCode: string, inputMap: ?MappingsMap) {
return uglify.minify(inputCode, { const result = uglify.minify(inputCode, {
fromString: true, mangle: {toplevel: true},
inSourceMap: inputMap, output: {
outSourceMap: true, ascii_only: true,
output: UGLIFY_JS_OUTPUT_OPTIONS, quote_style: 3,
wrap_iife: true,
},
sourceMap: {
content: inputMap,
includeSources: true,
},
toplevel: true,
}); });
if (result.error) {
throw result.error;
}
return {
code: result.code,
map: result.map,
};
} }
module.exports = { module.exports = {

View File

@ -13,7 +13,7 @@
jest.mock('../../worker-farm', () => () => () => {}) jest.mock('../../worker-farm', () => () => () => {})
.mock('worker-farm', () => () => () => {}) .mock('worker-farm', () => () => () => {})
.mock('uglify-js') .mock('../../JSTransformer/worker/minify')
.mock('crypto') .mock('crypto')
.mock( .mock(
'../symbolicate', '../symbolicate',