Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ Type: `string` (default: `'metro-minify-terser'`)

Path, or package name resolvable from `metro-transform-worker`, to the minifier that minifies the code after transformation.

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.

#### `minifierConfig`

Type: `{[key: string]: mixed}`
Expand Down
29 changes: 6 additions & 23 deletions packages/metro-minify-terser/src/__tests__/minify-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,17 @@ describe('Minification:', () => {
expect(result.code).toBe(code);
});

test('parses the source map object provided by terser and sets the sources property', async () => {
/* $FlowFixMe[incompatible-type](>=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.mockResolvedValue({map: JSON.stringify(map), code: ''});
const result = await minify({...baseOptions, filename});
expect(result.map).toEqual({...map, sources: [filename]});
});

test('returns the decoded source map provided by terser, and encodes `map` only when read', async () => {
const decodedMap = {...map, mappings: [[[0, 0, 0, 0]]]};
let encodeCount = 0;
test('returns the decoded source map provided by terser', async () => {
/* $FlowFixMe[incompatible-type] The mocked `minify` result isn't typed as
* Terser's. */
terser.minify.mockResolvedValue({
code: '',
decoded_map: decodedMap,
// flowlint-next-line unsafe-getters-setters:off
get map() {
encodeCount++;
return JSON.stringify(map);
},
decoded_map: {...map, names: ['name0'], mappings: [[[0, 0, 0, 0, 0]]]},
});
const result = await minify({...baseOptions, filename});
expect(result.decodedMap).toEqual(decodedMap);
expect(encodeCount).toBe(0);
expect(result.map).toEqual({...map, sources: [filename]});
expect(result.map).toBe(result.map);
expect(encodeCount).toBe(1);
expect(result).toEqual({
code: '',
decodedMap: {names: ['name0'], mappings: [[[0, 0, 0, 0, 0]]]},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ const GLO: MinifierOptions = {
test('parallel calls do not clobber each other', async () => {
const [barResult, gloResult] = await Promise.all([minify(BAR), minify(GLO)]);

const barMap = barResult.map;
const gloMap = gloResult.map;
const barMap = barResult.decodedMap;
const gloMap = gloResult.decodedMap;

expect(barMap).not.toBeNull();
expect(gloMap).not.toEqual(barMap);
});
36 changes: 14 additions & 22 deletions packages/metro-minify-terser/src/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @oncall react_native
*/

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

import terser from 'terser';
Expand All @@ -18,32 +17,26 @@ export default async function minifier(
options: MinifierOptions,
): Promise<MinifierResult> {
const result = await minify(options);
const {getMap} = result;

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

let map: ?BasicSourceMap;

return {
code: result.code,
// Terser encodes its map only when `result.map` is read.
// flowlint-next-line unsafe-getters-setters:off
get map(): BasicSourceMap {
if (map == null) {
map = {...JSON.parse(getMap()), sources: [options.filename]};
}
return map;
},
decodedMap: result.decodedMap,
};
const {mappings, names} = result.decodedMap;
return {code: result.code, decodedMap: {mappings, names}};
}

async function minify({code, map, reserved, config}: MinifierOptions): Promise<{
code: string,
getMap: ?() => string,
decodedMap: ?BabelDecodedMap,
decodedMap: ?{
mappings: Array<
Array<
| [number]
| [number, number, number, number]
| [number, number, number, number, number],
>,
>,
names: Array<string>,
...
},
}> {
const options = {
...config,
Expand Down Expand Up @@ -76,7 +69,6 @@ async function minify({code, map, reserved, config}: MinifierOptions): Promise<{

return {
code: result.code,
getMap: options.sourceMap ? () => result.map : null,
decodedMap: result.decoded_map,
};
}
9 changes: 0 additions & 9 deletions packages/metro-source-map/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,6 @@ export interface SourceMapGenerator {
toString(file?: string, options?: {excludeSource?: boolean | undefined}): string;
}

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

// @deprecated
export function toSegmentTuple(mapping: BabelSourceMapSegment): MetroSourceMapSegmentTuple;

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

export type VlqMap = {
readonly mappings: string;
readonly names: ReadonlyArray<string>;
Expand Down
1 change: 1 addition & 0 deletions packages/metro-source-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"@babel/traverse": "^7.29.0",
"@babel/types": "^7.29.8",
"@jridgewell/sourcemap-codec": "^1.5.0",
"flow-enums-runtime": "^0.0.6",
"invariant": "^2.2.4",
"metro-symbolicate": "0.87.1",
Expand Down

This file was deleted.

Loading
Loading