diff --git a/flow-typed/uglify.js b/flow-typed/uglify.js index 29a3e85a..3ad651e0 100644 --- a/flow-typed/uglify.js +++ b/flow-typed/uglify.js @@ -9,6 +9,60 @@ 'use strict'; +type _Input = + | string // code or file name + | Array // 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, + }, + reserved?: Array, + 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 = { file?: string, mappings: string, @@ -19,53 +73,24 @@ type _SourceMap = { version: number, }; -type _Result = { - code: string, - map: MapT, +type _SourceMapOptions = true | { + filename?: string, + content?: ?string | _SourceMap, + includeSources?: boolean, + root?: string, + url?: string, }; -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 _Error = {|error: Error|}; +type _Result = {|code: string, warnings?: Array|}; -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 module 'uglify-es' { declare function minify( - fileOrFilesOrCode: _Input, - options?: _Options & {outSourceMap?: ?false | ''}, - ): _Result; + code: _Input, + options?: _Options, + ): _Error | _Result; declare function minify( - fileOrFilesOrCode: _Input, - options?: _Options & {outSourceMap: true | string}, - ): _Result; + code: _Input, + options: {..._Options, sourceMap: _SourceMapOptions}, + ): _Error | {|..._Result, map: string|}; } diff --git a/packages/metro-bundler/package.json b/packages/metro-bundler/package.json index 999d211d..f6a7fddc 100644 --- a/packages/metro-bundler/package.json +++ b/packages/metro-bundler/package.json @@ -44,7 +44,7 @@ "source-map": "^0.5.6", "temp": "0.8.3", "throat": "^4.1.0", - "uglify-js": "2.7.5", + "uglify-es": "^3.1.0", "write-file-atomic": "^1.2.0", "xpipe": "^1.0.5" } diff --git a/packages/metro-bundler/src/Bundler/__tests__/Bundler-test.js b/packages/metro-bundler/src/Bundler/__tests__/Bundler-test.js index 23b831e5..f3009c35 100644 --- a/packages/metro-bundler/src/Bundler/__tests__/Bundler-test.js +++ b/packages/metro-bundler/src/Bundler/__tests__/Bundler-test.js @@ -15,7 +15,7 @@ jest .setMock('worker-farm', () => () => undefined) .setMock('../../worker-farm', () => () => undefined) - .setMock('uglify-js') + .setMock('../../JSTransformer/worker/minify') .mock('image-size') .mock('fs') .mock('os') 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 927fe39f..37a68aae 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/__tests__/minify-test.js +++ b/packages/metro-bundler/src/JSTransformer/worker/__tests__/minify-test.js @@ -10,7 +10,7 @@ */ 'use strict'; -jest.mock('uglify-js', () => ({ +jest.mock('uglify-es', () => ({ minify: jest.fn(code => { return { code: code.replace(/(^|\W)\s+/g, '$1'), @@ -29,7 +29,7 @@ describe('Minification:', () => { let uglify; beforeEach(() => { - uglify = require('uglify-js'); + uglify = require('uglify-es'); uglify.minify.mockClear(); uglify.minify.mockReturnValue({code: '', map: '{}'}); map = {version: 3, sources: ['?'], mappings: ''}; @@ -40,9 +40,10 @@ describe('Minification:', () => { expect(uglify.minify).toBeCalledWith( code, objectContaining({ - fromString: true, - inSourceMap: map, - outSourceMap: true, + sourceMap: { + content: map, + includeSources: true, + }, }), ); }); @@ -52,8 +53,10 @@ describe('Minification:', () => { expect(uglify.minify).toBeCalledWith( code, objectContaining({ - fromString: true, - outSourceMap: true, + sourceMap: { + content: undefined, + includeSources: true, + }, }), ); }); diff --git a/packages/metro-bundler/src/JSTransformer/worker/minify.js b/packages/metro-bundler/src/JSTransformer/worker/minify.js index c4d40f7f..b1270584 100644 --- a/packages/metro-bundler/src/JSTransformer/worker/minify.js +++ b/packages/metro-bundler/src/JSTransformer/worker/minify.js @@ -12,7 +12,7 @@ 'use strict'; -const uglify = require('uglify-js'); +const uglify = require('uglify-es'); import type {MappingsMap} from '../../lib/SourceMap'; type ResultWithMap = { @@ -20,11 +20,6 @@ type ResultWithMap = { map: MappingsMap, }; -const UGLIFY_JS_OUTPUT_OPTIONS = { - ascii_only: true, - screw_ie8: true, -}; - function noSourceMap(code: string): string { return minify(code).code; } @@ -42,12 +37,28 @@ function withSourceMap( } function minify(inputCode: string, inputMap: ?MappingsMap) { - return uglify.minify(inputCode, { - fromString: true, - inSourceMap: inputMap, - outSourceMap: true, - output: UGLIFY_JS_OUTPUT_OPTIONS, + const result = uglify.minify(inputCode, { + mangle: {toplevel: true}, + output: { + ascii_only: true, + 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 = { diff --git a/packages/metro-bundler/src/Server/__tests__/Server-test.js b/packages/metro-bundler/src/Server/__tests__/Server-test.js index 17ced48b..76248ee7 100644 --- a/packages/metro-bundler/src/Server/__tests__/Server-test.js +++ b/packages/metro-bundler/src/Server/__tests__/Server-test.js @@ -13,7 +13,7 @@ jest.mock('../../worker-farm', () => () => () => {}) .mock('worker-farm', () => () => () => {}) - .mock('uglify-js') + .mock('../../JSTransformer/worker/minify') .mock('crypto') .mock( '../symbolicate',