Skip to content

Commit 681924e

Browse files
rafecafacebook-github-bot
authored andcommitted
Change API for the minifiers
Reviewed By: davidaurelio Differential Revision: D10161586 fbshipit-source-id: 38a3295266a498a1f1c3350b83c06555d7e1c8b8
1 parent b1981c7 commit 681924e

File tree

7 files changed

+79
-68
lines changed

7 files changed

+79
-68
lines changed

packages/metro-minify-terser/__tests__/minify-test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ function getFakeMap(): BabelSourceMap {
3333
};
3434
}
3535

36+
const baseOptions = {
37+
code: '',
38+
map: getFakeMap(),
39+
filename: '',
40+
reserved: [],
41+
};
42+
3643
describe('Minification:', () => {
3744
const filename = '/arbitrary/file.js';
3845
const code = 'arbitrary(code)';
@@ -47,7 +54,12 @@ describe('Minification:', () => {
4754
});
4855

4956
it('passes file name, code, and source map to `terser`', () => {
50-
minify(code, map, filename);
57+
minify({
58+
...baseOptions,
59+
code,
60+
map,
61+
filename,
62+
});
5163
expect(terser.minify).toBeCalledWith(
5264
code,
5365
objectContaining({
@@ -61,13 +73,13 @@ describe('Minification:', () => {
6173

6274
it('returns the code provided by terser', () => {
6375
terser.minify.mockReturnValue({code, map: '{}'});
64-
const result = minify('', getFakeMap(), '');
76+
const result = minify(baseOptions);
6577
expect(result.code).toBe(code);
6678
});
6779

6880
it('parses the source map object provided by terser and sets the sources property', () => {
6981
terser.minify.mockReturnValue({map: JSON.stringify(map), code: ''});
70-
const result = minify('', getFakeMap(), filename);
82+
const result = minify({...baseOptions, filename});
7183
expect(result.map).toEqual({...map, sources: [filename]});
7284
});
7385
});

packages/metro-minify-terser/src/minifier.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,40 @@ const terser = require('terser');
1414

1515
import type {BabelSourceMap} from '@babel/core';
1616
import type {
17-
MetroMinifier,
18-
MetroMinifierResult,
19-
MinifyOptions,
17+
MinifierResult,
18+
MinifierOptions,
2019
} from 'metro/src/shared/types.flow.js';
2120

22-
function minifier(
23-
code: string,
24-
sourceMap: ?BabelSourceMap,
25-
filename: string,
26-
options?: MinifyOptions = {},
27-
): MetroMinifierResult {
28-
const result = minify(code, sourceMap, options);
21+
function minifier(options: MinifierOptions): MinifierResult {
22+
const result = minify(options);
2923

30-
if (!sourceMap) {
24+
if (!options.map || result.map == null) {
3125
return {code: result.code};
3226
}
3327

3428
const map: BabelSourceMap = JSON.parse(result.map);
35-
map.sources = [filename];
29+
map.sources = [options.filename];
3630

3731
return {code: result.code, map};
3832
}
3933

40-
function minify(
41-
inputCode: string,
42-
inputMap: ?BabelSourceMap,
43-
options: MinifyOptions,
44-
) {
45-
const result = terser.minify(inputCode, {
34+
function minify({
35+
code,
36+
map,
37+
reserved,
38+
}: MinifierOptions): {code: string, map: ?string} {
39+
const result = terser.minify(code, {
4640
mangle: {
4741
toplevel: false,
48-
reserved: options.reserved,
42+
reserved,
4943
},
5044
output: {
5145
ascii_only: true,
5246
quote_style: 3,
5347
wrap_iife: true,
5448
},
5549
sourceMap: {
56-
content: inputMap,
50+
content: map,
5751
includeSources: false,
5852
},
5953
toplevel: false,
@@ -73,6 +67,4 @@ function minify(
7367
};
7468
}
7569

76-
const metroMinifier: MetroMinifier = minifier;
77-
78-
module.exports = metroMinifier;
70+
module.exports = minifier;

packages/metro-minify-uglify/__tests__/minify-test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ function getFakeMap(): BabelSourceMap {
3333
};
3434
}
3535

36+
const baseOptions = {
37+
code: '',
38+
map: getFakeMap(),
39+
filename: '',
40+
reserved: [],
41+
};
42+
3643
describe('Minification:', () => {
3744
const filename = '/arbitrary/file.js';
3845
const code = 'arbitrary(code)';
@@ -47,7 +54,12 @@ describe('Minification:', () => {
4754
});
4855

4956
it('passes file name, code, and source map to `uglify`', () => {
50-
minify(code, map, filename);
57+
minify({
58+
...baseOptions,
59+
code,
60+
map,
61+
filename,
62+
});
5163
expect(uglify.minify).toBeCalledWith(
5264
code,
5365
objectContaining({
@@ -61,13 +73,13 @@ describe('Minification:', () => {
6173

6274
it('returns the code provided by uglify', () => {
6375
uglify.minify.mockReturnValue({code, map: '{}'});
64-
const result = minify('', getFakeMap(), '');
76+
const result = minify(baseOptions);
6577
expect(result.code).toBe(code);
6678
});
6779

6880
it('parses the source map object provided by uglify and sets the sources property', () => {
6981
uglify.minify.mockReturnValue({map: JSON.stringify(map), code: ''});
70-
const result = minify('', getFakeMap(), filename);
82+
const result = minify({...baseOptions, filename});
7183
expect(result.map).toEqual({...map, sources: [filename]});
7284
});
7385
});

packages/metro-minify-uglify/src/minifier.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,40 @@ const uglify = require('uglify-es');
1414

1515
import type {BabelSourceMap} from '@babel/core';
1616
import type {
17-
MetroMinifier,
18-
MetroMinifierResult,
19-
MinifyOptions,
17+
MinifierResult,
18+
MinifierOptions,
2019
} from 'metro/src/shared/types.flow.js';
2120

22-
function minifier(
23-
code: string,
24-
sourceMap: ?BabelSourceMap,
25-
filename: string,
26-
options?: MinifyOptions = {},
27-
): MetroMinifierResult {
28-
const result = minify(code, sourceMap, options);
21+
function minifier(options: MinifierOptions): MinifierResult {
22+
const result = minify(options);
2923

30-
if (!sourceMap) {
24+
if (!options.map || result.map == null) {
3125
return {code: result.code};
3226
}
3327

3428
const map: BabelSourceMap = JSON.parse(result.map);
35-
36-
map.sources = [filename];
29+
map.sources = [options.filename];
3730

3831
return {code: result.code, map};
3932
}
4033

41-
function minify(
42-
inputCode: string,
43-
inputMap: ?BabelSourceMap,
44-
options: MinifyOptions,
45-
) {
46-
const result = uglify.minify(inputCode, {
34+
function minify({
35+
code,
36+
map,
37+
reserved,
38+
}: MinifierOptions): {code: string, map: ?string} {
39+
const result = uglify.minify(code, {
4740
mangle: {
4841
toplevel: false,
49-
reserved: options.reserved,
42+
reserved,
5043
},
5144
output: {
5245
ascii_only: true,
5346
quote_style: 3,
5447
wrap_iife: true,
5548
},
5649
sourceMap: {
57-
content: inputMap,
50+
content: map,
5851
includeSources: false,
5952
},
6053
toplevel: false,
@@ -74,4 +67,4 @@ function minify(
7467
};
7568
}
7669

77-
module.exports = (minifier: MetroMinifier);
70+
module.exports = minifier;

packages/metro/src/JSTransformer/worker.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const {
3636

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

@@ -69,9 +69,11 @@ export type BabelTransformer = {|
6969
getCacheKey?: () => string,
7070
|};
7171

72-
export type MinifyOptions = {
73-
filename?: string,
74-
reserved?: $ReadOnlyArray<string>,
72+
export type MinifierOptions = {
73+
code: string,
74+
map: ?BabelSourceMap,
75+
filename: string,
76+
reserved: $ReadOnlyArray<string>,
7577
};
7678

7779
export type Type = 'script' | 'module' | 'asset';
@@ -314,7 +316,7 @@ class JsTransformer {
314316
result.code,
315317
sourceCode,
316318
map,
317-
{reserved},
319+
reserved,
318320
));
319321
}
320322

@@ -326,7 +328,7 @@ class JsTransformer {
326328
code: string,
327329
source: string,
328330
map: Array<MetroSourceMapSegmentTuple>,
329-
options?: MinifyOptions = {},
331+
reserved?: $ReadOnlyArray<string> = [],
330332
): Promise<{
331333
code: string,
332334
map: Array<MetroSourceMapSegmentTuple>,
@@ -338,7 +340,12 @@ class JsTransformer {
338340
const minify = getMinifier(this._config.minifierPath);
339341

340342
try {
341-
const minified = minify(code, sourceMap, filename, options);
343+
const minified = minify({
344+
code,
345+
map: sourceMap,
346+
filename,
347+
reserved,
348+
});
342349

343350
return {
344351
code: minified.code,

packages/metro/src/JSTransformer/worker/__tests__/worker-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
jest
1313
.mock('../constant-folding-plugin')
1414
.mock('../inline-plugin')
15-
.mock('../../../lib/getMinifier', () => () => (code, map) => ({
15+
.mock('../../../lib/getMinifier', () => () => ({code, map}) => ({
1616
code: code.replace('arbitrary(code)', 'minified(code)'),
1717
map,
1818
}))

packages/metro/src/shared/types.flow.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import type {
1313
CustomTransformOptions,
14-
MinifyOptions,
14+
MinifierOptions,
1515
} from '../JSTransformer/worker';
1616
import type {BabelSourceMap} from '@babel/core';
1717
import type {
@@ -84,16 +84,11 @@ export type RequestOptions = {|
8484
onProgress?: (transformedFileCount: number, totalFileCount: number) => void,
8585
|};
8686

87-
export type {MinifyOptions};
87+
export type {MinifierOptions};
8888

89-
export type MetroMinifierResult = {
89+
export type MinifierResult = {
9090
code: string,
9191
map?: BabelSourceMap,
9292
};
9393

94-
export type MetroMinifier = (
95-
code: string,
96-
inputMap: ?BabelSourceMap,
97-
filename: string,
98-
options?: MinifyOptions,
99-
) => MetroMinifierResult;
94+
export type MetroMinifier = MinifierOptions => MinifierResult;

0 commit comments

Comments
 (0)