diff --git a/docs/Configuration.md b/docs/Configuration.md index 04539f33c3..25c61ffc20 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -607,7 +607,7 @@ Type: `string` Path, or package name resolvable from `metro-transform-worker`, of the minifier applied after transformation. Defaults to `'metro-minify-terser'`. -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. +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` diff --git a/packages/metro-minify-terser/src/__tests__/minify-test.js b/packages/metro-minify-terser/src/__tests__/minify-test.js index af1225a37e..3ff932ae54 100644 --- a/packages/metro-minify-terser/src/__tests__/minify-test.js +++ b/packages/metro-minify-terser/src/__tests__/minify-test.js @@ -58,7 +58,7 @@ describe('Minification:', () => { /* $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({code: '', map: '{}'}); + terser.minify.mockResolvedValue({code: ''}); map = getFakeMap(); }); @@ -85,39 +85,22 @@ describe('Minification:', () => { /* $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({code, map: '{}'}); + terser.minify.mockResolvedValue({code}); const result = await minify(baseOptions); 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]]]}, + }); }); }); diff --git a/packages/metro-minify-terser/src/__tests__/terser-issue-1341-test.js b/packages/metro-minify-terser/src/__tests__/terser-issue-1341-test.js index 3ab1486065..29cdd9e6b2 100644 --- a/packages/metro-minify-terser/src/__tests__/terser-issue-1341-test.js +++ b/packages/metro-minify-terser/src/__tests__/terser-issue-1341-test.js @@ -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).toBeDefined(); expect(gloMap).not.toEqual(barMap); }); diff --git a/packages/metro-minify-terser/src/minifier.js b/packages/metro-minify-terser/src/minifier.js index 5680964b32..10020f32a2 100644 --- a/packages/metro-minify-terser/src/minifier.js +++ b/packages/metro-minify-terser/src/minifier.js @@ -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'; @@ -18,32 +17,16 @@ export default async function minifier( options: MinifierOptions, ): Promise { 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: MinifierResult['decodedMap'], }> { const options = { ...config, @@ -76,7 +59,6 @@ async function minify({code, map, reserved, config}: MinifierOptions): Promise<{ return { code: result.code, - getMap: options.sourceMap ? () => result.map : null, decodedMap: result.decoded_map, }; } diff --git a/packages/metro-transform-worker/API.md b/packages/metro-transform-worker/API.md index 78e7f29f46..2175cdb991 100644 --- a/packages/metro-transform-worker/API.md +++ b/packages/metro-transform-worker/API.md @@ -76,7 +76,6 @@ export type MinifierOptions = { export type MinifierResult = { code: string; - map?: BasicSourceMap | undefined; decodedMap?: null | undefined | BabelDecodedMap; }; diff --git a/packages/metro-transform-worker/package.json b/packages/metro-transform-worker/package.json index 2eec888e7e..4c408ab1e3 100644 --- a/packages/metro-transform-worker/package.json +++ b/packages/metro-transform-worker/package.json @@ -23,7 +23,6 @@ "@babel/generator": "^7.29.1", "@babel/parser": "^7.29.9", "@babel/types": "^7.29.8", - "@jridgewell/sourcemap-codec": "^1.5.0", "flow-enums-runtime": "^0.0.6", "metro": "0.87.2", "metro-babel-transformer": "0.87.2", @@ -35,6 +34,7 @@ "nullthrows": "^1.1.1" }, "devDependencies": { + "@jridgewell/sourcemap-codec": "^1.5.0", "@react-native/metro-babel-transformer": "0.78.0", "metro-memory-fs": "*", "metro-resolver": "0.87.2" diff --git a/packages/metro-transform-worker/src/__tests__/index-test.js b/packages/metro-transform-worker/src/__tests__/index-test.js index 10155a603d..78c2d80ea3 100644 --- a/packages/metro-transform-worker/src/__tests__/index-test.js +++ b/packages/metro-transform-worker/src/__tests__/index-test.js @@ -13,13 +13,19 @@ jest .mock('../utils/getMinifier', () => { + const {decode} = jest.requireActual( + '@jridgewell/sourcemap-codec', + ); const minifier = jest.fn(({code, map, config}) => { const trimmed = config.output.comments ? code : code.replace('/*#__PURE__*/', ''); return { code: trimmed.replace('arbitrary(code)', 'minified(code)'), - map, + decodedMap: + map == null + ? null + : {mappings: decode(map.mappings), names: map.names}, }; }); return () => minifier; @@ -33,6 +39,7 @@ jest import type {JsTransformerConfig, JsTransformOptions} from '../index'; import typeof * as TransformerType from '../index'; +import typeof * as MockSourcemapCodec from '@jridgewell/sourcemap-codec'; import type { BabelTransformer, BabelTransformerArgs, @@ -563,18 +570,11 @@ test('emits a compact VlqMap for both the non-minified and minified paths', asyn } }); -test("uses the minifier's decoded map, if it has one, without reading `map`", async () => { +test("encodes the minifier's decoded map as the module's map", async () => { const decodedMap = {mappings: [[[0, 0, 0, 0, 0]]], names: ['arbitrary']}; jest .requireMock('../utils/getMinifier')() - .mockImplementationOnce(() => ({ - code: 'minified(code);', - decodedMap, - // flowlint-next-line unsafe-getters-setters:off - get map() { - throw new Error('Expected the decoded map to be used'); - }, - })); + .mockImplementationOnce(() => ({code: 'minified(code);', decodedMap})); const result = await Transformer.transform( baseConfig, @@ -589,6 +589,49 @@ test("uses the minifier's decoded map, if it has one, without reading `map`", as ); }); +test('throws if a minifier returns only an encoded map', async () => { + jest + .requireMock('../utils/getMinifier')() + .mockImplementationOnce(() => ({ + code: 'minified(code);', + map: {version: 3, sources: [], names: [], mappings: ''}, + })); + + await expect( + Transformer.transform( + baseConfig, + '/root', + 'local/file.js', + Buffer.from('arbitrary(code);', 'utf8'), + {...baseTransformOptions, dev: false, minify: true}, + ), + ).rejects.toThrow('Return it as `decodedMap` instead'); +}); + +test.each([[''], [undefined]])( + 'stores an empty map if a minifier returns no decoded map and `map` is %j', + async map => { + jest + .requireMock('../utils/getMinifier')() + .mockImplementationOnce(() => ({code: 'minified(code);', map})); + + const result = await Transformer.transform( + baseConfig, + '/root', + 'local/file.js', + Buffer.from('arbitrary(code);', 'utf8'), + {...baseTransformOptions, dev: false, minify: true}, + ); + + expect(result.output[0].data.map).toEqual( + vlqMapFromBabelDecodedMap({mappings: [], names: []}, [ + 1, + 'minified(code);'.length, + ]), + ); + }, +); + test('throws if the reserved dependency map name appears in the input', async () => { await expect( Transformer.transform( diff --git a/packages/metro-transform-worker/src/index.js b/packages/metro-transform-worker/src/index.js index fd85158c65..42069c3ff5 100644 --- a/packages/metro-transform-worker/src/index.js +++ b/packages/metro-transform-worker/src/index.js @@ -42,8 +42,6 @@ import {transformFromAstSync, traverse} from '@babel/core'; import generate from '@babel/generator'; import * as babylon from '@babel/parser'; import * as types from '@babel/types'; -// $FlowFixMe[cannot-resolve-module] - No typings at Meta (OSS uses a libdef) -import {decode as decodeMappings} from '@jridgewell/sourcemap-codec'; import {stableHash} from 'metro-cache'; import {getCacheKey as metroGetCacheKey} from 'metro-cache-key'; import { @@ -82,11 +80,6 @@ export type MinifierOptions = { export type MinifierResult = { code: string, - /** - * @deprecated Return `decodedMap` instead. Only read if `decodedMap` is - * missing, and will be removed. - */ - map?: BasicSourceMap, // The minified code's source map, decoded, composed with the input `map`. decodedMap?: ?BabelDecodedMap, ... @@ -240,12 +233,22 @@ const minifyCode = async ( reserved, }); + // Minifiers with no source map to lose may still set an empty `map`, e.g. + // esbuild's `transform` result, so this tests for a truthy value. + // $FlowFixMe[prop-missing] `map` isn't part of `MinifierResult`. + if (minified.decodedMap == null && Boolean(minified.map)) { + throw new Error( + `Minifier ${config.minifierPath} returned its source map as \`map\`, ` + + 'which is no longer supported. Return it as `decodedMap` instead.', + ); + } + const {lineCount, lastLineColumn} = countLines(minified.code); return { code: minified.code, lineCount, map: vlqMapFromBabelDecodedMap( - getDecodedMap(minified) ?? {mappings: [], names: []}, + minified.decodedMap ?? {mappings: [], names: []}, [lineCount, lastLineColumn], ), }; @@ -260,18 +263,6 @@ const minifyCode = async ( } }; -// A minifier may return its map already decoded, which saves decoding it here. -function getDecodedMap(minified: MinifierResult): ?BabelDecodedMap { - if (minified.decodedMap != null) { - return minified.decodedMap; - } - const {map} = minified; - if (map != null) { - return {mappings: decodeMappings(map.mappings), names: map.names}; - } - return null; -} - const disabledDependencyTransformer: DependencyTransformer = { transformIllegalDynamicRequire: () => void 0, transformImportCall: () => void 0,