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

Uglify terser #243

Closed
wants to merge 3 commits 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
5 changes: 5 additions & 0 deletions packages/metro-minify-terser/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/__mocks__/**
**/__tests__/**
build
src.real
yarn.lock
87 changes: 87 additions & 0 deletions packages/metro-minify-terser/__tests__/minify-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @emails oncall+js_foundation
*/
'use strict';

import type {BabelSourceMap} from '@babel/core';

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

const minify = require('..');
const {objectContaining} = jasmine;

function getFakeMap(): BabelSourceMap {
return {
version: 3,
sources: ['?'],
mappings: '',
names: [],
};
}

describe('Minification:', () => {
const filename = '/arbitrary/file.js';
const code = 'arbitrary(code)';
let map: BabelSourceMap;
let terser;

beforeEach(() => {
terser = require('terser');
terser.minify.mockClear();
terser.minify.mockReturnValue({code: '', map: '{}'});
map = getFakeMap();
});

it('passes file name, code, and source map to `terser`', () => {
minify.withSourceMap(code, map, filename);
expect(terser.minify).toBeCalledWith(
code,
objectContaining({
sourceMap: {
content: map,
includeSources: false,
},
}),
);
});

it('passes code to `terser` when minifying without source map', () => {
minify.noSourceMap(code);
expect(terser.minify).toBeCalledWith(
code,
objectContaining({
sourceMap: {
content: undefined,
includeSources: false,
},
}),
);
});

it('returns the code provided by terser', () => {
terser.minify.mockReturnValue({code, map: '{}'});
const result = minify.withSourceMap('', getFakeMap(), '');
expect(result.code).toBe(code);
expect(minify.noSourceMap('')).toBe(code);
});

it('parses the source map object provided by terser and sets the sources property', () => {
terser.minify.mockReturnValue({map: JSON.stringify(map), code: ''});
const result = minify.withSourceMap('', getFakeMap(), filename);
expect(result.map).toEqual({...map, sources: [filename]});
});
});
20 changes: 20 additions & 0 deletions packages/metro-minify-terser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "metro-minify-terser",
"version": "0.45.2",
"description": "🚇 Terser-based minifier alternative for Metro",
"main": "src/index.js",
"repository": {
"type": "git",
"url": "git@github.com:facebook/metro.git"
},
"scripts": {
"prepare-release": "test -d build && rm -rf src.real && mv src src.real && mv build src",
"cleanup-release": "test ! -e build && mv src build && mv src.real src"
},
"license": "MIT",
"dependencies": {
"metro-minify-uglify": "^0.45.2",
Copy link
Contributor

Choose a reason for hiding this comment

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

Version needs to be hard set - e.g. remove the "^" - node CI tests should be good after this. 👍

"terser": "^3.8.2"
},
"devDependencies": {}
}
49 changes: 49 additions & 0 deletions packages/metro-minify-terser/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

import type {
MetroMinifier,
MinifyOptions,
ResultWithMap,
ResultWithoutMap,
} from './types';
import type {BabelSourceMap} from '@babel/core';

const minifier = require('metro-minify-uglify/src/minifier');
const terser = require('terser');

export type {MetroMinifier} from './types.js.flow';
export type {ResultWithMap} from './types.js.flow';
export type {ResultWithoutMap} from './types.js.flow';

function noSourceMap(
code: string,
options?: MinifyOptions = {},
): ResultWithoutMap {
return minifier.noSourceMap(code, options, terser);
}

function withSourceMap(
code: string,
sourceMap: ?BabelSourceMap,
filename: string,
options?: MinifyOptions = {},
): ResultWithMap {
return minifier.withSourceMap(code, sourceMap, filename, options, terser);
}

const metroMinifier: MetroMinifier = {
noSourceMap,
withSourceMap,
};

module.exports = metroMinifier;
41 changes: 41 additions & 0 deletions packages/metro-minify-terser/src/types.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/

'use strict';

import type {BabelSourceMap} from '@babel/core';
import type {MinifyOptions} from 'metro/src/JSTransformer/worker';

export type {MinifyOptions};

export type ResultWithMap = {
code: string,
map: BabelSourceMap,
options?: MinifyOptions,
};

export type ResultWithoutMap = string;

type MinifierWithSourceMap = (
code: string,
inputMap?: ?BabelSourceMap,
filename: string,
options?: MinifyOptions,
) => ResultWithMap;
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing terser param type definition as 5th arg.


type MinifierWithoutSourceMap = (
code: string,
options?: MinifyOptions,
) => ResultWithoutMap;
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing terser param type definition as 3rd arg.


export type MetroMinifier = {
noSourceMap: MinifierWithoutSourceMap,
withSourceMap: MinifierWithSourceMap,
};
9 changes: 6 additions & 3 deletions packages/metro-minify-uglify/src/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ import type {BabelSourceMap} from '@babel/core';
function noSourceMap(
code: string,
options?: MinifyOptions = {},
uglifyModule: ?Object,
): ResultWithoutMap {
Copy link
Contributor

Choose a reason for hiding this comment

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

Types need updating here to add the extra uglifyModule arg.

return minify(code, undefined, options).code;
return minify(code, undefined, options, uglifyModule).code;
}

function withSourceMap(
code: string,
sourceMap: ?BabelSourceMap,
filename: string,
options?: MinifyOptions = {},
uglifyModule: ?Object,
Copy link
Contributor

Choose a reason for hiding this comment

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

Types need updating here to add the extra uglifyModule arg.

): ResultWithMap {
const result = minify(code, sourceMap, options);
const result = minify(code, sourceMap, options, uglifyModule);
const map: BabelSourceMap = JSON.parse(result.map);

map.sources = [filename];
Expand All @@ -45,8 +47,9 @@ function minify(
inputCode: string,
inputMap: ?BabelSourceMap,
options: MinifyOptions,
uglifyModule: ?Object,
) {
const result = uglify.minify(inputCode, {
const result = (uglifyModule || uglify).minify(inputCode, {
mangle: {
toplevel: false,
reserved: options.reserved,
Expand Down