Skip to content

Commit

Permalink
Change API for the minifiers
Browse files Browse the repository at this point in the history
Reviewed By: davidaurelio

Differential Revision: D10161586

fbshipit-source-id: 38a3295266a498a1f1c3350b83c06555d7e1c8b8
  • Loading branch information
rafeca authored and facebook-github-bot committed Oct 5, 2018
1 parent b1981c7 commit 681924e
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 68 deletions.
18 changes: 15 additions & 3 deletions packages/metro-minify-terser/__tests__/minify-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ function getFakeMap(): BabelSourceMap {
};
}

const baseOptions = {
code: '',
map: getFakeMap(),
filename: '',
reserved: [],
};

describe('Minification:', () => {
const filename = '/arbitrary/file.js';
const code = 'arbitrary(code)';
Expand All @@ -47,7 +54,12 @@ describe('Minification:', () => {
});

it('passes file name, code, and source map to `terser`', () => {
minify(code, map, filename);
minify({
...baseOptions,
code,
map,
filename,
});
expect(terser.minify).toBeCalledWith(
code,
objectContaining({
Expand All @@ -61,13 +73,13 @@ describe('Minification:', () => {

it('returns the code provided by terser', () => {
terser.minify.mockReturnValue({code, map: '{}'});
const result = minify('', getFakeMap(), '');
const result = minify(baseOptions);
expect(result.code).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('', getFakeMap(), filename);
const result = minify({...baseOptions, filename});
expect(result.map).toEqual({...map, sources: [filename]});
});
});
38 changes: 15 additions & 23 deletions packages/metro-minify-terser/src/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,40 @@ const terser = require('terser');

import type {BabelSourceMap} from '@babel/core';
import type {
MetroMinifier,
MetroMinifierResult,
MinifyOptions,
MinifierResult,
MinifierOptions,
} from 'metro/src/shared/types.flow.js';

function minifier(
code: string,
sourceMap: ?BabelSourceMap,
filename: string,
options?: MinifyOptions = {},
): MetroMinifierResult {
const result = minify(code, sourceMap, options);
function minifier(options: MinifierOptions): MinifierResult {
const result = minify(options);

if (!sourceMap) {
if (!options.map || result.map == null) {
return {code: result.code};
}

const map: BabelSourceMap = JSON.parse(result.map);
map.sources = [filename];
map.sources = [options.filename];

return {code: result.code, map};
}

function minify(
inputCode: string,
inputMap: ?BabelSourceMap,
options: MinifyOptions,
) {
const result = terser.minify(inputCode, {
function minify({
code,
map,
reserved,
}: MinifierOptions): {code: string, map: ?string} {
const result = terser.minify(code, {
mangle: {
toplevel: false,
reserved: options.reserved,
reserved,
},
output: {
ascii_only: true,
quote_style: 3,
wrap_iife: true,
},
sourceMap: {
content: inputMap,
content: map,
includeSources: false,
},
toplevel: false,
Expand All @@ -73,6 +67,4 @@ function minify(
};
}

const metroMinifier: MetroMinifier = minifier;

module.exports = metroMinifier;
module.exports = minifier;
18 changes: 15 additions & 3 deletions packages/metro-minify-uglify/__tests__/minify-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ function getFakeMap(): BabelSourceMap {
};
}

const baseOptions = {
code: '',
map: getFakeMap(),
filename: '',
reserved: [],
};

describe('Minification:', () => {
const filename = '/arbitrary/file.js';
const code = 'arbitrary(code)';
Expand All @@ -47,7 +54,12 @@ describe('Minification:', () => {
});

it('passes file name, code, and source map to `uglify`', () => {
minify(code, map, filename);
minify({
...baseOptions,
code,
map,
filename,
});
expect(uglify.minify).toBeCalledWith(
code,
objectContaining({
Expand All @@ -61,13 +73,13 @@ describe('Minification:', () => {

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

it('parses the source map object provided by uglify and sets the sources property', () => {
uglify.minify.mockReturnValue({map: JSON.stringify(map), code: ''});
const result = minify('', getFakeMap(), filename);
const result = minify({...baseOptions, filename});
expect(result.map).toEqual({...map, sources: [filename]});
});
});
37 changes: 15 additions & 22 deletions packages/metro-minify-uglify/src/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,40 @@ const uglify = require('uglify-es');

import type {BabelSourceMap} from '@babel/core';
import type {
MetroMinifier,
MetroMinifierResult,
MinifyOptions,
MinifierResult,
MinifierOptions,
} from 'metro/src/shared/types.flow.js';

function minifier(
code: string,
sourceMap: ?BabelSourceMap,
filename: string,
options?: MinifyOptions = {},
): MetroMinifierResult {
const result = minify(code, sourceMap, options);
function minifier(options: MinifierOptions): MinifierResult {
const result = minify(options);

if (!sourceMap) {
if (!options.map || result.map == null) {
return {code: result.code};
}

const map: BabelSourceMap = JSON.parse(result.map);

map.sources = [filename];
map.sources = [options.filename];

return {code: result.code, map};
}

function minify(
inputCode: string,
inputMap: ?BabelSourceMap,
options: MinifyOptions,
) {
const result = uglify.minify(inputCode, {
function minify({
code,
map,
reserved,
}: MinifierOptions): {code: string, map: ?string} {
const result = uglify.minify(code, {
mangle: {
toplevel: false,
reserved: options.reserved,
reserved,
},
output: {
ascii_only: true,
quote_style: 3,
wrap_iife: true,
},
sourceMap: {
content: inputMap,
content: map,
includeSources: false,
},
toplevel: false,
Expand All @@ -74,4 +67,4 @@ function minify(
};
}

module.exports = (minifier: MetroMinifier);
module.exports = minifier;
21 changes: 14 additions & 7 deletions packages/metro/src/JSTransformer/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const {

import type {TransformResultDependency} from 'metro/src/DeltaBundler';
import type {DynamicRequiresBehavior} from '../ModuleGraph/worker/collectDependencies';
import type {Ast} from '@babel/core';
import type {Ast, BabelSourceMap} from '@babel/core';
import type {Plugins as BabelPlugins} from 'babel-core';
import type {MetroSourceMapSegmentTuple} from 'metro-source-map';

Expand Down Expand Up @@ -69,9 +69,11 @@ export type BabelTransformer = {|
getCacheKey?: () => string,
|};

export type MinifyOptions = {
filename?: string,
reserved?: $ReadOnlyArray<string>,
export type MinifierOptions = {
code: string,
map: ?BabelSourceMap,
filename: string,
reserved: $ReadOnlyArray<string>,
};

export type Type = 'script' | 'module' | 'asset';
Expand Down Expand Up @@ -314,7 +316,7 @@ class JsTransformer {
result.code,
sourceCode,
map,
{reserved},
reserved,
));
}

Expand All @@ -326,7 +328,7 @@ class JsTransformer {
code: string,
source: string,
map: Array<MetroSourceMapSegmentTuple>,
options?: MinifyOptions = {},
reserved?: $ReadOnlyArray<string> = [],
): Promise<{
code: string,
map: Array<MetroSourceMapSegmentTuple>,
Expand All @@ -338,7 +340,12 @@ class JsTransformer {
const minify = getMinifier(this._config.minifierPath);

try {
const minified = minify(code, sourceMap, filename, options);
const minified = minify({
code,
map: sourceMap,
filename,
reserved,
});

return {
code: minified.code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
jest
.mock('../constant-folding-plugin')
.mock('../inline-plugin')
.mock('../../../lib/getMinifier', () => () => (code, map) => ({
.mock('../../../lib/getMinifier', () => () => ({code, map}) => ({
code: code.replace('arbitrary(code)', 'minified(code)'),
map,
}))
Expand Down
13 changes: 4 additions & 9 deletions packages/metro/src/shared/types.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import type {
CustomTransformOptions,
MinifyOptions,
MinifierOptions,
} from '../JSTransformer/worker';
import type {BabelSourceMap} from '@babel/core';
import type {
Expand Down Expand Up @@ -84,16 +84,11 @@ export type RequestOptions = {|
onProgress?: (transformedFileCount: number, totalFileCount: number) => void,
|};

export type {MinifyOptions};
export type {MinifierOptions};

export type MetroMinifierResult = {
export type MinifierResult = {
code: string,
map?: BabelSourceMap,
};

export type MetroMinifier = (
code: string,
inputMap: ?BabelSourceMap,
filename: string,
options?: MinifyOptions,
) => MetroMinifierResult;
export type MetroMinifier = MinifierOptions => MinifierResult;

0 comments on commit 681924e

Please sign in to comment.