Skip to content

Commit cddcf36

Browse files
authored
Minify: Use the minifier's decoded source map where available (#1984)
* Minify: Use the minifier's decoded source map where available When minifying, the transform worker converted Babel's decoded map to tuples and encoded it for the minifier, then decoded the minifier's output map with `source-map` (`toBabelSegments`), converted it back to tuples and encoded it again as the stored VLQ map. Terser already builds that decoded map, and only encodes it when `result.map` is read. This diff: - Adds an optional `decodedMap` to `MinifierResult`. When a minifier provides it, the worker encodes the stored map straight from it with `vlqMapFromBabelDecodedMap`, as it already does for unminified modules. Otherwise it decodes `map` with `@jridgewell/sourcemap-codec`, which is already installed via `@babel/generator`. - Has `metro-minify-terser` return Terser's `decoded_map` as `decodedMap`, and encode `map` only if it's read. - Passes Babel's own encoded `result.map` to the minifier, rather than re-encoding it from tuples. - Deprecates `MinifierResult.map` in favour of `decodedMap`, and `toBabelSegments`, `toSegmentTuple` and `tuplesFromBabelDecodedMap` in `metro-source-map`, which Metro no longer uses. The next diff removes them. Output is unchanged - across every module of our benchmark app*, the minified code, line count and stored map are byte-identical. Custom minifiers that only return `map` keep working as before. Minifying the whole app is 10.4% faster (95% CI 7.4-11.4%; 21.3s -> 19.2s of CPU on our benchmark app*), from no longer converting the map to and from tuples, and from Terser no longer encoding its map at all: | | Minify step (median) | vs `main` | |---|---|---| | `main` | 21,286ms (20,844 to 21,561) | | | This diff | 19,228ms (18,886 to 19,773) | -10.4% (-11.4 to -7.4) | ## Changelog ``` - **[Performance]**: Faster source map handling when minifying, using the minifier's decoded map if it provides one (`MinifierResult.decodedMap`) - **[Deprecated]**: Custom minifiers should return their source map as `decodedMap` - `MinifierResult.map` is deprecated - **[Deprecated]**: `metro-source-map`: Deprecate `toBabelSegments`, `toSegmentTuple` and `tuplesFromBabelDecodedMap` ``` ## Test plan New tests cover the worker using `decodedMap` without reading `map`, and `metro-minify-terser` returning Terser's decoded map and encoding `map` only on request. ``` yarn jest yarn flow check yarn lint yarn verify-api-snapshots ``` \* Benchmark: Mattermost Mobile 2.45.0 (React Native 0.83.9), its release bundle split back into its 7,082 modules (5.13M mapping segments), each minified with `metro-minify-terser` from its code and map as the worker does. Times are single-threaded totals across all modules - medians of 15 rounds interleaving `main` and both diffs in this stack, Node 22.13.1 on an M5 Pro, with 95% bootstrap confidence intervals. Output was checked byte-identical in every run. * Document the minifier contract
1 parent 65a6e52 commit cddcf36

11 files changed

Lines changed: 190 additions & 76 deletions

File tree

‎docs/Configuration.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ Type: `string`
607607
608608
Path, or package name resolvable from `metro-transform-worker`, of the minifier applied after transformation. Defaults to `'metro-minify-terser'`.
609609
610+
The minifier is called with `{code, map, filename, reserved, config}`, where `map` is the source map of `code` and `config` is [`minifierConfig`](#minifierconfig). It returns, or resolves to, `{code, decodedMap}`, where `decodedMap` is the source map of the minified code, composed with `map`, in decoded form: `{mappings, names}`, with `mappings` as an array of segment arrays per line, as [`@jridgewell/sourcemap-codec`](https://github.com/jridgewell/sourcemaps/tree/main/packages/sourcemap-codec)'s `decode` returns them. Returning an encoded `map` instead of `decodedMap` is deprecated.
611+
610612
#### `minifierConfig`
611613
612614
Type: `{[key: string]: mixed}`

‎flow-typed/sourcemap-codec.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
declare module '@jridgewell/sourcemap-codec' {
13+
declare export type SourceMapSegment =
14+
| [number]
15+
| [number, number, number, number]
16+
| [number, number, number, number, number];
17+
18+
declare export function decode(
19+
mappings: string,
20+
): Array<Array<SourceMapSegment>>;
21+
}

‎flow-typed/uglify.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,21 @@ declare module 'terser' {
110110
declare function minify(
111111
code: _Input,
112112
options: {..._Options, sourceMap: _SourceMapOptions, ...},
113-
): _Error | {..._Result, map: string};
113+
):
114+
| _Error
115+
| {
116+
..._Result,
117+
map: string,
118+
decoded_map?: ?{
119+
mappings: Array<
120+
Array<
121+
| [number]
122+
| [number, number, number, number]
123+
| [number, number, number, number, number],
124+
>,
125+
>,
126+
names: Array<string>,
127+
...
128+
},
129+
};
114130
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,26 @@ describe('Minification:', () => {
9898
const result = await minify({...baseOptions, filename});
9999
expect(result.map).toEqual({...map, sources: [filename]});
100100
});
101+
102+
test('returns the decoded source map provided by terser, and encodes `map` only when read', async () => {
103+
const decodedMap = {...map, mappings: [[[0, 0, 0, 0]]]};
104+
let encodeCount = 0;
105+
/* $FlowFixMe[incompatible-type] The mocked `minify` result isn't typed as
106+
* Terser's. */
107+
terser.minify.mockResolvedValue({
108+
code: '',
109+
decoded_map: decodedMap,
110+
// flowlint-next-line unsafe-getters-setters:off
111+
get map() {
112+
encodeCount++;
113+
return JSON.stringify(map);
114+
},
115+
});
116+
const result = await minify({...baseOptions, filename});
117+
expect(result.decodedMap).toEqual(decodedMap);
118+
expect(encodeCount).toBe(0);
119+
expect(result.map).toEqual({...map, sources: [filename]});
120+
expect(result.map).toBe(result.map);
121+
expect(encodeCount).toBe(1);
122+
});
101123
});

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @oncall react_native
1010
*/
1111

