From 4c8fd62a8723956bfe8d5ff8937edff89a0d733d Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Thu, 20 Jan 2022 05:26:15 -0800 Subject: [PATCH] Update terser to v5 in `metro-minify-terser` (#606) Summary: **Summary** Update terser to the latest version that supports more modern js syntax like optional chaining. The main breaking change in v5 is that minify is now async. To support this I also needed to add async minifier support in metro-transform-worker. **Test plan** - Run tests - Test building my app bundle with minifier terser and untransformed optional chaining Pull Request resolved: https://github.com/facebook/metro/pull/606 Reviewed By: feedthejim Differential Revision: D24883529 Pulled By: rh389 fbshipit-source-id: c9ed893535337ecc1ce16e433e3d689c2037be0a --- packages/metro-minify-terser/package.json | 2 +- .../src/__tests__/minify-test.js | 22 ++++++++--------- packages/metro-minify-terser/src/minifier.js | 20 +++++++--------- yarn.lock | 24 ++++++++++++------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/packages/metro-minify-terser/package.json b/packages/metro-minify-terser/package.json index 170a9e3ee8..e903b9f921 100644 --- a/packages/metro-minify-terser/package.json +++ b/packages/metro-minify-terser/package.json @@ -13,6 +13,6 @@ }, "license": "MIT", "dependencies": { - "terser": "^4.6.3" + "terser": "^5.10.0" } } diff --git a/packages/metro-minify-terser/src/__tests__/minify-test.js b/packages/metro-minify-terser/src/__tests__/minify-test.js index 7d418f6648..accc31d105 100644 --- a/packages/metro-minify-terser/src/__tests__/minify-test.js +++ b/packages/metro-minify-terser/src/__tests__/minify-test.js @@ -17,10 +17,10 @@ const minify = require('..'); jest.mock('terser', () => ({ minify: jest.fn(code => { - return { + return Promise.resolve({ code: code.replace(/(^|\W)\s+/g, '$1'), map: {}, - }; + }); }), })); const {objectContaining} = expect; @@ -57,12 +57,12 @@ describe('Minification:', () => { /* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an * error found when Flow v0.99 was deployed. To see the error, delete this * comment and run Flow. */ - terser.minify.mockReturnValue({code: '', map: '{}'}); + terser.minify.mockResolvedValue({code: '', map: '{}'}); map = getFakeMap(); }); - it('passes file name, code, and source map to `terser`', () => { - minify({ + it('passes file name, code, and source map to `terser`', async () => { + await minify({ ...baseOptions, code, map, @@ -80,21 +80,21 @@ describe('Minification:', () => { ); }); - it('returns the code provided by terser', () => { + it('returns the code provided by terser', async () => { /* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an * error found when Flow v0.99 was deployed. To see the error, delete this * comment and run Flow. */ - terser.minify.mockReturnValue({code, map: '{}'}); - const result = minify(baseOptions); + terser.minify.mockResolvedValue({code, map: '{}'}); + const result = await minify(baseOptions); expect(result.code).toBe(code); }); - it('parses the source map object provided by terser and sets the sources property', () => { + it('parses the source map object provided by terser and sets the sources property', async () => { /* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an * error found when Flow v0.99 was deployed. To see the error, delete this * comment and run Flow. */ - terser.minify.mockReturnValue({map: JSON.stringify(map), code: ''}); - const result = minify({...baseOptions, filename}); + terser.minify.mockResolvedValue({map: JSON.stringify(map), code: ''}); + const result = await minify({...baseOptions, filename}); expect(result.map).toEqual({...map, sources: [filename]}); }); }); diff --git a/packages/metro-minify-terser/src/minifier.js b/packages/metro-minify-terser/src/minifier.js index 8b9c27fee6..2afa94bc1a 100644 --- a/packages/metro-minify-terser/src/minifier.js +++ b/packages/metro-minify-terser/src/minifier.js @@ -15,8 +15,8 @@ import type {MinifierOptions, MinifierResult} from 'metro-transform-worker'; const terser = require('terser'); -function minifier(options: MinifierOptions): MinifierResult { - const result = minify(options); +async function minifier(options: MinifierOptions): Promise { + const result = await minify(options); if (!options.map || result.map == null) { return {code: result.code}; @@ -27,10 +27,12 @@ function minifier(options: MinifierOptions): MinifierResult { return {code: result.code, map: {...map, sources: [options.filename]}}; } -function minify({code, map, reserved, config}: MinifierOptions): { - code: string, - map: ?string, -} { +async function minify({ + code, + map, + reserved, + config, +}: MinifierOptions): Promise<{code: string, map: ?string}> { const options = { ...config, mangle: @@ -53,11 +55,7 @@ function minify({code, map, reserved, config}: MinifierOptions): { /* $FlowFixMe(>=0.111.0 site=react_native_fb) This comment suppresses an * error found when Flow v0.111 was deployed. To see the error, delete this * comment and run Flow. */ - const result = terser.minify(code, options); - - if (result.error) { - throw result.error; - } + const result = await terser.minify(code, options); return { code: result.code, diff --git a/yarn.lock b/yarn.lock index 40d99fc354..bc6a215dc8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6909,7 +6909,7 @@ source-map-resolve@^0.5.0: source-map-url "^0.4.0" urix "^0.1.0" -source-map-support@^0.5.16, source-map-support@^0.5.6, source-map-support@~0.5.12: +source-map-support@^0.5.16, source-map-support@^0.5.6: version "0.5.16" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.16.tgz#0ae069e7fe3ba7538c64c98515e35339eac5a042" integrity sha512-efyLRJDr68D9hBBNIPWFjhpFzURh+KJykQwvMyW5UiZzYwoF6l4YMMDIJJEyFWxWCqfyxLzz6tSfUFR+kXXsVQ== @@ -6917,6 +6917,14 @@ source-map-support@^0.5.16, source-map-support@^0.5.6, source-map-support@~0.5.1 buffer-from "^1.0.0" source-map "^0.6.0" +source-map-support@~0.5.20: + version "0.5.21" + resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f" + integrity sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w== + dependencies: + buffer-from "^1.0.0" + source-map "^0.6.0" + source-map-url@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/source-map-url/-/source-map-url-0.4.0.tgz#3e935d7ddd73631b97659956d55128e87b5084a3" @@ -6932,7 +6940,7 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== -source-map@^0.7.3: +source-map@^0.7.3, source-map@~0.7.2: version "0.7.3" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383" integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ== @@ -7346,14 +7354,14 @@ terminal-link@^2.0.0: ansi-escapes "^4.2.1" supports-hyperlinks "^2.0.0" -terser@^4.6.3: - version "4.6.7" - resolved "https://registry.yarnpkg.com/terser/-/terser-4.6.7.tgz#478d7f9394ec1907f0e488c5f6a6a9a2bad55e72" - integrity sha512-fmr7M1f7DBly5cX2+rFDvmGBAaaZyPrHYK4mMdHEDAdNTqXSZgSOfqsfGq2HqPGT/1V0foZZuCZFx8CHKgAk3g== +terser@^5.10.0: + version "5.10.0" + resolved "https://registry.yarnpkg.com/terser/-/terser-5.10.0.tgz#b86390809c0389105eb0a0b62397563096ddafcc" + integrity sha512-AMmF99DMfEDiRJfxfY5jj5wNH/bYO09cniSqhfoyxc8sFoYIgkJy86G04UoZU5VjlpnplVu0K6Tx6E9b5+DlHA== dependencies: commander "^2.20.0" - source-map "~0.6.1" - source-map-support "~0.5.12" + source-map "~0.7.2" + source-map-support "~0.5.20" test-exclude@^6.0.0: version "6.0.0"