Skip to content

Commit

Permalink
Update terser to v5 in metro-minify-terser (facebook#606)
Browse files Browse the repository at this point in the history
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: facebook#606

Reviewed By: feedthejim

Differential Revision: D24883529

Pulled By: rh389

fbshipit-source-id: c9ed893535337ecc1ce16e433e3d689c2037be0a
  • Loading branch information
janicduplessis authored and nevilm-lt committed Mar 14, 2022
1 parent 62fbf39 commit 50aaa1a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/metro-minify-terser/package.json
Expand Up @@ -13,6 +13,6 @@
},
"license": "MIT",
"dependencies": {
"terser": "^4.6.3"
"terser": "^5.10.0"
}
}
22 changes: 11 additions & 11 deletions packages/metro-minify-terser/src/__tests__/minify-test.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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]});
});
});
20 changes: 9 additions & 11 deletions packages/metro-minify-terser/src/minifier.js
Expand Up @@ -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<MinifierResult> {
const result = await minify(options);

if (!options.map || result.map == null) {
return {code: result.code};
Expand All @@ -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:
Expand All @@ -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,
Expand Down
24 changes: 16 additions & 8 deletions yarn.lock
Expand Up @@ -6909,14 +6909,22 @@ 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==
dependencies:
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"
Expand All @@ -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==
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 50aaa1a

Please sign in to comment.