12-
import type {BasicSourceMap} from 'metro-source-map';
12+
import type {BabelDecodedMap, BasicSourceMap} from 'metro-source-map';
1313
import type {MinifierOptions, MinifierResult} from 'metro-transform-worker';
1414

1515
import terser from 'terser';
@@ -18,22 +18,33 @@ export default async function minifier(
1818
options: MinifierOptions,
1919
): Promise<MinifierResult> {
2020
const result = await minify(options);
21+
const {getMap} = result;
2122

22-
if (!options.map || result.map == null) {
23+
if (!options.map || getMap == null) {
2324
return {code: result.code};
2425
}
2526

26-
const map: BasicSourceMap = JSON.parse(result.map);
27+
let map: ?BasicSourceMap;
2728

28-
return {code: result.code, map: {...map, sources: [options.filename]}};
29+
return {
30+
code: result.code,
31+
// Terser encodes its map only when `result.map` is read.
32+
// flowlint-next-line unsafe-getters-setters:off
33+
get map(): BasicSourceMap {
34+
if (map == null) {
35+
map = {...JSON.parse(getMap()), sources: [options.filename]};
36+
}
37+
return map;
38+
},
39+
decodedMap: result.decodedMap,
40+
};
2941
}
3042

31-
async function minify({
32-
code,
33-
map,
34-
reserved,
35-
config,
36-
}: MinifierOptions): Promise<{code: string, map: ?string}> {
43+
async function minify({code, map, reserved, config}: MinifierOptions): Promise<{
44+
code: string,
45+
getMap: ?() => string,
46+
decodedMap: ?BabelDecodedMap,
47+
}> {
3748
const options = {
3849
...config,
3950
output: {
@@ -65,6 +76,7 @@ async function minify({
6576

6677
return {
6778
code: result.code,
68-
map: result.map,
79+
getMap: options.sourceMap ? () => result.map : null,
80+
decodedMap: result.decoded_map,
6981
};
7082
}

‎packages/metro-source-map/API.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ export interface SourceMapGenerator {
125125
toString(file?: string, options?: {excludeSource?: boolean | undefined}): string;
126126
}
127127

128+
// @deprecated
128129
export function toBabelSegments(sourceMap: BasicSourceMap): Array<BabelSourceMapSegment>;
129130

131+
// @deprecated
130132
export function toSegmentTuple(mapping: BabelSourceMapSegment): MetroSourceMapSegmentTuple;
131133

134+
// @deprecated
132135
export function tuplesFromBabelDecodedMap(decodedMap: BabelDecodedMap): Array<MetroSourceMapSegmentTuple>;
133136

134137
export type VlqMap = {

‎packages/metro-source-map/src/source-map.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ function toIndexMapSection(module: RawMappingsModule): BasicSourceMap {
346346
/**
347347
* Transforms a standard source map object into a Raw Mappings object, to be
348348
* used across the bundler.
349+
*
350+
* @deprecated Unused by Metro, and will be removed. `Consumer`'s
351+
* `generatedMappings()` decodes a source map.
349352
*/
350353
function toBabelSegments(
351354
sourceMap: BasicSourceMap,
@@ -383,6 +386,9 @@ function toBabelSegments(
383386
return rawMappings;
384387
}
385388

389+
/**
390+
* @deprecated Unused by Metro, and will be removed.
391+
*/
386392
function toSegmentTuple(
387393
mapping: BabelSourceMapSegment,
388394
): MetroSourceMapSegmentTuple {
@@ -409,6 +415,8 @@ function toSegmentTuple(
409415
* during generation, whereas accessing `rawMappings` triggers a second decode
410416
* (`allMappings`) that allocates ~4-5 objects per segment. No terminating
411417
* mapping is appended (callers that need one use `countLinesAndTerminateMap`).
418+
*
419+
* @deprecated Unused by Metro, and will be removed.
412420
*/
413421
function tuplesFromBabelDecodedMap(
414422
decodedMap: BabelDecodedMap,

‎packages/metro-transform-worker/API.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ export type MinifierOptions = {
7474
config: MinifierConfig;
7575
};
7676

77-
export type MinifierResult = {code: string; map?: BasicSourceMap | undefined};
77+
export type MinifierResult = {
78+
code: string;
79+
map?: BasicSourceMap | undefined;
80+
decodedMap?: null | undefined | BabelDecodedMap;
81+
};
7882

7983
export const transform: (config: JsTransformerConfig, projectRoot: string, projectRelativePath: string, data: Buffer, options: JsTransformOptions, assetUrlPath?: string) => Promise<TransformResponse>;
8084

‎packages/metro-transform-worker/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@babel/generator": "^7.29.1",
2424
"@babel/parser": "^7.29.9",
2525
"@babel/types": "^7.29.8",
26+
"@jridgewell/sourcemap-codec": "^1.5.0",
2627
"flow-enums-runtime": "^0.0.6",
2728
"metro": "0.87.2",
2829
"metro-babel-transformer": "0.87.2",

‎packages/metro-transform-worker/src/__tests__/index-test.js‎

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
'use strict';
1313

1414
jest
15-
.mock('../utils/getMinifier', () => () => ({code, map, config}) => {
16-
const trimmed = config.output.comments
17-
? code
18-
: code.replace('/*#__PURE__*/', '');
19-
return {
20-
code: trimmed.replace('arbitrary(code)', 'minified(code)'),
21-
map,
22-
};
15+
.mock('../utils/getMinifier', () => {
16+
const minifier = jest.fn(({code, map, config}) => {
17+
const trimmed = config.output.comments
18+
? code
19+
: code.replace('/*#__PURE__*/', '');
20+
return {
21+
code: trimmed.replace('arbitrary(code)', 'minified(code)'),
22+
map,
23+
};
24+
});
25+
return () => minifier;
2326
})
2427
.mock('metro-transform-plugins', () => ({
2528
...jest.requireActual('metro-transform-plugins'),
@@ -39,6 +42,7 @@ import typeof FSType from 'node:fs';
3942
const {
4043
posixToSystemPath: p,
4144
} = require('metro-resolver/private/__tests__/utils');
45+
const {vlqMapFromBabelDecodedMap} = require('metro-source-map');
4246
const {Buffer} = require('node:buffer');
4347
const path = require('node:path');
4448

@@ -537,7 +541,7 @@ test('emits a compact VlqMap for both the non-minified and minified paths', asyn
537541
source,
538542
{...baseTransformOptions, experimentalImportSupport: true},
539543
);
540-
// Minified path re-encodes the minifier's tuple output to VLQ.
544+
// Minified path encodes VLQ from the minifier's decoded map.
541545
const minifiedResult = await Transformer.transform(
542546
baseConfig,
543547
'/root',
@@ -559,6 +563,32 @@ test('emits a compact VlqMap for both the non-minified and minified paths', asyn
559563
}
560564
});
561565

566+
test("uses the minifier's decoded map, if it has one, without reading `map`", async () => {
567+
const decodedMap = {mappings: [[[0, 0, 0, 0, 0]]], names: ['arbitrary']};
568+
jest
569+
.requireMock('../utils/getMinifier')()
570+
.mockImplementationOnce(() => ({
571+
code: 'minified(code);',
572+
decodedMap,
573+
// flowlint-next-line unsafe-getters-setters:off
574+
get map() {
575+
throw new Error('Expected the decoded map to be used');
576+
},
577+
}));
578+
579+
const result = await Transformer.transform(
580+
baseConfig,
581+
'/root',
582+
'local/file.js',
583+
Buffer.from('arbitrary(code);', 'utf8'),
584+
{...baseTransformOptions, dev: false, minify: true},
585+
);
586+
587+
expect(result.output[0].data.map).toEqual(
588+
vlqMapFromBabelDecodedMap(decodedMap, [1, 'minified(code);'.length]),
589+
);
590+
});
591+
562592
test('throws if the reserved dependency map name appears in the input', async () => {
563593
await expect(
564594
Transformer.transform(

0 commit comments

Comments
 (0)