Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update terser to v5, support async minifiers #606

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.3.8"
}
}
22 changes: 11 additions & 11 deletions packages/metro-minify-terser/src/__tests__/minify-test.js
Expand Up @@ -15,10 +15,10 @@ import type {BasicSourceMap} from 'metro-source-map';

jest.mock('terser', () => ({
minify: jest.fn(code => {
return {
return Promise.resolve({
code: code.replace(/(^|\W)\s+/g, '$1'),
map: {},
};
});
}),
}));

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]});
});
});
14 changes: 5 additions & 9 deletions packages/metro-minify-terser/src/minifier.js
Expand Up @@ -15,8 +15,8 @@ const terser = require('terser');
import type {BasicSourceMap} from 'metro-source-map';
import type {MinifierResult, MinifierOptions} from 'metro-transform-worker';

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,12 +27,12 @@ function minifier(options: MinifierOptions): MinifierResult {
return {code: result.code, map: {...map, sources: [options.filename]}};
}

function minify({
async function minify({
code,
map,
reserved,
config,
}: MinifierOptions): {code: string, map: ?string} {
}: MinifierOptions): Promise<{code: string, map: ?string}> {
const options = {
...config,
mangle: {
Expand All @@ -50,11 +50,7 @@ function minify({
/* $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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terser will now throw an error so there is no need for additional handling anymore.

throw result.error;
}
const result = await terser.minify(code, options);

return {
code: result.code,
Expand Down
6 changes: 4 additions & 2 deletions packages/metro-transform-worker/src/index.js
Expand Up @@ -73,7 +73,9 @@ export type MinifierResult = {
...
};

export type Minifier = MinifierOptions => MinifierResult;
export type Minifier = MinifierOptions =>
| Promise<MinifierResult>
| MinifierResult;

export type Type = 'script' | 'module' | 'asset';

Expand Down Expand Up @@ -170,7 +172,7 @@ const minifyCode = async (
const minify = getMinifier(config.minifierPath);

try {
const minified = minify({
const minified = await minify({
code,
map: sourceMap,
filename,
Expand Down